How to implement the user login function and submit the form?

Hello everyone, I'm Captain Hai Dao.

If you let everyone write the front page of a form, maybe everyone can write it perfectly, but we also have to submit the form to the backend, so how to realize the submission of the frontend form? The following uses java code to achieve the form submission effect.

    First write a simple form form, the example is as follows:

    

<a href="/servletday/PDayA?name=NAME&password=PASSWORD">
      Click to test whether to connect to the background
</a>
<form action="/servletday/PDayA" method="post">
    用户名:<input type="text" name="username"/>
    密码:<input type="password" name="password">
    爱好<input type="checkbox" name="hobby" value="cf"/>吃饭
    <input type="checkbox" name="hobby" value="sj"/>睡觉
    <input type="checkbox" name="hobby" value="pb"/>跑步
    <br/>
   <input type="submit" value="提交"/>
</form>

    Where /servletday is the project name, and PDayA is the name of the java file in the background.

      The first a tag is to test whether to connect to the PDayA file.

    Then start to write the background:

    

public class PDayA extends HttpServlet {
         //First write the doGet method of the class to test the connection of the a tag.
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("GET :" + request.getParameter("name"));
		System.out.println("GET :" + request.getParameter("password"));
	}
        //After the form is submitted, the doPost method used
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //The first method: first obtain the data in the request field and define variables, and then output.
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String[] hobby = request.getParameterValues("hobby");
        System.out.println(username + ", "+password + ", " + Arrays.toString(hobby));

        //The second method is implemented with an interface
        Enumeration names = request.getParameterNames();
	while(names.hasMoreElements()) {
	    System.out.println(names.nextElement());
	}

        //The third is implemented with Map, the key and value need to be paid attention to
        Map<String,String[]> maps = request.getParameterMaps();
        for(String name : maps.ketSet()){
            String[] value = maps.get(name){
                  System.out.println(name + "=" + Arrays.toString(value));
            }
        }
    }
}

    In this way, the function of form submission is realized.

    If you want to know more exciting content, please pay attention to the WeChat public account: Hai Dao Ship

         

    Captain Hai Dao will update the content from time to time.


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325880041&siteId=291194637