Implement a fake login using HttpServlet

1. Create a web project in IDEA

2. Create the first page in index.jsp,

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>欢迎访问web网页</title>
  </head>
  <body>
  <h>欢迎访问web网页</h>
  <a href="login.jsp">去登录</a><br>
  <a href="zhuce.jsp">去注册</a><br>
  <a href="aa">千万别进!</a>
  </body>
</html>

3. Create a login.jsp and set up a login page

<head>
    <title>登录</title>
</head>
<body>
<form action="Login" method="post">
    账号:<input type="text" name="name" value=""><br>
    密码:<input type="password" name="pwd" value=""><br>
    <input type="submit" value="登录">
</form>
</body>

4. Create a zhuce.jsp, and create a registration page

<head>
    <title>注册</title>
</head>
<body>
<form action="zhuce" method="post">
    账号:<input type="text" name="user" value=""><br>
    密码:<input type="password" name="pwd" value=""><br>
    <input type="submit" value="注册">
</form>
</body>

5. Complete the mapping of login and registration in web.xml

 <!--    配置servlet类;-->
    <servlet>
        <!--        起别名-->
        <servlet-name>login</servlet-name>
        <!--        servlet类所在位置-->
        <servlet-class>Login</servlet-class>
    </servlet>
    <!--    servlet类的映射-->
    <servlet-mapping>
        <servlet-name>login</servlet-name>
        <url-pattern>/Login</url-pattern>
    </servlet-mapping>
    <!--    配置servlet类;-->
    <servlet>
        <!--        起别名-->
        <servlet-name>zhuce</servlet-name>
        <!--        servlet类所在位置-->
        <servlet-class>Login</servlet-class>
    </servlet>
    <!--    servlet类的映射-->
    <servlet-mapping>
        <servlet-name>zhuce</servlet-name>
        <url-pattern>/zhuce</url-pattern>

    </servlet-mapping>

6. Use HttpServlet in Login.java to complete fake login

public class Login extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("Login-get...");
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        String requestURI = req.getRequestURI();
//        String contextPath = req.getContextPath();
//        String servletPath = req.getServletPath();
//
//        System.out.println(servletPath);
//        System.out.println(contextPath);
//        System.out.println(requestURI);


        req.setCharacterEncoding("utf-8");
        String parameter = req.getParameter("name");
        String pwd = req.getParameter("pwd");
        System.out.println(parameter);
        System.out.println(pwd);

        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charest=UTF-8");

        if (pwd.equals("666")&&parameter.equals("张三")){
            //登陆成功
            resp.getWriter().write("登录成功");
        }else {
            //登陆失败
            resp.getWriter().write("登录失败");
        }
        System.out.println("Login-poat...");
    }

}

Guess you like

Origin blog.csdn.net/AMYCX/article/details/129032578