Servlet 登录
(过滤器简单示例),简单演示过滤器,并未处理直接使用地址访问的情况
1.目标功能
- 用户登录后分别授予不同目录资源的访问权限
2.设计
3.架构
-
项目类型:Java Web
-
目录结构:
4.编码
4.1 后台
4.1.1 模块代码
-
loginServlet.java
package com.z.servlet; import com.z.domain.User; 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; /** * @ClassNameloginServlet * @Description * @Author ANGLE0 * @Date2019/12/11 18:54 * @Version V1.0 **/ @WebServlet("/login") public class loginServlet extends HttpServlet { @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException { String name = req.getParameter("username"); String passwd = req.getParameter("pass"); System.out.println(name+ passwd); User u = new User(name, passwd); String to = null;//前往的地址 if (u.getName() != null && u.getPassword() != null) { if(u.getName().equals("aaa") && u.getPassword().equals("aaa")){ u.setPermission("A"); to = "/list.jsp"; req.getSession().setAttribute("user", u.getName()); req.getSession().setAttribute("role", u