JSP显示新闻 第四次作业
小组成员:郭昭杰 唐任峻杰
码云地址:https://gitee.com/ashes-g/JSP
一、数据库相关准备
新闻相关信息数据
登录相关信息数据
二、新闻类
新建新闻类News,用于获取和设置新闻的各项属性
package Service; import java.util.Date; public class News { public News(){} private int newsID; private String newsCategory; private String newsTitle; private String newsContent; private String newsAuthor; private Date newsDate; public int getNewsID(){ return newsID; } public void setNewsID(int newsID){ this.newsID=newsID; } public String getNewsCategory(){ return newsCategory; } public void setNewsCategory(String newsCategory){ this.newsCategory= this.newsCategory; } public String getNewsTitle(){ return newsTitle; } public void setNewsTitle(String newsTitle){ this.newsTitle= this.newsTitle; } public String getNewsContent(){ return newsContent; } public void setNewsContent(String newsContent){ this.newsContent= this.newsContent; } public String getNewsAuthor(){ return newsAuthor; } public void setNewsAuthor(String newsAuthor){ this.newsAuthor= this.newsAuthor; } public Date getNewsDate(){ return newsDate; } public void setNewsDate(java.sql.Date newsDate){ this.newsDate= this.newsDate; } }
三、新闻服务类
新建新闻服务类NewsService,向数据库中查询所有新闻信息,并将每条新闻生成一个新闻对象装入List中并返回
package Controller; import Service.News; import Service.NewsService; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.sql.SQLException; import java.util.List; @WebServlet(name ="Controller.ShowNewsListServlet") public class ShowNewsServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); NewsService newsService=new NewsService(); try { List<News> lsNews=newsService.QueryNews(); request.setAttribute("lsNews",lsNews); request.getRequestDispatcher("ShowNewsList.jsp").forward(request,response); } catch (SQLException | ClassNotFoundException e) { e.printStackTrace(); } } }
四、登录
根据用户输入的用户名和密码查询登录信息数据库,若验证正确则进入index.jsp页面显示欢迎信息,若验证失败则提示用户名或密码错误
package Controller; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.*; import java.io.IOException; import java.io.PrintWriter; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; @WebServlet(name = "Controller.LoginServlet") public class LoginServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html; charset=UTF-8"); request.setCharacterEncoding("UTF-8"); PrintWriter output=response.getWriter(); String username = request.getParameter("username"); String pwd = request.getParameter("pwd"); //output.println("账号: "+username+" 密码: "+pwd+"<br>"); ResultSet rs ; Statement statement = null; Connection conn =null; try { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection("jdbc:mysql://localhost:3306","root","Guozhaojie610"); String sql="select * from new_schema.login where username='"+username+"' and password='"+pwd+"'"; statement =conn.createStatement(); rs = statement.executeQuery(sql); if(rs.next()){ request.setAttribute("username",username); request.getRequestDispatcher("index.jsp").forward(request,response); }else { output.println("账号或密码错误"); } } catch (Exception e) { e.printStackTrace(); } } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request,response); } }
五、新闻显示
进入欢迎页面5秒后自动跳转至新闻显示页面
欢迎页面:
<%-- Created by IntelliJ IDEA. User: Administrator Date: 2020/6/2 Time: 16:36 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <% response.setHeader("refresh", "5;ShowNewsList");%> <html> <head> <title>$Title$</title> </head> <body> <h3>登陆成功!</h3> <h4>当前用户:${username}</h4> <h4>5秒后自动跳转至新闻页面...</h4> </body> </html>
新闻显示页面调用控制层代码获取新闻信息并显示
控制层代码:
package Controller; import Service.News; import Service.NewsService; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.sql.SQLException; import java.util.List; @WebServlet(name ="Controller.ShowNewsListServlet") public class ShowNewsServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); NewsService newsService=new NewsService(); try { List<News> lsNews=newsService.QueryNews(); request.setAttribute("lsNews",lsNews); request.getRequestDispatcher("ShowNewsList.jsp").forward(request,response); } catch (SQLException | ClassNotFoundException e) { e.printStackTrace(); } } }
六、最终效果截图
登录界面:
登录成功:
新闻页面: