Home > Java > javaTutorial > body text

How to forward Servlet

(*-*)浩
Release: 2020-09-14 15:10:10
Original
5814 people have browsed it

Servlet forwarding steps: first bind the data to the request object; then obtain the forwarder; finally, in the forwarded destination component, obtain the binding value based on the binding name, if the corresponding value does not exist , just return null.

How to forward Servlet

Forwarding is when a web component (servlet/jsp) passes the unfinished processing to another web component through the container for further completion.

Generally, after a servlet obtains the data, it forwards it to a jsp, and the jsp generates the corresponding page based on the data.

Recommended course: Java Tutorial.

How does Servlet forward?

step1. Bind the data to the request object.

request.setAttribute(String name,Object obj);
Copy after login

For example:

request.setAttribute("emplist",emplist);
Copy after login

step2. Obtain the forwarder

RequestDispatcher rd = request.getRequestDispatcher(String uri);
Copy after login

uri: It is the destination component to be forwarded

For example:

RequestDispatcher rd = request.getRequestDispatcher("empList3.jsp");
Copy after login

step3. Forwarding

rd.forward(request,response);
Copy after login

In the forwarded destination component, the binding value can be obtained based on the binding name. If the corresponding value is not exists, returns null.

Object request.getAttribute(String name);
Copy after login

Problems that need attention when forwarding

Out.close and out.flush cannot be called before forwarding.

Before forwarding, the container will clear the cached data on the response object.

Forwarding features

After forwarding, the address in the browser address bar remains unchanged.

The forwarding destination can only be the address of a component within the same application.

The above is the detailed content of How to forward Servlet. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!