How to Redirect to a New URL in Servlet

How to Redirect to a New URL in Servlet thumbnail
Use a redirect to move your web page to a new location

Servlet is a type of Java programming that allows you to connect to a web server. Servlet can be used to control request and responses to web hosts on your web pages. Servlet uses basic Java code and can be programmed into a web page just as you would Javascript. One of the ways you can use Servlet is to redirect your web page to a new location or automatically move your web page to a new URL.

Instructions

    • 1

      Open a text editor, such as Notepad or Text Edit. Select "File," then "Save" from the top menu. Save your page with a .html extension, and name it what you want your web page to be called. For instance, if you want the page to have a URL of www.yourdomain.com/newpage.html, call your file "newpage.html."

    • 2

      Type <html> at the top of the page. Next, type "<script type="text/javascript">". This will indicate that you will be using Javascript.

    • 3

      Import Java servlet code into your page. Type in:
      "import javax.servlet.ServletException;
      import javax.servlet.http.HttpServlet;
      import javax.servlet.http.HttpServletRequest;
      import javax.servlet.http.HttpServletResponse;"
      This will import the code necessary for your page to recognize your URL redirect.

    • 4

      Insert the redirect code. Type in:
      "public class ExampleServlet extends HttpServlet {
      public void service(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
      response.sendRedirect("http://www.yourredirect.com");
      }
      }"
      Replace "http://www.yourredirect.com" to the URL you wish to redirect your page to.

    • 5

      Type in "</script></html>" to indicate that you are finished with your web page. Select "File," then "Save" from the top menu. Upload this page to your web server to have a functional URL redirect.

Tips & Warnings

  • Servlet URL redirects may not be recognized by search engines and is not recommended for proper SEO.

Related Searches:

References

  • Photo Credit Hemera Technologies/Photos.com/Getty Images

Comments

Related Ads

Featured