The difference between 10--request introduction and case analysis, request redirection and request forwarding

Source: Internet
Author: User

1.HttpServletRequest

The HttpServletRequest object represents the client's request , and when the client accesses the server through the HTTP protocol, all the information in the HTTP request header is encapsulated in the object, and the developer can get the customer's information through the object's method.

2.request Common methods

L Get client information

• The Getrequesturl method returns the full URL when the client makes a request.

• The Getrequesturi method returns the resource name portion of the request line .

• The getquerystring method returns the parameter portion of the request line .

Getremoteaddr method returns the IP address of the client that made the request

• The Getremotehost method Returns the full host name of the client that made the request

Getremoteport Method Returns the network port number used by the client

• The Getlocaladdr method returns the IP address of the Web server .

Getlocalname method returns the host name of the Web server

GetMethod Get client request method

Example:

Package com.request;

Import java.io.IOException;

Import javax.servlet.ServletException;

Import Javax.servlet.http.HttpServlet;

Import Javax.servlet.http.HttpServletRequest;

Import Javax.servlet.http.HttpServletResponse;

Various ways to test the request

public class Request1 extends HttpServlet {

Public Voiddoget (HttpServletRequest request, httpservletresponse response)

Throws Servletexception, IOException {

System.out.println (Request.getlocaladdr ());

System.out.println (Request.getlocalname ());

System.out.println (Request.getlocalport ());

System.out.println (Request.getlocale ());

System.out.println (Request.getrequesturi ());

System.out.println (Request.getrequesturl ());

System.out.println (Request.getquerystring ());

System.out.println (Request.getremoteaddr ());

System.out.println (Request.getremotehost ());

System.out.println (Request.getremoteport ());

System.out.println (Request.getremoteuser ());

/**

* Output results are as follows: 0.0.0.0

* 0.0.0.0

* 8080

* ZH_CN

*/webdemo/request1

* Http://localhost:8080/WebDemo/Request1

* Name=join the GetQueryString () method here, add a property value to the browser's address bar (. Name=join) To output this content, Name=join can be arbitrary value

* 0:0:0:0:0:0:0:1

* 0:0:0:0:0:0:0:1

* 61264

* NULL

* */

}

Public Voiddopost (HttpServletRequest request, httpservletresponse response)

Throwsservletexception, IOException {

Doget (Request,response);

}

}

L Get client request headers

GetHead (name) method

getheaders (String name) method, which returns an enumeration value

Getheadernames method that returns an enumeration value

Example:

Packagecom.request;

Importjava.io.IOException;

Importjava.util.Enumeration;

Importjavax.servlet.ServletException;

Importjavax.servlet.http.HttpServlet;

Importjavax.servlet.http.HttpServletRequest;

Importjavax.servlet.http.HttpServletResponse;

Publicclass Request2 extends HttpServlet {

public void doget (httpservletrequest request,httpservletresponse response)

Throws Servletexception, IOException {

Test2 (Request);

}

The extraction method must not be less than the parentheses behind, or the extraction is unsuccessful

public void Test2 (HttpServletRequest request) {

Enumeration str =request.getheadernames ();

while (Str.hasmoreelements ()) {

String name = (string) str.nextelement ();

String value = Request.getheader (name);

SYSTEM.OUT.PRINTLN (name + "----" + value);

/* Output Result: Accept----text/html,application/xhtml+xml, *

*accept-language----zh-cnuser-agent----mozilla/5.0 (compatible;

*msie 9.0; Windows NT 6.1; Win64; x64; trident/5.0)

*UA-CPU----AMD64 accept-encoding----gzip, deflate

*host----localhost:8080 Connection----keep-alive

*/

}

}

public void Test1 (HttpServletRequest request) {

String value =request.getheader ("Accept");

System.out.println (value);

Output results: text/html, Application/xhtml+xml, */*

}

public void DoPost (Httpservletrequestrequest, httpservletresponse response)

Throws Servletexception, IOException {

Doget (request, response);

}

}

L obtain client request parameters (data submitted by client)

getparameter (name): Gets the parameter value of the specified name. This is one of the most common methods.

getparametervalues (String name): Gets an array of all the values for the specified name parameter. It applies to situations where a parameter name corresponds to multiple values. such as a check box in a page form, a value submitted by a multiple-selection list.

getparameternames (): Returns a enumeration object that contains all the parameter names in the request message. By traversing the enumeration object, you can get all the parameter names in the request message.

Getparametermap (): Returns a Map object that holds all parameter names and values in the request message. The key of the map object is the parameter name of the string type, and value is an array of values of the type object of this parameter.

3.request Common applications

L access to various form entry data

text, password, radio, checkbox, File, select, textarea, hidden, image, button for JS programming

L Request Parameter's Chinese garbled problem

L Code of URL address

L anti-theft chain

Request Forwarding: Request forwarding means that when a Web resource receives a client request, it notifies the server to invoke another Web resource for processing

Note: The following blog will explain in detail

4. The difference between request redirection and request forwarding

L A Web resource receives a client request and notifies the server to call another Web resource for processing, called a request forwarding. (like the analogy we said earlier: Help borrow money)

L A Web resource receives a client request and notifies the browser to access another Web resource, called a request redirection (tell him who to borrow from)

The L Requestdispatcher.forward method can only forward requests to components in the same Web application ; httpservletresponse.sendredirect Method can also be redirected to a resource in another application on the same site, or even to a resource that is redirected to another site using an absolute URL .

L If the relative URL passed to the Httpservletresponse.sendredirect method starts with "/", it is relative to the root of the entire Web site, or if the relative URL specified when creating the RequestDispatcher object starts with "/", It is relative to the current Web application's root directory.

L Call HttpServletResponse. after the Sendredirect method redirects the Access procedure, the URL displayed in the browser address bar will change from the initial URL address to the redirected target URL; requestdispatcherforward The browser address bar retains its original URL address after the request forwarding process for the method ends .

L The Httpservletresponse.sendredirect method responds directly to the browser's request, and the result of the response is to tell the browser to issue an access request to another URL; The Requestdispatcher.forward method forwards the request within the server side to another A resource in which the browser only knows that a request has been made and the result of the response is not known to have a forwarding behavior within the server program.

L The caller of the Requestdispatcher.forward method shares the same request object and the response object as the callee, which belongs to the same access request and response process, and the Httpservletresponse.sendredirect method caller and the Callers use their own request objects and response objects, which belong to two separate access request and response procedures.

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.