Use HttpServlet to complete a fake login

1. HttpServlet

1, Concept:HttpServlet is an implementation class ofServlet interface, and it is an abstraction Class, the HttpServlet class using the HTTP communication protocol (a stateless protocol) is defined in the servlet.http package.

The process of Servlet processing HTTP requests is as follows:

2. Response process

1. The Web client issues an Http request to the Servlet container
2. The Servlet container parses the Web client's Http request
3. The Servlet container creates an HttpRequest Object, encapsulate Http request information in this object
4. The Servlet container creates an HttpResponse object to respond to the request information
5. The Servlet container calls the service method of HttpServlet, Pass the HttpRequest and HttpResponse objects to the HttpServlet object as parameters of the service method
6. HttpServlet calls the relevant methods of HttpRequest to obtain HTTP request information
7.HttpServlet calls HttpResponse Relevant methods to generate response data
8. The Servlet container passes the response result of HttpServlet to the Web client
HttpServlet must first read the content of the Http request. Servlet The container is responsible for creating the HttpServlet object and encapsulating the Http request directly into the HttpServlet object.

3. Steps to create HttpServlet
1. Inherit the HttpServlet abstract class and create two classes

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

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("Login-post...");
        //前端请求(request),后端处理后,最后给前端做出响应(response)

        //获取请求的地址相关
        String uri = request.getRequestURI();
        String contextPath = request.getContextPath();
        String servletPath = request.getServletPath();

        System.out.println(uri); //包含项目名和资源路径
        System.out.println(contextPath);//项目名称
        System.out.println(servletPath);//请求的路径

//        String remoteAddr = request.getRemoteAddr();
//        String remoteHost = request.getRemoteHost();
//
//        System.out.println(remoteAddr);
//        System.out.println(remoteAddr);
        System.out.println("-------------------");
        //1,从请求中获取用户提交的参数(数据)
        request.setCharacterEncoding("utf-8");//设置请求的编码格式为中文
        String user = request.getParameter("user");//根据表单的name属性获取用户输入的值
        String pwd = request.getParameter("pwd");

        System.out.println(user);
        System.out.println(pwd);

        //2.根据用户提交的用户名和密码,去数据库执行查询

        //3.判断成功要干什么?判断失败要干什么? 做出响应
        response.setCharacterEncoding("utf-8");//设置响应的编码格式
        response.setContentType("text/html;charset=UTF-8");//以什么样的格式(文本/网页)响应
        if (user.equals("张三") && pwd.equals("666")) {
            //登录成功
            response.getWriter().write("登录成功");
        }else {
            //登录失败
            response.getWriter().write("登录失败");
        }

    }
}

 2. Rewrite some methods of HttpServlet, such as doGet() or doPost() method

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

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("Login-post...");
    }
}

 Write some code in web.xml

 <!-- 配置servlet类 -->
    <servlet>
        <!-- 起别名 -->
        <servlet-name>Login</servlet-name>
        <!-- servlet类所在的位置:类的全类名就是 包名.类名 -->
        <servlet-class>com.gao.servlet.Login</servlet-class>
    </servlet>
    <!-- Servlet类的映射:Servlet用来处理哪个请求 -->
    <servlet-mapping>
        <servlet-name>Login</servlet-name>
        <url-pattern>/Login</url-pattern>
    </servlet-mapping>


    <servlet>
        <!-- 起别名 -->
        <servlet-name>denglu</servlet-name>
        <!-- servlet类所在的位置:类的全类名就是 包名.类名 -->
        <servlet-class>com.gao.servlet.Login</servlet-class>
    </servlet>
    <!-- Servlet类的映射:Servlet用来处理哪个请求 -->
    <servlet-mapping>
        <servlet-name>denglu</servlet-name>
        <url-pattern>/denglu</url-pattern>
    </servlet-mapping>


    <servlet>
        <servlet-name>zhuce</servlet-name>
        <servlet-class>com.gao.servlet.Zhuce</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>zhuce</servlet-name>
        <url-pattern>/zhuce</url-pattern>
    </servlet-mapping>
</web-app>

3. Get HTTP request information. Retrieve the data submitted by the HTML form or the query string on the URL through the HttpServletRequest object

<html>
<head>
    <title>这是第一个Web项目</title>
</head>
  <body>
  <h2>欢迎使用 Java Web</h2>
  <a href="login.jsp">登录</a>
  <a href="zhuce.jsp">注册</a>
  </body>
</html>

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

<html>
<head>
    <title>注册</title>
</head>
<body>
      <h2>注册</h2>
      <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>
</html>

It can be seen from the code that we use the form form to enter our data parameters and requests. Some of the more important attributes are: action="login": action indicates which page the content in the current form is submitted to. Processing, for our code, is to submit it to the login page for processing.
The attribute values ​​of input are type="text" and type="password": type="text" represents a single-line text input box and type="password" represents a password input box. , the biggest difference between the password input box and the text input box is that the password input box will not display the entered characters in plain text, but will be replaced by "•", which enhances the security of the data.
type="submit": The submit button submits the information in the form to the file pointed to by the action in the form attribute, that is, submitted to "login" and passed to the server.

Run it and the result is:

4. Generate HTTP response results. Generate response results through the HttpServletResponse object.
 

 

 

Guess you like

Origin blog.csdn.net/weixin_69036336/article/details/129031984