URL Rewrite in Servlets - Following is an example to understand the concept.
index.Html
[html]
<html>
<form action="./welcome">
Enter name:<input type="text" name="username"/>
Enter Email:<input type="text" name="emailId"/>
<input type="submit" value="submit"/>
</form>
</html>
[/html]
Here the developer created two text boxes to
Enter name and
Enter Email.
web.xml
[xml]
<web-app >
<servlet>
<servlet-name>DemoURLRewriting</servlet-name>
<servlet-class>servleturlrewriting.DemoURLRewriting</servlet-class>
</servlet>
<servlet>
<servlet-name>ShowURLRewriting</servlet-name>
<servlet-class>servleturlrewriting.ShowURLRewriting</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DemoURLRewriting</servlet-name>
<url-pattern>/welcome</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>ShowURLRewriting</servlet-name>
<url-pattern>/servlet2</url-pattern>
</servlet-mapping>
</web-app>
[/xml]
Sample rule of WEB.XML file as follows.
Make sure that servlet name should be same and URL name also should be same. Here the developer used two classes they are
DemoURLRewriting,
ShowURLRewriting.
DemoURLRewriting.java
[java]
package servleturlrewriting;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class DemoURLRewriting extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String userName=request.getParameter("username");
String emailId=request.getParameter("emailId");
//appending the username in the query string
out.print("<h2><a href='servlet2?uname="+userName+"&email="+emailId+"'>click Here</a></h2>");
out.flush();
out.close();
}
}
[/java]
Sets the content type of the response being sent to the client, if the response has not been committed yet. The given content type may include a character encoding specification. Sets the content type of the response being sent to the client, if the response has not been committed yet. The given content type may include a character encoding specification.
In
doGet(), the parameters are appended to the URL and sent along with the header information. The
doPost() shall be used when comparatively large amount of sensitive data has to be sent. Examples are sending data after filling up a form or sending login id and password. Flushes the output stream and forces any buffered output bytes to be written out. The general contract of flush is that calling it is an indication that, if any bytes previously written have been buffered by the implementation of the output stream, such bytes should immediately be written to their intended destination.
ShowURLRewriting.java
[java]
package servleturlrewriting;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ShowURLRewriting extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String userName = request.getParameter("uname");
String email = request.getParameter("email");
out.println("<h2>This is your name get from the URL pattern::"+userName+"</h2>");
out.println("<h2>This is your emailId get from the URL pattren::"+email+"</h2>");
out.flush();
out.close();
}
}
[/java]
Here the parameters will be called and the result will be displayed on the browser.
Output
URL Rewrite in Servlets - Following is the result where user has to enter the details.
Click on the link.
Result will be as follows.