java2db

The sendRedirect method (If the servlets are in different servers)



The sendRedirect method sends 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 method 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 it deployed in GlassFishserver.

The destination servlet (resultServlet.java) located in different web application WebApplicationTwo and it deployed in WebSphere (IBM) server.

 

The sendRedirect methodWe are calling resultServlet.java using it’s URL from web.xml of WebApplicationTwo.

 

 

The sendRedirect method WebApplicationOne directory structure :
The sendRedirect method

 

Deployment view :
The sendRedirect method

 

The sendRedirect method index.jsp

The sendRedirect method redirectServlet.java

In the above servlet ,The redirection URL is

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

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

We can also call JSP or HTML files with its absolute URLs.
The sendRedirect method web.xml

 

 

The sendRedirect method WebApplicationTwo directory structure :
The sendRedirect method

 

Deployment view :
Use of ServletConfig object in servlet with an example
The sendRedirect method resultServlet.java

The sendRedirect method web.xml

 

 

The sendRedirect method Execution Result :

Enter the name and click Redirect button .

The sendRedirect method

 

The browser redirects to the specified location URL .
The sendRedirect method




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]



^ <