Objects can be transmitted between different Web components during the processing of a request.
Validity Period: the validity period of a request.
Test HttpServletRequest:
1. Create a web project.
2. Create a new package named com. neusoft. httpServletRequest under the src directory.
3. Create a servlet named HttpServletRequestTest under this package. The content of HttpServletRequestTest is:
------------------------------------
Package com. neusoft. httpServletRequest;
Import java. io. IOException;
Import java. io. PrintWriter;
Import javax. servlet. ServletException;
Import javax. servlet. http. HttpServlet;
Import javax. servlet. http. HttpServletRequest;
Import javax. servlet. http. HttpServletResponse;
Public class HttpServletRequestTest extends HttpServlet {
Public void doGet (HttpServletRequest request, HttpServletResponse response)
Throws ServletException, IOException {
This. doPost (request, response );
}
Public void doPost (HttpServletRequest request, HttpServletResponse response)
Throws ServletException, IOException {
Response. setContentType ("text/html; charset = gb2312 ");
PrintWriter out = response. getWriter ();
String userName = request. getParameter ("userName"); // obtain userName from the request object
String userPassWord = request. getParameter ("userPassWord"); // obtain userPassWord from the request object
Out. println ("userName:" + userName + "<br>" + "userPassWord:" + userPassWord );
Out. flush ();
Out. close ();
}
}
------------------------------------
The content of the form to be submitted to index. jsp processed by the HttpServletRequestTest class is:
------------------------------------
<Form action = "/Test/servlet/HttpServletRequestTest" method = "post">
<Label> name: </label>
<Input type = "text" name = "userName"> <br>
<Label> password: </label>
<Input type = "password" name = "userPassWord"> <br>
<Input type = "submit" value = "OK">
<Input type = "reset" value = "reset">
------------------------------------
The servlet configuration information in web. xml is as follows:
------------------------------------
<Servlet>
<Servlet-name> HttpServletRequestTest
</Servlet-name>
<Servlet-class> com. neusoft. httpServletRequest. HttpServletRequestTest
</Servlet-class>
</Servlet>
<Servlet-mapping>
<Servlet-name> HttpServletRequestTest </servlet-name>
<Url-pattern>/servlet/HttpServletRequestTest </url-pattern>
</Servlet-mapping>
------------------------------------
4. re-release the project, enter http: // localhost: 8080/Test/in the browser, enter the information, and click OK to submit the request to HttpServletRequestTest for processing, httpServletRequestTest obtains the information you entered from the request object and displays it on the page, as shown below:
UserName: wjl
UserPassWord: 123
The above information can be output only when index. jsp makes a request for it, the above information cannot be output in other cases.
Author: "extreme scholar"