Path issues for forwarding and redirection in the servlet and form submission path issues

Source: Internet
Author: User

the type of request forwarding and response redirection

There are two ways to get the Servlet forwarding object (RequestDispatcher): One is obtained through the HttpServletRequest Getrequestdispatcher () method, One is obtained by means of the ServletContext Getrequestdispatcher () method.

There is only one method of servlet redirection: The HttpServletResponse sendredirect () method.

The arguments for these three methods are a string in the form of a URL, but there is a difference between using a relative path or an absolute path.

difference of path parameters in request forwarding and response redirection


Assume that the servlet to which this method belongs is reached through a http://localhost/myApp/cool/bar.do request.

1. Response redirection httpservletresponse.sendredirect (String)

Parameters can be specified as relative paths, absolute paths, or other Web applications.

I: relative path :response.sendredirect ("foo/stuff.do"), the container is relative to the original request URL of the directory plus parameters to generate a complete url--http:// Localhost/myapp/cool/foo/stuff.do.

II: Absolute path :response.sendredirect ("/foo/stuff.do"), the container establishes the complete URL relative to the Web application itself, because REDIRECT Response.sendredirect ("") is the server sends a request header information to the client, the client requests again the server, the request is carried out outside the server, namely the complete URL is-http://localhost/foo/stuff.do.

III: Other Web Applications : Response.sendredirect ("http://www.xxx.com") containers are directed directly to the URL.

2. Request Forwarding Httpservletrequest.getrequestdispatcher (String)

Parameter can be specified as a relative or absolute path.

I: The full URL generated for the relative path is the same as the redirection method.

II: The absolute path is different from servlet redirection , and the container generates a complete ur L (that is, the "/" root path is relative to the virtual path ) relative to the Web application's root directorybecause the forwarding is done inside the server. Write absolute path/start refers to the current Web application

。 That

The URL generated by request.getrequestdispatcher ("/foo/stuff.do") is http://localhost/myApp/foo/stuff.do.

3, Servletcontext.getrequestdispatcher (String)

Parameter can only be specified as an absolute path and the complete URL generated is the same as Httpservletrequest.getrequestdispatcher (String) .


##################################

Similarly:
JSP Submit form to Servlet path problem When the JSP page submits the form to the servlet, the path should be written with particular attention.

For example, the following servlet is registered in Web.xml:

<servlet>
<servlet-name>addStudent</servlet-name>
<servlet-class>org.mytest.addStudent</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>addStudent</servlet-name>
<url-pattern>/servlet/addStudent</url-pattern>
</servlet-mapping>

If you say that your project name is hibernateapp3,jsp page Submission form to the servlet, there are two ways to write:

1. relative path: <formaction=servlet/addstudent method=post>...</form>

2. absolute path: <formaction= "/hibernateapp3/servlet/addstudent" method=post>...</form>

or <formaction= "<%=request.getcontextpath ()%>/servlet/addstudent" method=post>...</form>

Note:/Represents the root directory, if the path is to use/start, Tomcat is webApp that directory, if you are not/start representing you from the current project directory, for example: webapp/hibernateapp3/

This is important, and many of the errors that occur when submitting a form are caused by an error in the commit path.

The attached, <ahref> path if "/" is the beginning, it means relative to the host , if not the current request. The most crucial thing to know is whether the request to request the destination resource is within the server or outside the server: internal, "/" is the virtual directory of the project, when external, "/" is the root directory representing the host

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.