JSP显示新闻

1、新建新闻表

  JSP显示新闻_第1张图片

 

 

 2、新建LoginServlet验证登录用户是否则正确

 1  //连接数据库
 2         response.setContentType("text/html; charset=utf-8");
 3         request.setCharacterEncoding("utf-8");
 4         PrintWriter writer = response.getWriter();
 5         String strName = request.getParameter("txtName");
 6         String strPwd = request.getParameter("txtPwd");
 7 
 8         Connection conn = null;
 9         Statement stmt;
10         ResultSet rs;
11         PreparedStatement pstmt;
12         try {
13             // 注册驱动程序
14             Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
15             conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1434;DatabaseName=NEWS_SYSTEM",
16                    "test", "123456");
17             System.out.println("加载成功");
18             stmt = conn.createStatement();
19 
20             String sql = "SELECT * FROM USERS WHERE NAME=? and PWD=?";
21             pstmt = conn.prepareStatement(sql);
22             pstmt.setString(1,strName);
23             pstmt.setString(2,strPwd);
24             rs = pstmt.executeQuery();
25             if (rs.next()) {
26                 Cookie cooUser = new Cookie("username", strName);
27                 cooUser.setMaxAge(60*60*24*7);
28                 response.addCookie(cooUser);
29 
30                 Cookie cooPwd = new Cookie("pwd", strPwd);
31                 cooPwd.setMaxAge(60*60*24*7);
32                 response.addCookie(cooPwd);
33 
34                 HttpSession session = request.getSession(true);
35                 session.setAttribute("username",strName);
36 
37                 String path = request.getContextPath();
38                 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
39                 String page = basePath+"yingLiang/index.html";
40                 System.out.println(basePath);
41                 response.sendRedirect(page);
42 
43             }
44             else {
45                 writer.println("用户名或密码出现错误");
46                 response.sendRedirect("login.html");
47             }
48             rs.close();
49             stmt.close();
50             conn.close();
51             pstmt.close();
52         } catch (Exception e) {
53             System.out.println(e);
54         }

3、新建登录页面index.html,用户验证通过就跳转到这个页面

 1 
 2     <div class="top">
 3             <div style="float: left"><span style="font-size: 16px;line-height: 45px;padding-left: 20px;color: #fff">主页新闻管理中心h1>span>div>
 4             <div id="ad_setting" class="ad_setting">
 5                 <a class="ad_setting_a" href="javascript:; ">[email protected]a>
 6                 <ul class="dropdown-menu-uu" style="display: none" id="ad_setting_ul">
 7                     <li class="ad_setting_ul_li"> <a href="javascript:;"><i class="icon-user glyph-icon">i>个人中心a> li>
 8                     <li class="ad_setting_ul_li"> <a href="javascript:;"><i class="icon-cog glyph-icon">i>设置a> li>
 9                     <li class="ad_setting_ul_li"> <a href="javascript:;"><i class="icon-signout glyph-icon">i> <span class="font-bold">退出span> a> li>
10                 ul>
11                 <img class="use_xl" src="images/right_menu.png" />
12             div>
13     div>
14     
15     
16     <div class="left-menu">
17         <ul id="menu">
18             <li class="menu-list">
19                <a style="cursor:pointer" class="firsta"><i  class="glyph-icon xlcd">i>主页<s class="sz">s>a>
20                 <ul>
21                     <li><a href="info.html" target="menuFrame"><i class="glyph-icon icon-chevron-right1">i>资料管理a>li>
22                     <li><a href="ShowNewsList" target="menuFrame"><i class="glyph-icon icon-chevron-right2">i>新闻管理a>li>
23                 ul>
24             li>
25         ul>
26     div>
27     
28     
29     <div id="right-content" class="right-content">
30         <div class="content">
31             <div id="page_content">
32                 <iframe id="menuFrame" name="menuFrame" src="info.html" style="overflow:visible;"
33                         scrolling="yes" frameborder="no" width="100%" height="100%">iframe>
34             div>
35         div>
36     div>

4、新建新闻显示Servlet,当用户点击新闻管理栏目时跳转到ShowNewsList.jsp新闻显示页面

 1      request.setCharacterEncoding("utf-8");
 2         NewsService newsService = new NewsService();
 3         try
 4         {
 5             List lstNews = newsService.QueryNews();
 6             request.setAttribute("lstNews",lstNews);
 7             request.getRequestDispatcher("yingLiang/ShowNewsList.jsp").forward(request, response);
 8         }
 9         catch (SQLException e)
10         {
11             e.printStackTrace();
12         }

5、新建新显示动态界面ShowNewsList.jsp

 1 
 2     
 3     产品管理
 4     
 5     
 6 
 7 
 8 
 9 class="cp_title">新闻管理
10 
class="add_cp"> 11 +添加新闻 12
13
class="table_con"> 14 15class="tb_title"> 1617181920212223 24 252627282930353637
ID 标题 内容 作者 时间 操作
${news.id} ${news.title} ${news.content} ${news.author} ${news.time} 31 class="bj_btn">编辑 32 class="sj_btn">查看 33 class="del_btn">删除 34
38
39

6、结果图:

JSP显示新闻_第2张图片

 

 

    JSP显示新闻_第3张图片

7、github地址:https://github.com/LastReincarnation/JSP-News-Display

 

你可能感兴趣的:(JSP显示新闻)