Wednesday 16 July 2014

doGet (HttpServletRequest req, HttpServletResponse resp) : Handles Get requests

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
"GET" action is used generally to retrieve the data from a server machine. It is called by server to process a get request. If the request is incorrectly formatted, doGet returns an HTTP "Bad Request" message. DoGet method automatically supports HEAD request also.

In your html/jsp page if you choose method type as 'get'
  1. GET method appends the name-value pairs on the address bar. Thereby it has got the limitations on the number of characters (hence the values) to be passed. Since number of characters supported in address bar is browser dependent.
  2. As information sent to the server is visible in the address bar, certain sensitive information like your credit card number, password etc are not suggested to be sent this way.
  3. Since the complete values are available in the address bar, it can be easily bookmarked.
<%@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>

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
    <servlet>
        <servlet-name>GetBooks</servlet-name>
        <servlet-class>GetBooks</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>GetBooks</servlet-name>
        <url-pattern>/GetBooks</url-pattern>
    </servlet-mapping>
</web-app>

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

public class GetBooks extends HttpServlet {
    
    Map<String, List<String>> booksInfo = new HashMap<> ();
    
    List<String> Yashavant = new ArrayList<> ();
    List<String> Deitel = new ArrayList<> ();
    List<String> Herbert = new ArrayList<> ();
    List<String> Bruce = new ArrayList<> ();
    
    {
        Yashavant.add("C Projects");
        Yashavant.add("Data Structure Through C ");
        Yashavant.add("Let Us C");
        Yashavant.add("Test Your C Skill");
        
        Deitel.add("How To Program C");
        Deitel.add("How To Program C++");
        Deitel.add("How To Program Java");
        Deitel.add("How To Program Android");
        
        Herbert.add("Java Programming Cook Book");
        Herbert.add("C++ Programming Cook Book");
        
        Bruce.add("Using C++");
        Bruce.add("Thinking in C++");
        Bruce.add("Thinking in Java");
        
        booksInfo.put("Yashavant Kanetkar", Yashavant);
        booksInfo.put("Deitel", Deitel);
        booksInfo.put("Herbert Schildt", Herbert);
        booksInfo.put("Bruce Eckel", Bruce);  
    }
    
    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException{
       PrintWriter out = res.getWriter();
       String author;
       List<String> books;
       Iterator<String> iter;
       StringBuilder body;
       res.setContentType("text/html");
      
       String header = "<html>" +
                    "<head>" +
                    "<title> Hello World </title>" +
                    "</head>" +
                    "<body>" +
                    "<h1> <br />";                     
       String footer = "</h1> </body>" +
                    "</html>";
       
       body = new StringBuilder();
       author = req.getParameter("author");
       body.append("Books written by ").append(author).append(" are <br />");
       
       books = booksInfo.get(author);
       iter = books.iterator();
       
       while(iter.hasNext()){
           body.append(iter.next()).append("<br />");
       }
       
       out.println(header + "" + body + footer);
    }
}


Output




As you observe the address bar, request parameters are passed as part of Address Bar.

req.getParameter("author") returns the value of a request parameter 'author'.



Prevoius                                                 Next                                                 Home

No comments:

Post a Comment