100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > jsp简易实现:过滤器实现用户未注册时 跳转到登录页面

jsp简易实现:过滤器实现用户未注册时 跳转到登录页面

时间:2020-06-07 10:08:38

相关推荐

jsp简易实现:过滤器实现用户未注册时 跳转到登录页面

使用过滤器简单监控用户是否登录 如果没有登录则让用户进入登录页面登录后才能访问

1-前端jsp页面

1.1 login.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head><title>登录页面</title><style>.panel{width: 500px;}</style><link rel="stylesheet" href="${pageContext.request.contextPath}/bootstrap/css/bootstrap.min.css"><script src="${pageContext.request.contextPath}/bootstrap/js/jquery-2.2.3.min.js"></script><script src="${pageContext.request.contextPath}/bootstrap/js/bootstrap.min.js"></script></head><body><div class="container" style="margin-left:690px;margin-top: 20px;"><div class="panel panel-primary"><div class="panel-heading"><div class="panel-title"><h3>用户登录</h3></div></div><div class="panel-body"><form class="form-signin" method="post" action="${pageContext.request.contextPath}/login"><label class="sr-only">用户名</label><input type="text" name="username" style="margin-bottom: 10px;" class="form-control" placeholder="请输入用户名" required autofocus><label class="sr-only">密码</label><input type="password" style="margin-bottom: 10px;" name="password" class="form-control" placeholder="请输入密码" required><button class="btn btn-lg btn-primary btn-block" type="submit">登录</button></form></div></div></div></body></html>

1.2 list.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %><%@taglib prefix="c" uri="/jsp/jstl/core" %><html><head><title>学生列表</title></head><body><h1>${studs}</h1></body></html>

2-后端代码

2.1 pojo实体类

Student

package com.zelin.pojo;import lombok.AllArgsConstructor;import lombok.Data;import lombok.NoArgsConstructor;@Data@AllArgsConstructor@NoArgsConstructorpublic class Student {private int sid;private String sname;private String sex;private int age;private String addr;private int cid;private String cname;public Student(String sname, String sex, int age, String addr, int cid){this.sname = sname;this.sex = sex;this.age = age;this.addr = addr;this.cid = cid;}public Student(int sid, String sname, String sex, int age, String addr, int cid){this(sname,sex,age,addr,cid);this.sid = sid;}}

2.2 web目录下创建servlet目录以及filter目录和listener目录

2.2.1-servlet目录:

LoginServlet

package com.zelin.web.servlet;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 javax.servlet.http.HttpSession;import java.io.IOException;/*** @author wf* @date -10-10 17:40*/@WebServlet("/login")public class LoginServlet extends HttpServlet {@Overrideprotected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//1.获取前端传来的参数String username = req.getParameter("username");String password = req.getParameter("password");System.out.println("username = " + username);System.out.println("password = " + password);//2.将用户名添加到session作用域中HttpSession session = req.getSession();session.setAttribute("username",username);//3.重定向resp.sendRedirect(req.getContextPath()+"/student?cmd=list");}}

StudentServlet

package com.zelin.web.servlet;import cn.hutool.core.util.StrUtil;import com.zelin.pojo.Student;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.util.ArrayList;import java.util.List;/*** @author wf* @date -10-11 11:23*/@WebServlet("/student")public class StudentServlet extends HttpServlet {@Overrideprotected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {String cmd = req.getParameter("cmd");if(StrUtil.isNotBlank(cmd)){if("list".equals(cmd)){list(req,resp); //学生列表}}}//1.学生列表private void list(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {List<Student> students = new ArrayList<>();Student stu1 = new Student(101,"刘亦菲","女",28,"上海",1001);Student stu2 = new Student(102,"刘诗诗","女",30,"杭州",1001);Student stu3 = new Student(103,"佟丽娅","女",37,"新疆",1001);students.add(stu1);students.add(stu2);students.add(stu3);req.setAttribute("studs",students);req.getRequestDispatcher("/WEB-INF/student/list.jsp").forward(req,resp);}}

2.2.2 filter目录

CharacterFilter:解决中文乱码

package com.zelin.web.filter;import javax.servlet.*;import javax.servlet.annotation.WebFilter;import java.io.IOException;/*** @author wf* @date -10-10 17:47*/@WebFilter(urlPatterns ="/*" )public class CharacterEncodeingFilter implements Filter {@Overridepublic void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {req.setCharacterEncoding("UTF-8");chain.doFilter(req,resp);}}

LoginFilter

package com.zelin.web.filter;import cn.hutool.core.util.StrUtil;import javax.servlet.*;import javax.servlet.annotation.WebFilter;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;import java.io.IOException;/*** @author wf* @date -10-11 11:31*///判断用户是否登陆 如果登陆 就访问学生列表页面 如果未登录 就让用户跳转到登录页面@WebFilter(urlPatterns = "/*")public class LoginFilter implements Filter {@Overridepublic void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {System.out.println("判断用户是否登录过滤器.....");HttpServletRequest request = (HttpServletRequest) req;HttpServletResponse response = (HttpServletResponse) resp;//1.得到请求地址 如果是登录请求 就放行String requestURI = request.getRequestURI(); //获取后面的请求参数//2.判断请求地址是否是登录请求 说明正在跳转到登录页面或者说用户正在跳转到学生列表页面if(requestURI.contains("login.jsp") || requestURI.contains("/login")){chain.doFilter(req,resp); //此时过滤器就需要放行}else{//说明不是进入登录页面 别的请求方式 这时候就需要判断用户是否已经登录//3.获取session中的用户HttpSession session = request.getSession();String username = (String) session.getAttribute("username");//4.判断用户是否已经登录if(StrUtil.isBlank(username)){//如果用户为空 说明没有用户 需要让此用户跳转到登录页面response.sendRedirect(request.getContextPath()+"/login.jsp");}else{//用户存在 进行执行chain.doFilter(req,resp);}}}}

当用户未注册时,会自动跳转到登录页面

比如访问localhost:9000/student?cmd=list会跳转到localhost:9000/login.jsp

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。