getQueryString() Method Example

This method is defined in HttpServletRequest interface from javax.servlet.http package.

With this method, the servlet programmer can know the data (through FORM fields like user name etc.) sent by the client.

What is query string in servlets?

It is the string containing the name of FORM fields and the data entered by the user in the fields. Field is separated with the value entered by the user with = symbol (see the output screen).

Let us see what Java API says about this method.

  • java.lang.String getQueryString():
    Returns the query string that is contained in the request URL after the path. This method returns null if the URL does not have a query string. Same as the value of the CGI variable QUERY_STRING.

The method getQueryString() returns a string of data, filled by the user in FORM fields (of text boxes, check boxes etc.), and sent to server.

Let us see the output by writing a program.

HTML Program: ClientData.html

Enter Your Name

web.xml entry for ClientInformation servlet



Servelet Program:

import javax.servlet.*; 
import javax.servlet.http.*;

public class ClientInformation extends HttpServlet
  public void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
    PrintWriter out = res.getWriter();

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


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


The output screen when submit button is clicked.


Observe, t1 is the name of text box in the HTML file and SNRao is the value entered in t1. The field and value are separated by = symbol.

Note: With POST method in HTML, it is observed that the queryString() returns null.

View all Servlets

Leave a Comment

Your email address will not be published.