JSP显示新闻
1、项目结构
java:
Web:
2、Java部分详细介绍
2.1 Controller
- GetSessionServlet:
- 显示在线人数的Servlet,具体介绍见上一篇博客https://www.cnblogs.com/luomei/p/13034052.html
- LoginServlet:
- 用来获取网站的登录信息,并传给UsersService进行处理判断
1 package Controller; 2 3 import Entity.Users; 4 import Service.UsersService; 5 import Util.CookieSave; 6 import javax.servlet.ServletException; 7 import javax.servlet.annotation.WebServlet; 8 import javax.servlet.http.HttpServlet; 9 import javax.servlet.http.HttpServletRequest; 10 import javax.servlet.http.HttpServletResponse; 11 import java.io.IOException; 12 import java.sql.SQLException; 13 14 @WebServlet(name = "Controller.LoginServlet") 15 public class LoginServlet extends HttpServlet { 16 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { 17 doGet(request,response); 18 } 19 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 20 response.setContentType("text/html;charset=utf-8"); 21 request.setCharacterEncoding("utf-8"); 22 //Login的处理 23 try { 24 Login(request,response); 25 } catch (SQLException e) { 26 e.printStackTrace(); 27 } 28 } 29 void Login(HttpServletRequest request, HttpServletResponse response) throws IOException, SQLException { 30 //获取表单中的数据的值 31 Users user=new Users(); 32 user.setName(request.getParameter("username")); 33 user.setPassword(request.getParameter("password")); 34 System.out.println("登入用户名:"+user.getName()); 35 System.out.println("登入用户密码:"+user.getPassword()); 36 System.out.println("-------------------------------"); 37 //验证用户名密码 38 UsersService usersService=new UsersService(); 39 boolean hasUsers=usersService.QueryUsers(user); 40 if(hasUsers){ 41 CookieSave cookieSave=new CookieSave(); 42 cookieSave.Save(response,"username",user.getName(),60*60*24*30); 43 cookieSave.Save(response,"password",user.getPassword(),60*60*24*30); 44 //成功登录,进入管理主界面 45 response.sendRedirect("yinglang/index.html"); 46 }else 47 { 48 response.getWriter().println("登陆失败"); 49 //登录失败,返回登陆界面 50 response.sendRedirect("login.html"); 51 } 52 } 53 }
- 用来获取网站的登录信息,并传给UsersService进行处理判断
- ShowNewsListServlet:
- 从NewsService中获取所需的数据,并将数据传递给ShowNewsList.jsp来显示
package Controller; import Entity.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 ShowNewsListServlet 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 e) { e.printStackTrace(); } } }
- 从NewsService中获取所需的数据,并将数据传递给ShowNewsList.jsp来显示
2.2 DAL
- SqlHelper
- 用以连接数据库,这里我选用的是MySql
package DAL; import com.mysql.jdbc.StatementImpl; import java.sql.*; public class SqlHelper { public static Connection getConnection() { Connection connection = null; try { Class.forName("com.mysql.jdbc.Driver"); connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/servletdata?useSSL=true&characterEncoding=utf-8&user=root&password="); System.out.println("数据库连接成功!"); System.out.println("-------------------------------"); return connection; } catch (Exception e) { e.printStackTrace(); } System.out.println("创建连接失败!"); System.out.println("-------------------------------"); return null; } //select public static ResultSet executeQuery(String SQL) { try { Connection conn=getConnection(); Statement stmt=conn.createStatement(); ResultSet rs=stmt.executeQuery(SQL); return rs; } catch (SQLException e) { e.printStackTrace(); System.out.println("查询失败!"); System.out.println("-------------------------------"); return null; } } //insert update delete public static boolean executeUpdate(String SQL) { try { Connection conn=getConnection(); Statement stmt=conn.createStatement(); int rs=stmt.executeUpdate(SQL); if (rs>0) return true; else return false; } catch (SQLException e) { e.printStackTrace(); System.out.println("更新失败!"); System.out.println("-------------------------------"); return false; } } public static void ShutDown(StatementImpl statement, Connection connection) { if (statement != null) { try { statement.close(); } catch (SQLException e) { e.printStackTrace(); } } if (connection != null) { try { connection.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
- 用以连接数据库,这里我选用的是MySql
2.3 Entity
- News
- 用于映射数据库中的news表
package Entity; import java.sql.Date; public class News { private int idnews; private String category; private String title; private String content; private String author; private Date newsdate; public int getIdnews() { return idnews; } public void setIdnews(int idnews) { this.idnews = idnews; } public String getCategory() { return category; } public void setCategory(String category) { this.category = category; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public Date getNewsdate() { return newsdate; } public void setNewsdate(Date newsdate) { this.newsdate = newsdate; } }
- 用于映射数据库中的news表
- Users
- 用于映射数据库中的users表
package Entity; public class Users { String name="";//用户名 String password="";//用户密码 public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
- 用于映射数据库中的users表
2.4 Service
- NewsService
- 提供News类的数据服务(目前只完善了查询所有新闻内容的函数)
package Service; import DAL.SqlHelper; import Entity.News; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; public class NewsService { public void AddNews(){} public void DeleteNews(){} public void UpdateNews(){} public List<News> QueryNews() throws SQLException { String sql="Select * from news"; ResultSet rs= SqlHelper.executeQuery(sql); List<News> lsNews=new ArrayList<News>(); while (rs.next()) { News news=new News(); news.setIdnews(rs.getInt(1)); news.setCategory(rs.getString(2)); news.setTitle(rs.getString(3)); news.setContent(rs.getString(4)); news.setAuthor(rs.getString(5)); news.setNewsdate(rs.getDate(6)); lsNews.add(news); } return lsNews; } public void GetNews(){} }
- 提供News类的数据服务(目前只完善了查询所有新闻内容的函数)
- UsersService
- 提供Users类的数据服务(目前只完善了验证用户信息是否正确的函数)
package Service; import DAL.SqlHelper; import Entity.News; import Entity.Users; import com.mysql.jdbc.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class UsersService { public void AddUsers(){} public void DeleteUsers(){} public void UpdateUsers(){} public boolean QueryUsers(Users user) throws SQLException { //验证用户名存在性 PreparedStatement pstmt; String sql = "select * from users where username=‘"+user.getName()+"‘ and password=‘"+user.getPassword()+"‘"; ResultSet rs= SqlHelper.executeQuery(sql); if (rs.next()) { return true; } return false; } public void GetUsers(){} }
- 提供Users类的数据服务(目前只完善了验证用户信息是否正确的函数)
2.5 Listener
- OnlineCounterListener:
- 监听Session的创建与销毁,具体介绍见上一篇博客https://www.cnblogs.com/luomei/p/13034052.html
2.6 Filter
- GBFilter:
- 过滤器,处理乱码问题,具体介绍见上一篇博客https://www.cnblogs.com/luomei/p/13034052.html
2.7 Util
- CookieSave:
- 保存Cookie的工具类,具体介绍见上一篇博客https://www.cnblogs.com/luomei/p/13034052.html
3、Web部分详细介绍
3.1 登录
登陆部分的网页无需修改,具体介绍见上一篇博客https://www.cnblogs.com/luomei/p/13034052.html
3.2 新闻后台管理
后台管理部分,我们需要先把模板内容调整为合适的内容,其中index.html是后台的主界面,其中菜单部分,我们需要把选项的href修改成ShowNewsListServlet,用以通过Servlet来实现网页信息的动态展示
在ShowNewsListjsp文件中,我们还需要增加循环的代码,其中的lsNews数据就是在ShowNewsListServlet通过服务获取到并传递给该jsp文件的数据
3.3 新闻展示(暂时还未使用)
3.4 web.xml 配置文件
具体功能与上一博客介绍的无区别,可参考上一博客https://www.cnblogs.com/luomei/p/13034052.html进行配置。
4、效果展示
数据库数据:
用户数据
新闻数据
网页展示:
登录
主页
新闻管理页