getQueryString() Method Example

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

web.xml entry for ClientInformation servlet

Servelet Program: ClientInformation.java

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

getQueryString()

The output screen when submit button is clicked.

getQueryString()

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 Reply

Your email address will not be published. Required fields are marked *