Monday, February 18, 2013

Basic Java Servlet Structure : Servlet that generates HTML

Basic Servlet Structure : Servlet that generates HTML

Basic Servlet Structure
Java servlets can be said server side programming. To make servlet application, javax.servlet and javax.servlet.http provides required classes and interfaces. Servlets generally extend the HttpServlet class and override the doGet or the doPost methods. In addition, other methods such as init, service and destroy also called as life cycle methods.  The structure of a servlet is given below

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

public class SomeServlet extends HttpServlet {
  public void doGet(HttpServletRequest request,HttpServletResponse response)
      throws ServletException, IOException {
     
    // Code for Business Logic

  }public void doPost(HttpServletRequest request,HttpServletResponse response)
      throws ServletException, IOException {
    doGet(request, response);
    }

As a servlet, a class should extend HttpServlet and override doGet or doPost (or both), depending on whether the data is being sent by GET or by POST. In above example, doGet method is call from doPost method. This is because, if programmer has written all his business logic in doGet method, and doPost method is called from jsp, then it will get redirect to doGet method. It removed complexity of programmer to remember and arrange business logic accouding to it's calling nature from jsp page. doGet and doPost methods take two arguments: an HttpServletRequest and an HttpServletResponse. The HttpServletRequest has methods that let you find out about incoming information such as FORM data, HTTP request headers, and the like. The HttpServletResponse has methods that lets you specify the HTTP response line (200, 404, etc.), response headers (Content-Type, Set-Cookie, etc.). Note that doGet and doPost throw two exceptions, so it is necessary to include them in the declaration. Also note that you have to import classes in java.io (for PrintWriter, etc.), javax.servlet (for HttpServlet, etc.), and javax.servlet.http (for HttpServletRequest and HttpServletResponse).

Servlet that generates HTML

Not only plain text but HTML can also generated by Servlets. 

package com.cc;

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

public class HelloWorld extends HttpServlet {
  public void doGet(HttpServletRequest request,
                    HttpServletResponse response)
      throws ServletException, IOException {
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
                                        "Transitional//EN\">\n" +
                "<HTML>\n" +
                "<HEAD><TITLE>Hello World</TITLE></HEAD>\n" +
                "<BODY>\n" +
                "<H1>Hello World</H1>\n" +
                "</BODY></HTML>");
  }
}

Difference Between doGet and doPost Methods

doGet is used when there is are requirement of sending data appended to a query string in the URL. The doGet models the GET method of Http and it is used to retrieve the info of the client to server for processing it .The doGet cannot be used to send too much info appended as a query stream. GET puts the form values into the URL string. GET is limited to about 256 characters (usually a browser limitation) and creates really complicated URLs.

POST allows you to have extremely dense forms and pass that to the server without clutter or limitation in size. e.g. you obviously can't send a file from the client to the server via GET. POST has no limit on the amount of data you can send and because the data does not show up on the URL you can send passwords. But this doesn’t mean that POST is truly secure. For real security encryption is required which is an entirely different topic.

No comments:

Post a Comment