servlet項目demo

頁面邏輯html

  頁面:首頁、登陸頁面、登陸成功頁面java

  servlet:登陸服務成功跳到登陸成功頁面api

  filter:判斷是否登陸。無登陸信息跳轉到登陸頁面session

登陸&首頁ide

<!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>

登陸成功頁面post

<!--登陸成功-->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body> success </body>
</html>

loginfilter.javaurl

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.javaspa

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 { } }

代碼結構code

依賴包:orm

<dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>4.0.1</version>
        <scope>provided</scope>
    </dependency>
相關文章
相關標籤/搜索