Monday 11 August 2014

getQueryString : Get the Query string associated with the URL

public String getQueryString()
A query string is the part of a uniform resource locator (URL) that contains data to be passed to web applications like Servlets, getQueryString method Returns the query string that is contained in the request URL after the path.

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Get Books</title>
    </head>
    <body>
        <form method="get" action="/servlet/GetBooks">
           <select name="author">
                <option>Yashavant Kanetkar</option>
                <option>Deitel</option>
                <option>Herbert Schildt</option>
                <option>Bruce Eckel</option>
          </select> 
            <input type="submit" value="submit" />
        </form>
    </body>
</html>

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

@WebServlet(urlPatterns = {"/GetBooks"})
public class GetBooks extends HttpServlet {
     @Override
    public void doGet(HttpServletRequest req, HttpServletResponse res)throws IOException, ServletException{
        try(PrintWriter out = res.getWriter()){
            String query = req.getQueryString();
            out.println("<html><head><title>Server Details</title>");
            out.println("<body><h1>");
            out.println("Query String: " + query);
            out.println("</h1></body></html>");
        }
    }
}

Output







Prevoius                                                 Next                                                 Home

No comments:

Post a Comment