Java Learning Note-httpservletresponse (23)

Source: Internet
Author: User

Get client Information

1 Writing Clientservlet.java

 Public voiddoget (httpservletrequest request, httpservletresponse response) throws Servletexception, IOException { Response.setcharacterencoding ("Utf-8"); Request.setcharacterencoding ("Utf-8"); Response.setcontenttype ("Text/html;charset=utf-8"); PrintWriter out=Response.getwriter ();//Get client Information out. println ("GetMethod ():"+request.getmethod () +"<br/>"); out. println ("Getrequesturl ():"+request.getrequesturl () +"<br/>"); out. println ("Getrequesturi ():"+request.getrequesturi () +"<br/>"); out. println ("Getprotocol ():"+request.getprotocol () +"<br/>"); out. println ("getquerystring ():"+request.getquerystring () +"<br/>"); out. println ("getremoteaddr ():"+REQUEST.GETREMOTEADDR () +"<br/>"); out. println ("getremotehost ():"+request.getremotehost () +"<br/>"); out. println ("Getremoteport ():"+request.getremoteport () +"<br/>"); out. println ("Getservletpath ():"+request.getservletpath () +"<br/>"); out. println ("Getcontextpath ():"+request.getcontextpath () +"<br/>");}

The above method can be found by using request to obtain the path of servlet and ServletContext. So in the actual development of the name of our site generally need to change. Therefore, if you use a Web site name in a future project, you must use Request.getcontextpath () to get it instead of writing to death. This can improve the maintenance of the later stage.

2 Getting server information

Write a Getserverinfo.java

 Public voiddoget (httpservletrequest request, httpservletresponse response) throws Servletexception, IOException { Response.setcharacterencoding ("Utf-8"); Request.setcharacterencoding ("Utf-8"); Response.setcontenttype ("Text/html;charset=utf-8"); PrintWriter out=Response.getwriter ();//Get client Information out. println ("getlocaladdr ():"+ request.getlocaladdr () +"<br/>"); out. println ("getlocalname ():"+ request.getlocalname () +"<br/>"); out. println ("Getlocalport ():"+ request.getlocalport () +"<br/>");}

3 Request Forwarding

    1. Access path

Http://localhost:8080/demo1?name=jack&password=root

Implement a demo1servlet that handles user requests

 Public voiddoget (httpservletrequest request, httpservletresponse response) throws Servletexception, IOException {//gets the parameters of the user's requestString name = Request.getparameter ("name"); String PSW= Request.getparameter ("Password"); //determine the information entered by the user        if(Name! =NULL&& PSW! =NULL&&"Jack". Equals (name)&&"Root". Equals (PSW)) {            //login successful need to pass the user information to the Welcome pageRequest.setattribute ("name", name); //Login successfully forwards the page to the Welcome pageRequest.getrequestdispatcher ("/demo2"). Forward (request, response); }Else{Request.getrequestdispatcher ("/regist.html"). Forward (request, response); }}

2. Write a forward-demo2servlet

 Public voiddoget (httpservletrequest request, httpservletresponse response) throws Servletexception, IOException {response . setcharacterencoding ("Utf-8"); Request.setcharacterencoding ("Utf-8"); Response.setcontenttype ("Text/html;charset=utf-8"); PrintWriter out=Response.getwriter (); //get the data forwarded overString name = (string) Request.getattribute ("name");  out. println ("Welcome to:"+name); }
The difference between forwarding and redirection
1 .    The implementation way is different from forwarding  request.getrequestdispatcher ("/regist.html"). Forward (Request, Response); redirect Response.sendredirect ("/day07/regist.html")2.    The number of requests is different forwarded only one request redirect sent 2 requests 3.    Address bar change different forwarding address bar unchanged redirect Address bar change 4. You can carry data through the request object when forwarding.
Path issues in the Web

If you need to use a path in a Web project, such as forwarding, redirection, and hyperlinks.

Principle: "All Web paths begin with/start"

then/the path must be a relative path, then/exactly which is the relative path to be wise.

If the path is for the server, then/ represents the root directory of the current Web site. If the path is for use by the browser, then /represents the current Tomcat WebApps directory.

Java Learning Note-httpservletresponse (*)

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.