Hey there! any question in your mind? Ask It Now!.

Popular Categories

Redirect URL in Servlet

+8 votes
asked in Programming by mayank (250 points)
i am using a servlet by jsp i am calling my main and want to Redirect URL in then end of program.

2 Answers

+1 vote
answered by mannu (990 points)
selected by mayank
Best answer
you can redirect your url like this by servlet request
protected void  doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}catch(Throwable e){


0 votes
answered by amit_pammu Expert (5,030 points)


The simplest way of redirecting a request to another page is using method sendRedirect() of response object. Following is the signature of this method:

public void HttpServletResponse.sendRedirect(String location)
throws IOException 

This method sends back the response to the browser along with the status code and new page location. You can also use setStatus() and setHeader() methods together to achieve the same:

String site = "http://www.newpage.com" ;
response.setHeader("Location", site); 


This example shows how a servlet performs page redirection to another location:

import java.io.*;
import java.sql.Date;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class PageRedirect extends HttpServlet{
  public void doGet(HttpServletRequest request,
                    HttpServletResponse response)
            throws ServletException, IOException
      // Set response content type

      // New location to be redirected
      String site = new String("http://www.google.com");

      response.setHeader("Location", site);    

Now let us compile above servlet and create following entries in web.xml



Now call this servlet using URL http://localhost:8080/PageRedirect. This would take you given URL http://www.google.com.

Related Questions

+5 votes
2 answers 1,169 views
+1 vote
1 answer 87 views
+1 vote
1 answer 172 views
+2 votes
1 answer 607 views
0 votes
0 answers 161 views
asked in Programming by vimal jain
0 votes
0 answers 91 views
0 votes
1 answer 120 views
0 votes
1 answer 72 views
+1 vote
1 answer 816 views
+3 votes
1 answer 720 views

Not a Member yet?

Ask to Folks Login

My Account
683 Folks are online
1 members and 682 guest online
Your feedback is highly appreciated