getRequestURL() method Example


What is client data?

getRequestURL(): When the user clicks the submit button, we think that the data entered by the user like user name and password are sent to the server. Ofcourse, right. But along with it, lot of client data goes to the server. The client data includes the protocol used by the client, client IP address and also the name of the browser. Also its version (headers) etc.

Is there anyway to retrieve client data from the Servlet?

Yes, HttpServletRequest interface comes with many methods to retrieve the client data.

This program uses getRequestURL() to retrieve the URL used by the client to call the servlet on the server.

Following is the method signature as defined in HttpServletRequest interface.

public abstract java.lang.StringBuffer getRequestURL();

Example on getRequestURL()

Client Program: ClientData.html

Enter Your Name

web.xml entry for ClientInformation servlet


  efgh
  ClientInformation



  efgh
  /jasmine

Server Program: ClientInformation.java

import javax.servlet.*; 
import javax.servlet.http.*;
import java.io.*; 

public class ClientInformation extends HttpServlet
{
  public void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
  {
    res.setContentType("text/html");
    PrintWriter out = res.getWriter();

    StringBuffer sb = req.getRequestURL();
    out.println("req.getRequestURL() : " + sb);

    out.close();
  }
}

Output screen of ClientData.html with text field filled up.

getme

The output screen when submit button is clicked.

getRequestURL()

Observe, the getRequestURL() prints the complete URL (written in ACTION attribute of FORM tag) used by the client to call the servlet. This is where getRequestURL() differs from getRequestURI().

Leave a Comment

Your email address will not be published.