java2db

The sendRedirect (servlets are in different web applications)



The sendRedirect method is used to send a temporary response to the client (Ex :Browser) with specified redirect URL.The client collects the destination URL and generate request to the destination program.

 

If the response has already been committed, the sendRedirect method throws an IllegalStateException.

 

The sendRedirect Syntax :

ServletResponseObject . sendRedirect(“URL“);

 

Example for sendRedirect Example for the sendRedirect method :

In the below example , the source servlet (redirectServlet.java) resides in WebApplicationOne  and the destination servlet (resultServlet.java) located in different web application WebApplicationTwo. But the two web applications are deployed in same server.

create a servletWe are calling resultServlet.java using it’s URL from web.xml .

 

Directory structure :

The sendRedirect

The sendRedirect WebApplicationOne  :

The sendRedirect index.jsp

The sendRedirect redirectServlet.java

In the above servlet ,The redirection URL is

http://localhost:8080/WebApplicationTwo/redirect?userName=”+name+”

http : Hypertext transfer protocol.
localhost : Ip address (127.0.0.1 also indicates the localhost).
8080 : Server port (Same server).
WebApplicationTwo : Web application name.
redirect : URL pattern from web.xml.
userName : Parameter name.
name : Parameter value.
The sendRedirect web.xml

Example for sendRedirect WebApplicationTwo:

The sendRedirect resultServlet.java

The sendRedirect web.xml

 

Deployment  view:
Use of ServletConfig object in servlet with an example

 

Execution Result :

Enter the name and click Redirect button .
Use of ServletConfig object in servlet with an example

The browser will redirect to WebApplicationTwo  using the redirection URL.
Use of ServletConfig object in servlet with an example




Related Posts :
Kill or invalidate session using session id ( concurrent login )
Servlet Filter ( Request and Response filters in servlets ) example
URL rewriting in servlets ( Servlet session tracking )
Servlet Cookies ( Servlets session tracking with cookie )
Hidden form field in servlets ( Servlet session tracking )
Request attributes in servlet with an example ( Attribute scope )
Session attributes in servlet with example (Attribute scope)
ServletConext Attributes in servlet ( Attribute scope )
   [will not be published]



^ <