Home  >  Article  >  Java  >  Java-servlet-Analysis of examples of forwarding AND paths

Java-servlet-Analysis of examples of forwarding AND paths

黄舟
黄舟Original
2017-10-20 09:30:571607browse

The following editor will bring you an article about java-servlet-forwarding AND path (detailed explanation). The editor thinks it’s pretty good, so I’ll share it with you now and give it as a reference. Let’s follow the editor and take a look.

1. Forward:

a) What is forwarding?

One web component hands over the unfinished task to another web component to continue.

Usually a servlet obtains the data and transfers it to jsp for display.

Note: The web component should be servlet or jsp

b) How to forward?

1. Bind the data to the request object.

request.setatteribute(string name,obj)

Note: name binding name obj: binding value

obj request.getattr…(string name);//Get the binding value based on the binding name.

2. Get Forwarder

requestdispatatcher rd = request.getrequestdispatcher(string url);

Note: forwarding destination, such as a jsp

3. Forwarding

rd.forward(request, response)

Note: The essence of forwarding is that a web component notifies the container to call another web component (that is, calling the service method, so the request needs to be passed , response)

c) Features:

1. The forwarding destination is limited (requires the same web application A certain address)

2. After forwarding, the browser address remains unchanged.

d) Compare redirection and forwarding:

1. Each web component involved in forwarding can share request and response. Redirection cannot.

Note: request and response The survival time is not long (the server will create these two objects immediately after receiving the request, and destroy these two objects immediately when the response is sent). That is, the survival time of these two objects is the period between a request and a response.

2. After forwarding, the address in the browser address bar remains unchanged (requires an address in the same web application), there is no restriction on redirection and the address will change.

3. Path problem:

a) What is a path problem?

Links, form submissions, redirects and forwarding all need to fill in a path, such as

How to fill in this path /

b) Relative path

1. What is a relative path?

Path that does not start with "/".

c) Absolute path

1 .What is a relative path?

Path starting with "/".

2. How to write an absolute path?

Links, form submissions, redirects are written starting from the application name, forwarding is written starting from the application name.

zNote: Do not write the application name directly in the path, you should use request.getcontextpath() gets the application name.

The above is the detailed content of Java-servlet-Analysis of examples of forwarding AND paths. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn