• com.jsj.web=====新闻发布系统(10)


    2020-05-15

    这个有点像快递站点(快递应该有学我们计算机思想的)

    接收快递,发送快递到客户手上

    接收界面信息,发生到下一级

    AdminServlet

     1 package com.jsj.web;
     2 
     3 import com.jsj.entity.Admin;
     4 import com.jsj.factory.ServiceFactory;
     5 import com.jsj.service.AdminService;
     6 
     7 import javax.servlet.ServletException;
     8 import javax.servlet.annotation.WebServlet;
     9 import javax.servlet.http.HttpServlet;
    10 import javax.servlet.http.HttpServletRequest;
    11 import javax.servlet.http.HttpServletResponse;
    12 import java.io.IOException;
    13 import java.io.PrintWriter;
    14 
    15 //注解用于标注在一个继承了HttpServlet类之上,属于类级别的注解。
    16 @WebServlet("/admin")
    17 public class AdminServlet extends HttpServlet {
    18 
    19     //调用工厂类创建对象
    20     //括号是强制转换
    21     private AdminService adminService = (AdminService) ServiceFactory.getService("AdminService");
    22 
    23     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
    24         response.setContentType("text/html;charset=UTF-8");
    25         try(PrintWriter out = response.getWriter()){
    26             /*
    27             getParameter:它是一种取参数的方法。把jsp文件中的数据读取到出来。然后就可以封装利用起来
    28              */
    29             String username = request.getParameter("username");
    30             String password = request.getParameter("password");
    31 
    32             //调用service层的login方法
    33             Admin admin = adminService.login(username,password);
    34 
    35             if (admin.getId()!=null){
    36 
    37                 request.getSession().setAttribute("adminStatus",true);
    38                 request.getSession().setAttribute("admin",admin);
    39                 out.println("登录成功,3秒后跳转到管理中心!如果没有跳转请点<a href='/admin/manage'>这里</a>");
    40                 response.setHeader("refresh", "2;url=/admin/manage");
    41             }else {
    42                 out.println("用户名或密码错误,请<a href='/admin'>重试</a>");
    43                 // 二秒跳到其他页面 response.setHeader("refresh","2;URL=otherPagename");
    44                 response.setHeader("refresh", "2;url=/admin");
    45             }
    46         }
    47     }
    48 
    49     @Override
    50     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    51         request.getRequestDispatcher("/WEB-INF/view/admin/login.jsp").forward(request,response);
    52     }
    53 }
    View Code

    IndexServlet

     1 package com.jsj.web;
     2 
     3 import com.jsj.entity.NewsCateVo;
     4 import com.jsj.entity.NewsCate;
     5 import com.jsj.factory.ServiceFactory;
     6 import com.jsj.service.NewsCateService;
     7 
     8 import javax.servlet.ServletException;
     9 import javax.servlet.annotation.WebServlet;
    10 import javax.servlet.http.HttpServlet;
    11 import javax.servlet.http.HttpServletRequest;
    12 import javax.servlet.http.HttpServletResponse;
    13 import java.io.IOException;
    14 import java.util.List;
    15 
    16 @WebServlet("/index")
    17 public class IndexServlet extends HttpServlet {
    18 
    19     private NewsCateService newsCateService = (NewsCateService) ServiceFactory.getService("NewsCateService");
    20 
    21     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    22         List<NewsCate> allCate = newsCateService.getAllCate();
    23         request.setAttribute("allCate",allCate);
    24         List<NewsCateVo> allNewsCateVo = newsCateService.getAllNewsCateVo(5);
    25         request.setAttribute("allNewsCateVo", allNewsCateVo);
    26         request.getRequestDispatcher("/WEB-INF/view/index.jsp").forward(request,response);
    27     }
    28 
    29 }
    View Code

    LoginServlet

     1 package com.jsj.web;
     2 
     3 import com.jsj.entity.User;
     4 import com.jsj.factory.ServiceFactory;
     5 import com.jsj.service.UserService;
     6 
     7 import javax.servlet.ServletException;
     8 import javax.servlet.annotation.WebServlet;
     9 import javax.servlet.http.Cookie;
    10 import javax.servlet.http.HttpServlet;
    11 import javax.servlet.http.HttpServletRequest;
    12 import javax.servlet.http.HttpServletResponse;
    13 import java.io.IOException;
    14 import java.io.PrintWriter;
    15 
    16 @WebServlet("/login")
    17 public class LoginServlet extends HttpServlet {
    18     //通过ServiceFactory工厂生产UserService对象
    19     private UserService userService = (UserService) ServiceFactory.getService("UserService");
    20 
    21     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
    22         //设置编码方式,中文不会出现乱码
    23         response.setContentType("text/html;charset=UTF-8");
    24 
    25         try(PrintWriter out = response.getWriter()){
    26             //获取login.jsp界面的用户名和密码
    27             String username = request.getParameter("username");
    28             String password = request.getParameter("password");
    29 
    30 
    31             //通过调用login方法查询账号密码,返回对象
    32             User user = userService.login(username,password);
    33             //如果账号密码正确的话,id就不为空
    34             if (user.getId()!=null){
    35                 //判断是否有点 记住我这个
    36                 if (request.getParameter("remember")!=null){
    37                     //创建cookie对象,参数分别代表 cookie名称 和 值
    38                     Cookie usernameCookie = new Cookie("rememberUsername",username);
    39                     //是设置cookie最大保存时间
    40                     usernameCookie.setMaxAge(7*24*60*60);
    41                     //创建cookie对象,参数分别代表 cookie名称 和 值
    42                     Cookie passwordCookie = new Cookie("rememberPassword",password);
    43                     //是设置cookie最大保存时间
    44                     passwordCookie.setMaxAge(7*24*60*60);
    45                     //添加cookie到服务百器端。可以使用抓包工具查看到,然后服务器给浏览器返回set-cookie键值对
    46                     response.addCookie(usernameCookie);
    47                     response.addCookie(passwordCookie);
    48                 }
    49                 //这个登录成功后跳到index.jsp(主界面)
    50                 out.println("登录成功,3秒后跳转到首页!如果没有跳转请点<a href='/'>这里</a>");
    51                 //参数表示 session的名称 和 值(对象)
    52                 request.getSession().setAttribute("userStatus",true);
    53                 request.getSession().setAttribute("user",user);
    54                 //设置表头,刷新
    55                 response.setHeader("refresh", "2;url=/");
    56             }else {
    57                 out.println("用户名或密码错误,请<a href='/login'>重试</a>");
    58                 response.setHeader("refresh", "2;url=/login");
    59             }
    60         }
    61     }
    62 
    63     @Override
    64     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    65         Cookie[] cookies = request.getCookies();
    66         //这个是判断记住密码时
    67         for (Cookie cookie:cookies) {
    68             //这是获得账号
    69             if (cookie.getName().equals("rememberUsername")){
    70                 request.setAttribute("rememberUsername",cookie.getValue());
    71             }
    72             //这是获得密码
    73             if (cookie.getName().equals("rememberPassword")){
    74                 request.setAttribute("rememberPassword",cookie.getValue());
    75             }
    76         }
    77         //重定向,将账号密码传过去
    78         request.getRequestDispatcher("/WEB-INF/view/login.jsp").forward(request,response);
    79     }
    80 }
    View Code

    LogoutServlet

     1 package com.jsj.web;
     2 
     3 import javax.servlet.annotation.WebServlet;
     4 import javax.servlet.http.HttpServlet;
     5 import javax.servlet.http.HttpServletRequest;
     6 import javax.servlet.http.HttpServletResponse;
     7 import java.io.IOException;
     8 
     9 @WebServlet("/logout")
    10 public class LogoutServlet extends HttpServlet {
    11 
    12     @Override
    13     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
    14         //
    15         response.setContentType("text/html;charset=UTF-8");
    16         if (request.getSession().getAttribute("user")!=null){
    17             request.getSession().removeAttribute("userStatus");
    18             request.getSession().removeAttribute("user");
    19         }
    20         if (request.getSession().getAttribute("admin")!=null){
    21             request.getSession().removeAttribute("adminStatus");
    22             request.getSession().removeAttribute("admin");
    23         }
    24         response.getWriter().println("注销成功,3秒后跳转到首页!如果没有跳转请点<a href='/'>这里</a>");
    25         response.setHeader("refresh", "2;url=/");
    26     }
    27 }
    View Code

    NewsCateServlet

     1 package com.jsj.web;
     2 
     3 import com.jsj.entity.News;
     4 import com.jsj.entity.NewsCate;
     5 import com.jsj.entity.Page;
     6 import com.jsj.factory.ServiceFactory;
     7 import com.jsj.service.NewsCateService;
     8 import com.jsj.service.NewsService;
     9 
    10 import javax.servlet.ServletException;
    11 import javax.servlet.annotation.WebServlet;
    12 import javax.servlet.http.HttpServlet;
    13 import javax.servlet.http.HttpServletRequest;
    14 import javax.servlet.http.HttpServletResponse;
    15 import java.io.IOException;
    16 import java.util.List;
    17 
    18 @WebServlet("/newsCate")
    19 public class NewsCateServlet extends HttpServlet {
    20 
    21     private NewsService newsService = (NewsService) ServiceFactory.getService("NewsService");
    22 
    23     private NewsCateService newsCateService = (NewsCateService) ServiceFactory.getService("NewsCateService");
    24 
    25     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    26         try {
    27             int cateId = Integer.valueOf(request.getParameter("cateId"));
    28             NewsCate newsCate = newsCateService.getNewsCateById(cateId);
    29             if (newsCate == null){
    30                 request.getRequestDispatcher("/WEB-INF/view/error/error.jsp").forward(request, response);
    31             }else {
    32                 int pageIndex = request.getParameter("pageIndex") == null ? 1 : Integer.valueOf(request.getParameter("pageIndex"));
    33                 int pageSize = 10;
    34                 Page<News> newsPage = newsService.getNewsPageByCate(cateId,pageIndex,pageSize);
    35                 if (newsPage.getBeanList() == null || newsPage.getBeanList().size() == 0){
    36                     request.getRequestDispatcher("/WEB-INF/view/error/error.jsp").forward(request, response);
    37                 }
    38                 request.setAttribute("newsPage", newsPage);
    39                 request.setAttribute("cate",newsCate);
    40                 List<NewsCate> allCate = newsCateService.getAllCate();
    41                 request.setAttribute("allCate",allCate);
    42                 request.getRequestDispatcher("/WEB-INF/view/newsCate.jsp").forward(request,response);
    43             }
    44         }catch (Exception e){
    45             request.getRequestDispatcher("/WEB-INF/view/error/error.jsp").forward(request, response);
    46         }
    47 
    48     }
    49 }
    View Code

    NewsServlet

     1 package com.jsj.web;
     2 
     3 import com.jsj.entity.News;
     4 import com.jsj.entity.NewsCate;
     5 import com.jsj.factory.ServiceFactory;
     6 import com.jsj.service.NewsCateService;
     7 import com.jsj.service.NewsService;
     8 
     9 import javax.servlet.ServletException;
    10 import javax.servlet.annotation.WebServlet;
    11 import javax.servlet.http.HttpServlet;
    12 import javax.servlet.http.HttpServletRequest;
    13 import javax.servlet.http.HttpServletResponse;
    14 import java.io.IOException;
    15 import java.util.List;
    16 
    17 @WebServlet("/news")
    18 public class NewsServlet extends HttpServlet {
    19 
    20     private NewsService newsService = (NewsService) ServiceFactory.getService("NewsService");
    21 
    22     private NewsCateService newsCateService = (NewsCateService) ServiceFactory.getService("NewsCateService");
    23 
    24     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    25         try {
    26             int newsId = Integer.valueOf(request.getParameter("newsId"));
    27             News news = newsService.getNewsById(newsId);
    28             if (news == null){
    29                 request.getRequestDispatcher("/WEB-INF/view/error/error.jsp").forward(request, response);
    30             }else {
    31                 request.setAttribute("news",news);
    32                 List<NewsCate> allCate = newsCateService.getAllCate();
    33                 request.setAttribute("allCate",allCate);
    34                 request.getRequestDispatcher("/WEB-INF/view/news.jsp").forward(request,response);
    35             }
    36         }catch (Exception e){
    37             request.getRequestDispatcher("/WEB-INF/view/error/error.jsp").forward(request, response);
    38         }
    39 
    40     }
    41 }
    View Code

    RegisisterServlet

     1 package com.jsj.web;
     2 
     3 import com.jsj.entity.User;
     4 import com.jsj.factory.ServiceFactory;
     5 import com.jsj.service.UserService;
     6 
     7 import javax.servlet.ServletException;
     8 import javax.servlet.annotation.WebServlet;
     9 import javax.servlet.http.HttpServlet;
    10 import javax.servlet.http.HttpServletRequest;
    11 import javax.servlet.http.HttpServletResponse;
    12 import java.io.IOException;
    13 import java.io.PrintWriter;
    14 import java.text.DateFormat;
    15 import java.text.ParseException;
    16 import java.text.SimpleDateFormat;
    17 
    18 @WebServlet("/register")
    19 public class RegisterServlet extends HttpServlet {
    20 
    21     private UserService userService = (UserService) ServiceFactory.getService("UserService");
    22 
    23     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
    24         //告诉你我是用utf -8编码的
    25         response.setContentType("text/html;charset=UTF-8");
    26 
    27         try(PrintWriter out = response.getWriter()){
    28             //创建实体对象
    29             User user = new User();
    30             //获取用户名加密码
    31             user.setUsername(request.getParameter("username"));
    32             user.setPassword(request.getParameter("password"));
    33             //日期格式化
    34             DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    35             try {
    36                 //获取生日日期
    37                 user.setBirthday(dateFormat.parse(request.getParameter("birthday")));
    38             } catch (ParseException ignored) {}
    39             //获取邮箱
    40             user.setEmail(request.getParameter("email"));
    41             //获取手机号
    42             user.setTelNumber(request.getParameter("telNumber"));
    43 
    44             int res = userService.register(user);
    45 
    46             if (res>0){
    47                 out.println("注册成功,3秒后跳转到首页!或现在去<a href='/login'>登录</a>");
    48                 response.setHeader("refresh", "2;url=/");
    49             }else {
    50                 out.println("注册失败!请<a href='/register'>重试</a>重试");
    51                 response.setHeader("refresh", "2;url=/register");
    52             }
    53         }
    54     }
    55 
    56     @Override
    57     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    58         request.getRequestDispatcher("/WEB-INF/view/register.jsp").forward(request,response);
    59     }
    60 }
    View Code
    会当凌绝顶,一览众山小
  • 相关阅读:
    if 语句运用
    c#语言基础
    关于条件运算符的应用
    Could not open Hibernate Session for transaction;
    node to traverse cannot be null!
    Struts2文件下载中文名乱码
    Spring MVC框架下的第一个Hello World程序
    strtus2中的defaultactionref无效的解决方法
    c 笔记
    工具系列1
  • 原文地址:https://www.cnblogs.com/quenvpengyou/p/12896515.html
Copyright © 2020-2023  润新知