×
☰ See All Chapters

URL Rewriting in Servlet

In URL rewriting, we append a query string to the URL. Below figure gives the different parts of a URL

url-rewriting-0
 

Query string is a name value pair separated using an equal = sign, a name/value pair is separated from another name/value pair using the ampersand (&).  Query string always should start from Question mark (?). From a Servlet, we can use getParameter(), getParameterNames() methods to obtain a name value pair.

Example for URL rewriting

Project directory structure

url-rewriting-1
 

home.html

<!DOCTYPE html>

<html>

<body>

        <form action="../servlet1" method="post">

                Enter Personal Details:<br /> Name:<input type="text" name="name" /><br />

                <input type="submit" value="go" />

        </form>

</body>

</html>

FirstServlet.java

package com.java4coding;

 

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

 

public class FirstServlet extends HttpServlet {

        public void doPost(HttpServletRequest request, HttpServletResponse response) {

                try {

                        response.setContentType("text/html");

                        PrintWriter out = response.getWriter();

                        String name = request.getParameter("name");

                        out.print("Welcome " + name);

                        out.print("<form action='servlet2?name="+name+"' method='post'> ");

                        out.print("Enter Contact Details:<br/>");

                        out.print("Address:<input type='text' name='address'/><br/>");

                        out.print("<input type='submit' value='go'/> ");

                        out.print("</form>");

                        out.close();

                } catch (Exception e) {

                        System.out.println(e);

                }

        }

}

SecondServlet.java

package com.java4coding;

 

import java.io.*;

import javax.servlet.http.*;

 

public class SecondServlet extends HttpServlet {

        public void doPost(HttpServletRequest request, HttpServletResponse response) {

                try {

                response.setContentType("text/html");

                PrintWriter out = response.getWriter();

                String address = request.getParameter("address");

                String name = request.getParameter("name");

                out.print("<form action='servlet3?name="+name+"&address="+address+"' method='post'> ");

                out.print("Enter Eduction Details:<br/>");

                out.print("Education:<input type='text' name='education'/><br/>");

                out.print("<input type='submit' value='go'/> ");

                out.print("</form>");

                out.close();

                } catch (Exception e) {

                        System.out.println(e);

                }

        }

}

ThirdServlet.java

package com.java4coding;

 

import java.io.*;

 

import javax.servlet.*;

import javax.servlet.http.*;

 

public class ThirdServlet extends HttpServlet {

        public void doPost(HttpServletRequest request, HttpServletResponse response) {

                try {

                        response.setContentType("text/html");

                        PrintWriter out = response.getWriter();

                        out.print("Name: " + request.getParameter("name") + "</br>");

                        out.print("Address: " +request.getParameter("address") + "</br>");

                        out.print("Education: " + request.getParameter("education"));

                        out.close();

                } catch (Exception e) {

                        System.out.println(e);

                }

        }

}

web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:web="https://xmlns.jcp.org/xml/ns/javaee">

  <servlet>

    <servlet-name>s1</servlet-name>

    <servlet-class>com.java4coding.FirstServlet</servlet-class>

  </servlet>

  <servlet-mapping>

    <servlet-name>s1</servlet-name>

    <url-pattern>/servlet1</url-pattern>

  </servlet-mapping>

  <servlet>

    <servlet-name>s2</servlet-name>

    <servlet-class>com.java4coding.SecondServlet</servlet-class>

  </servlet>

  <servlet-mapping>

    <servlet-name>s2</servlet-name>

    <url-pattern>/servlet2</url-pattern>

  </servlet-mapping>

  <servlet>

    <servlet-name>s3</servlet-name>

    <servlet-class>com.java4coding.ThirdServlet</servlet-class>

  </servlet>

  <servlet-mapping>

    <servlet-name>s3</servlet-name>

    <url-pattern>/servlet3</url-pattern>

  </servlet-mapping>

</web-app>

Output

url-rewriting-2
 

All Chapters
Author