HttpServletRequest for browser objects

Source: Internet
Author: User

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"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.