• 第四次作业——JSP显示新闻


    1.数据库构建:

     利用MySQL workbench 完成可视化表的建立:

    这是建立的用户登录表。

    这是随便建立的一个简单的两行“新闻”列表。

    2.用户登录操作:

    用户登录时,用servlet获取用户名和密码,这个和第三次作业的servlet第一个操作一致,而因为单独制作一个界面有些耗时,于是继续延用了作业二中的西南石油大学用户登录界面作为此次的用户登录验证界面。

    而登录成功后将进入一个简易的JSP页面,页面显示用户名,和新闻查询链接(即JSP新闻显示链接):

    登录界面:

    登录成功后,跳转至index.jsp界面:

    相关代码:

     1 import javax.servlet.ServletException;
     2 import javax.servlet.annotation.WebServlet;
     3 import javax.servlet.http.Cookie;
     4 import javax.servlet.http.HttpServlet;
     5 import javax.servlet.http.HttpServletRequest;
     6 import javax.servlet.http.HttpServletResponse;
     7 import java.io.IOException;
     8 import java.io.PrintWriter;
     9 import java.sql.Connection;
    10 import java.sql.DriverManager;
    11 import java.sql.ResultSet;
    12 import java.sql.Statement;
    13 
    14 
    15 @WebServlet(name = "LoginServlet")
    16 public class LoginServlet extends HttpServlet {
    17     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    18                 response.setContentType("text/html; charset=UTF-8");
    19                 request.setCharacterEncoding("UTF-8");
    20 
    21         PrintWriter output=response.getWriter();
    22         String uname = request.getParameter("username");
    23         String pwd = request.getParameter("pwd");
    24         //output.println("账号:  "+username+"  密码:  "+pwd+"<br>");
    25         ResultSet rs ;
    26         Statement statement = null;
    27         Connection conn =null;
    28 
    29         try {
    30             Class.forName("com.mysql.cj.jdbc.Driver");
    31             conn = DriverManager.getConnection("jdbc:mysql://localhost:3306","root","pb112227");
    32             String sql="select * from test_db.login_chitestlist where username='"+uname+"' and password='"+pwd+"'";
    33             statement =conn.createStatement();
    34             rs = statement.executeQuery(sql);
    35 
    36 
    37             if(rs.next()){
    38 
    39                 request.setAttribute("username",uname);
    40                 request.getRequestDispatcher("index.jsp").forward(request,response);
    41             }else {
    42                 output.println("账号或密码错误");
    43             }
    44 
    45 
    46         } catch (Exception e) {
    47             e.printStackTrace();
    48         }
    49     }
    50 
    51     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    52         doGet(request,response);
    53     }
    54 }
    loginServletLogin
     1 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
     2 <html>
     3   <head>
     4     <title>$Title$</title>
     5   </head>
     6   <body>
     7     <h3>登陆成功</h3>
     8     <h3>欢迎,${username}</h3>
     9     <a href="http://localhost:8080/j2ee_war_exploded/ShowNewsServlet">新闻查询</a>
    10   </body>
    11 </html>
    index.jsp

    3.JSP新闻显示:

     (1)准备工作:因为要用到jstl语言,需要配置两个jar包:

            将该两个jar安装或者复制到了lib文件夹下后,到项目结构——依赖处引用配置:

             

            

             

    后续在最终的新闻展示的JSP界面引用资源:

    (2)建立两个类——News,NewsService。

        News:用于暂存从数据库中读取的信息和存储新闻信息的单位。注意:变量命名时最好首字母小写,后续新闻显示的调用EL语言时,需要用到News中对应变量的get{}方法。而利用get方法获取变量值时,需要先将变量名第一个字母改为大写,在在前方加上get,如果找到对应的get方法调用,否则错误报告,显示EL语言找不到对应属性。也可以在EL语言中直接试用get方法。总之,需要注意编码规范。

        

     1 public class News {
     2     private int newsId;
     3     private String newsContent;
     4     private  String newsAuthor;
     5     private  String newsTitle;
     6 
     7     public int getNewsId() {
     8         return newsId;
     9     }
    10 
    11     public void setNewsId(int newsId) {
    12         this.newsId = newsId;
    13     }
    14 
    15     public String getNewsContent() {
    16         return newsContent;
    17     }
    18 
    19     public void setNewsContent(String newsContent) {
    20         this.newsContent = newsContent;
    21     }
    22 
    23     public String getNewsAuthor() {
    24         return newsAuthor;
    25     }
    26 
    27     public void setNewsAuthor(String newsAuthor) {
    28         this.newsAuthor = newsAuthor;
    29     }
    30 
    31     public String getNewsTitle() {
    32         return newsTitle;
    33     }
    34 
    35     public void setNewsTitle(String newsTitle) {
    36         this.newsTitle = newsTitle;
    37     }
    38 
    39     public News() {
    40     }
    41 }
    News

        

        NewsService:作为服务层操作,便于Servlet控制层进行对数据库信息进行查询操作。即,为后续JSP新闻显示界面提供数据。

     1 import java.sql.*;
     2 import java.util.ArrayList;
     3 import java.util.List;
     4 
     5 public class NewsService {
     6 
     7     public List<News> QueryNews() throws ClassNotFoundException, SQLException {
     8         Statement statement = null;
     9         Connection conn ;
    10         ResultSet rs;
    11         Class.forName("com.mysql.cj.jdbc.Driver");
    12         conn = DriverManager.getConnection("jdbc:mysql://localhost:3306?useSSL=false","root","pb112227");
    13         String sql = "Select * from test_db.news";
    14 
    15         statement=conn.createStatement();
    16         rs = statement.executeQuery(sql);
    17 
    18         List<News> NewsList = new ArrayList<News>();
    19 
    20         while (rs.next()){
    21             News n = new News();
    22             n.setNewsId(rs.getInt("newsID"));
    23             n.setNewsTitle(rs.getString("title"));
    24             n.setNewsAuthor(rs.getString("author"));
    25             n.setNewsContent(rs.getString("content"));
    26             NewsList.add(n);
    27         }
    28         return  NewsList;
    29     }
    30 
    31 
    32 }
    News Service

    (3)控制层操作:

        ShowNewsServlet:接受来自NewsService的新闻列表信息,并将其转发至ShowNews.jsp界面,便于EL语言调用数据。

     1 import javax.servlet.ServletException;
     2 import javax.servlet.annotation.WebServlet;
     3 import javax.servlet.http.HttpServlet;
     4 import javax.servlet.http.HttpServletRequest;
     5 import javax.servlet.http.HttpServletResponse;
     6 import java.io.IOException;
     7 import java.util.List;
     8 
     9 @WebServlet(urlPatterns = "/ShowNewsServlet")
    10 public class ShowNewsServlet extends HttpServlet {
    11     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    12                 doGet(request,response);
    13     }
    14 
    15     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    16         NewsService newsService=new NewsService();
    17         try {
    18             List<News> lstNews=newsService.QueryNews();
    19 
    20             for (News s:lstNews
    21                  ) {
    22                 System.out.println(s.getNewsId());
    23             }
    24             request.setAttribute("lstNews",lstNews);
    25             request.getRequestDispatcher("ShowNews.jsp").forward(request,response);
    26         } catch (Exception e) {
    27             e.printStackTrace();
    28         }
    29     }
    30 }
    ShowNewsServlet

    (4)最终显示页面(ShowNews.jsp):

        该页面的模板格式是利用现有的模板进行套用。因为模板规格和我的数据库信息不一致,做出了适当的修改,删除了一些不用的链接,和多余的表格。而后利用EL语言将获取的数据库信息显示在表格上。

      

     1 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
     2 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
     3 <html>
     4 <head>
     5     <title>Title</title>
     6 </head>
     7 <body>
     8 <span class="cp_title">新闻内容</span>
     9 <div class="add_cp">
    10 </div>
    11 <div class="table_con">
    12     <table>
    13         <tr class="tb_title">
    14             <td width="20%">ID</td>
    15             <td width="30%">标题</td>
    16             <td width="40%">内容</td>
    17             <td width="10%">作者</td>
    18 
    19         </tr>
    20         <c:forEach var="news" items="${lstNews}" varStatus="status">
    21             <tr>
    22                 <td >${news.newsId}</td>
    23                 <td >${news.newsTitle}</td>
    24                 <td >${news.newsContent} </td>
    25                 <td >${news.newsAuthor}</td>
    26             </tr>
    27         </c:forEach>
    28     </table>
    29 </div>
    30 </body>
    31 </html>
    ShowNews

    (5)效果展示:

     4.总结

       由于该段时间任务较多,且对Java Web的掌握不够,功能做的较为简陋,只是完成了初步的新闻界面展示,不过即便是初步完成,中间也是遇见了一些问题,其中最关键的就是在News类中的字段命名不规范。命名不规范导致后面一直报错找不到属性值。后面哪怕重构了字段,但是还是要报错字段不可读,总之问题颇多。不过在找问题的这段时间内,我对于JSP的操作更加娴熟了,之间的业务逻辑层次也更加熟练,多次的失败帮我累计了不少的经验。

      后续附上Mysql时区问题的解决方式,网上不少是通过Cmd命令行操作设置时区,但是重启电脑后就会失效,而且从新设置时区还会出现设置不上的问题。后来网上找到了一个很简答的方法:

    5.码云地址:https://gitee.com/shuaipb/j2ee-homework/commits/master

  • 相关阅读:
    Kafka Tuning Recommendations
    ​Installing the Ranger Kafka Plug-in
    Problem of Creating Topics in Kafka with Kerberos
    Step by Step Recipe for Securing Kafka with Kerberos
    ERROR:"org.apache.zookeeper.KeeperException$NoAuthException: KeeperErrorCode = NoAuth for /config/topics/test" when creating or deleting Kafka operations authorized through the Ranger policies
    Scala操作Hbase空指针异常java.lang.NullPointerException处理
    使用ranger对kafka进行鉴权
    IBM developer:Setting up the Kafka plugin for Ranger
    IBM developer:Kafka ACLs
    Exception in thread "main" org.I0Itec.zkclient.exception.ZkAuthFailedException: Authentication failure is thrown while creating kafka topic
  • 原文地址:https://www.cnblogs.com/XiaoPB/p/13128707.html
Copyright © 2020-2023  润新知