Statement : All my articles , All of them are the arrangement of online teaching videos , Including the madness saying 、 Silicon Valley 、 Black horse programmers and so on , For reference , No commercial use , Please refer to , No joy, no spray. , thank you .( Be careful , Because of the website , Some code characters may have problems , When you read the code , It's better to look at the picture below )
One 、HttpServletRequest
HttpServletRequest Requests on behalf of clients , User pass Http Protocol access server ,HTTP All information in the request will be encapsulated in HttpServletRequest, Through this HttpServletRequest Methods , We can get all the information from the client . Including getting the parameters passed by the front end , Request forwarding, etc .
Two 、 Get the parameters passed by the front end
There are four ways to get parameters :
But the first and fourth ones we use most often , The other two are not commonly used , There is no explanation here , You can find the information yourself to understand and master .
3、 ... and 、 Case study
1、 On the basis of the previous , modify index.jsp
<body>
<h2> The user login </h2>
<%-- The path to submit here , Need to find the path to the project --%>
<%-- ${pageContext.request.contextPath} Represents the current project path --%>
<form action="${pageContext.request.contextPath}/login01" method="post">
user name :<input type="text" name="username"><br>
password :<input type="password" name="password"><br>
hobby :
<input type="checkbox" name="hobbys" value=" Sing a song "> Sing a song
<input type="checkbox" name="hobbys" value=" dance "> dance
<input type="checkbox" name="hobbys" value=" Basketball "> Basketball
<input type="checkbox" name="hobbys" value=" football "> football
<br>
<input type="submit" value=" Submit ">
</form>
</body>
2、 Build a success.jsp
The code is as follows :
3、 Build a LoginServlet Class file , The code is as follows
package com.kuang.servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
@Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); }
}
modify web.xml:
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>com.kuang.servlet.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/login01</url-pattern>
</servlet-mapping>
4、 start-up tomcat
Console output :
By running the results , We can sum it up by ourselves getParameter() and getParameterValues() The function of these two methods .