jsp新闻发布系统

一.首先建一个web工程,把tomcat容器调好,然后把jdbc的jar包导入 

建立好分层结构

 

 

.entity层,Dao层,和实现层都和oop一样,层与层进行交互,特别说一下业务逻辑层(servlet)

 建一个NewServlet类然后继承HttpServlet从而继承了它的doGet(),doPost()方法在doGet里进行

 调用doPost(request, response); 

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//准备新闻数据
INewsInfoService newsservice=new NewsInfoServiceImpl();
try {

Page page=new Page();

//页大小
int pagesize=2;
page.setPageSize(pagesize);

//2.当前是第几页
int pageIndex=1;
try {
if (request.getParameter("pageIndex")!=null) {
pageIndex=Integer.parseInt(request.getParameter("pageIndex"));
}
} catch (Exception e) {
pageIndex=1;
}
page.setPageIndex(pageIndex);


//3.总记录数
int count = newsservice.getCount();
page.setTotalrecords(count);

//4.总页数
int pageCount=count%pagesize==0 ? count/pagesize : count/pagesize+1;
page.setTotalpages(pageCount);


//3.给page中的List集合赋值

List<NewsInfo> newsList = newsservice.getOnePageData(page.getPageIndex(), page.getPageSize());
page.setList(newsList);
//放入request作用域
request.setAttribute("onePage", page);
} catch (SQLException e1) {

}


String action=request.getParameter("action"); //logout
if ("logout".equals(action)) {
//执行注销操作

//1.清除session
request.getSession().removeAttribute("uname");
//2.跳转到index
/*response.sendRedirect("/NewsManagerSystem/index.jsp");*/

request.getRequestDispatcher("/index.jsp").forward(request, response);

}else if("login".equals(action)) {
//code write here
//1.解决乱码
request.setCharacterEncoding("utf-8");
//2.解析前台传递的表单数据
String uname=request.getParameter("uname");
String upwd=request.getParameter("upwd");

//拼接成一个用户对象
UserInfo info=new UserInfo();
info.setUname(uname);
info.setUpwd(upwd);

IUserInfoService service=new UserInfoServiceImpl();

try {
boolean flag = service.isLogin(info);

if (flag) { //登录成功

//3.记录session
request.getSession().setAttribute("uname", uname);
//4.转发或者重定向 转发不需要加项目名称
request.getRequestDispatcher("/newspages/admin.jsp").forward(request, response);

}else {
response.sendRedirect("/NewsManagerSystem/index.jsp");
}
} catch (SQLException e) {
e.printStackTrace();
}//code write here

}else {
//第一次访问首页
request.getRequestDispatcher("/index.jsp").forward(request, response);
}

doPost将泛型集合放入request作用域中,然后进行转发。

  进入index.jsp获取标题位置进行页面展示。

<%
Page onePage=(Page)request.getAttribute("onePage");
for(NewsInfo item:onePage.getList()){
%>
<li><a href='news_read.jsp'><%=item.getNtitle() %></a><span> <%=item.getNcreateda() %></span></li>
<%
}
%>

登录:

登陆后获取用户名:

添加:

posted @ 2017-04-27 19:43  mashirozZ  阅读(718)  评论(0编辑  收藏  举报