java2db

Example for sendRedirect (Both servlets are in same web application)



The sendRedirect method is used to send a temporary redirect response to the client with  specified redirect location URL.The client collect the destination URL and generates request to the destination program.

 

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

 

Example for sendRedirect Syntax :

ServletResponseObject . sendRedirect(“<URL>“);

 

Example for sendRedirect Example for sendRedirect method :

In the below example , both calling servlet (redirectServlet.java) and called JSP (result.jsp)  are in the same web application.

 

create a servlet We can also call a servlet from sendRedirect method by using it’s URL .

 

Directory structure :
Use of ServletConfig object in servlet with an example

 

Example for sendRedirect index.jsp

Example for sendRedirect redirectServlet.java

In the above servlet ,The redirection  URL is

http://localhost:8080/redirectionInsideWebApp/result.jsp?userName=”+name+”

http : Hypertext transfer protocol.
localhost : Ip address (127.0.0.1 also indicates the localhost).
8080 : Server port.
redirectionInsideWebApp : Web application name.
result.jsp : Called JSP file.
userName : Parameter name.
name : Parameter value.

 

Example for sendRedirect web.xml

Example for sendRedirect result.jsp

 

Example for sendRedirect Execution Result:

Enter the name and click Redirect button.
Example for sendRedirect

The response redirects to the specified URL from the browser window (Client).
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]



^ <