servlet项目demo
页面逻辑
页面:首页、登录页面、登录成功页面
servlet:登录服务成功跳到登录成功页面
filter:判断是否登录。无登录信息跳转到登录页面
登录&首页
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <form action="/loginServlet" method="post"> 请输入账号<input type="text" name="username"><br> 请输入密码<input type="text" name="password"><br> <input type="submit"> </form> </body> </html>
登录成功页面
<!--登录成功--> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> success </body> </html>
loginfilter.java
package filter; 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; @WebFilter(filterName = "Loginfilter", urlPatterns = {"/*"}) public class loginfilter implements Filter { public void destroy() { } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException { HttpServletRequest reqs = (HttpServletRequest) request; HttpServletResponse res = (HttpServletResponse) response; System.out.println(" req.getRequestURI() " + reqs.getRequestURI()); String requestURI = reqs.getRequestURI(); // 获取请求地址 // String requestURI = req.getRequestURI().substring(req.getRequestURI().indexOf("/", 1), // req.getRequestURI().length()); // 登陆页面和接口放开限制,否则死循环 if ("/login.html".equals(requestURI) || "/loginServlet".equals(requestURI)) { chain.doFilter(reqs, res); } else { // 默认不开启 HttpSession session = reqs.getSession(false); // 如果session中没有用户信息,拦截返回登陆页面 if (session == null || session.getAttribute("username") == null) { System.out.println(session); res.sendRedirect(reqs.getContextPath() + "/login.html"); // 返回 return; } else { System.out.println(session); chain.doFilter(reqs, res); } } } public void init(FilterConfig config) throws ServletException { } }
loginServlet.java
package 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; @WebServlet(name = "loginServlet",urlPatterns = "/loginServlet") public class loginServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); String username=request.getParameter("username"); String password=request.getParameter("password"); System.out.println(username); // if(username=="admin" && password=="123"){ System.out.println(password); HttpSession session = request.getSession();//获取session session.setAttribute("username",username); session.setAttribute("password",password); response.sendRedirect(request.getContextPath()+"/success.html"); // } } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } }
代码结构
依赖包:
<dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.1</version> <scope>provided</scope> </dependency>