Request domain object and demand forwarding (14)

Source: Internet
Author: User

The difference between redirection and forwarding:


(1) Request is a domain object

The request object is also a region object that stores data, so it also has the following methods:

SetAttribute (String name,object o)
GetAttribute (String name)
RemoveAttribute (String name)

Note: The scope of the request domain: one request (2) request to complete the forwarding of requests

1. Get the Request forwarder----path is the forwarding address
Requestdispatcher.getrequestdispatcher (String Path)
2. Forwarding by forwarder Object
Requestdispathcer.forward (servletrequest request,servletresponse response)
As above, forwarding is divided into two steps. The first step is to get a requestdispatcher and then call its forward method.
Example:

Package Com.ken.forward;

Import java.io.IOException;

Import Javax.servlet.RequestDispatcher;
Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;

public class Servlet1 extends HttpServlet {
	private static final long serialversionuid = 1L;

	protected void doget (HttpServletRequest request, httpservletresponse response)
			throws Servletexception, IOException {
		///SERVLET1 forwarded the request to Servlet2
		RequestDispatcher dispatcher = Request.getrequestdispatcher ("/ Servlet2 ");
		method to perform forwarding
		Dispatcher.forward (request, response);
	}

	protected void DoPost (HttpServletRequest request, httpservletresponse response)
			throws Servletexception, IOException {
		doget (request, response);
	}
}
Operation Result:


The address of the address bar has not changed, the content returned is actually returned by Servlet2. This is forwarding.

Understand the forwarding, we are speaking of the request domain.

Request and response each visit is new. Therefore, their scope is within a single request. To store data in the request field

Package Com.ken.forward;

Import java.io.IOException;

Import Javax.servlet.RequestDispatcher;
Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;

public class Servlet1 extends HttpServlet {
	private static final long serialversionuid = 1L;

	protected void doget (HttpServletRequest request, httpservletresponse response)
			throws Servletexception, IOException {

		//Request.setattribute data to the request domain
		("name", "Tom");

		Servlet1 forwarded the request to Servlet2
		RequestDispatcher dispatcher = Request.getrequestdispatcher ("/servlet2");
		method to perform forwarding
		Dispatcher.forward (request, response);
	}

	protected void DoPost (HttpServletRequest request, httpservletresponse response)
			throws Servletexception, IOException {
		doget (request, response);
	}
}
Package Com.ken.forward;

Import java.io.IOException;
Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;

public class Servlet2 extends HttpServlet {
	private static final long serialversionuid = 1L;

	protected void doget (HttpServletRequest request, httpservletresponse response)
			throws Servletexception, IOException {

		//Fetch data from the request domain
		String name = (string) request.getattribute ("name");

		Response.getwriter (). Write ("Hello world ..." + name);
	}

	protected void DoPost (HttpServletRequest request, httpservletresponse response)
			throws Servletexception, IOException {
		doget (request, response);
	}
}
Operating Effect:


Note: The ServletContext domain is compared to the request domain's life cycle.
ServletContext:
Create: Server Startup
Destroyed: Server shutdown
Domain scope: The entire Web application
Request
Create: Create request upon access
Destroy: Response ends request destroy
Domain scope: In one request

Note: the Forwarding and redirection
1) redirect two requests, forward one time
2) redirect Address change of address bar, forwarding address unchanged
3) redirects can access external Web sites, forwarding only access to internal
4) Forwarding performance is better than redirection

Note: The client address and the server-side address
Client:
Is the address of the client to access the server, the address outside the server, features: Write on the Web application name
Enter address directly:
redirect
Server-side address:
Server internal resource jump address, features: Do not need to write the name of the Web App
Forward

Request Summary:
Request to get the contents of the row
Request.getmethod ()
Request.getrequesturi ()
Request.getrequesturl ()
Request.getcontextpath ()
REQUEST.GETREMOTEADDR ()
Request to get the contents of the header
Request.getheader (name)
Request acquisition Body (requested parameters)
String Request.getparameter (name)
Map<string,string[]>request.getparametermap ();
String[] Request.getparametervalues (name);
Note: The parameters sent by the client are string to the server side
Get the Chinese garbled solution:
Post
Request.setcharacterencoding ("UTF-8");
Get
parameter= New String (Parameter.getbytes ("iso8859-1"), "UTF-8");
Request forwarding and Domain
Request.getrequestdispatcher (forwarding address). Forward (REQ,RESP);
Request.setattribute (Name,value)
Request.getattribute (name)

SOURCE download

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.