getRequestURI() Method Example


What is client data?

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 to access the server, client IP address and also the name of the browser client is using, 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 getRequestURI() to retrieve the a part of 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.String getRequestURI();

Example on getRequestURI()

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();

    String str = req.getRequestURI();
    out.println("req.getRequestURI()  :  " + str);

    out.close();
  }
}

Following is the method signature as defined in HttpServletRequest interface.

public abstract java.lang.String getRequestURI();

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

getRequestURI()

The output screen when submit button is clicked.

ima

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

View All Servlets

Leave a Comment

Your email address will not be published.