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 ( resides in WebApplicationOne  and the destination servlet ( located in different web application WebApplicationTwo. But the two web applications are deployed in same server.

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


Directory structure :

The sendRedirect

The sendRedirect WebApplicationOne  :

The sendRedirect index.jsp

The sendRedirect

In the above servlet ,The redirection URL is


http : Hypertext transfer protocol.
localhost : Ip address ( 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

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 :
AES Password Encryption in javascript and decryption in java – servlet
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)
   [will not be published]

^ <