In this tutorial I will cover HTTP basics and server side programming. If you are not a beginner and have done server side programming before then you can skip this post. However a java beginner should understand the basics of HTTP in servlet programming.
I am going to split it up in three sections – When a web browser sends a request the web server receives the request and sends the response based on the request. That’s how transaction gets completed.
Request, Response and Header
Client sending a request to Sever
When a client(web browser) makes a request, first line in the request it specifies is the URL and the version of HTTP protocol. for example
GET /index.html HTTP/1.0
The above request is using the GET method to ask for a webpage index.html using HTTP version 1.0. Along with the above line, client can send optional header information which can be helpful for webserver in order to generate better response. for example:
user-Agent: Firefox/4.0 (compatible; Windows 2000) Accept: image/jpg, image/png, */*
User-Agent header: It provides the information about client(web browser) software. In the above example it is telling web-server that request is being sent from a Firefox version 4.0 browser.
Accept header: It specifies the media(MIME) time which client can easily accept.
Server sending a response to client
In the above part we learned what all things client mentions while sending a request to server. So Once Web-Server processed the request it sends a response to client. In the first line it specifies the HTTP protocol server is using along with the status code of request. for example –
HTTP/1.0 200 OK
The status code 200 means request processed successfully.
HTTP/1.0 404 Not Found
The status code 404 means request document was not found by server.
Code Range Response Meaning
200-299 Client request successful
300-399 Client request redirected, further action necessary
400-499 Client request incomplete
500-599 Server error
Along with above status line Response header contains few additional things which tells client more about Sever side software and content type – for example –
Date: Monday, 15-May-2012 02:03:52 GMT Server: JavaWebServer/1.1.1 MIME-version: 1.0 Content-type: text/html Content-length: 2024 Last-modified: Tuesday, 9-May-2012 01:02:12 GMT
Points to remember:
- Server sends a blank line after the header section to separate it from content section.
- If server process the request successfully it sends the response along with header otherwise it respond back in human readable format to let them know why it had not be able to process the request.
GET & POST
The request which client sends to server can be of different types, generally referred as methods. The most frequently used methods are GET and POST.
It is used for fetching the information such as getting a webpage or querying a database. This method can include its own information in client’s request in order to better specify what to get. The GET methods parameters are appended to the URL while passing the request to server. The query string (URL+Parameters) can have maximum 240 characters thus through this method you cannot send large amount of information to server.
POST method is generally used to send large (in megabytes) and sensitive (confidential such as credit card number) information. It doesn’t append anything to the URL, instead it sends the information directly to server through a socket connection.
GET vs POST – A Gist:
1. For sending large amount of information you must prefer POST over GET.
2. For sensitive information use POST as it doesn’t append information in the URL so makes confidentiality.
Few Other Methods
Apart from GET and POST there are other HTTP methods, which are rarely used –
HEAD method : It is used by client when client just want to see response header information such as document modification date, document size etc.
PUT method: In order to place document directly on server.
DELETE method: Reverse of PUT method.
TRACE method: Used for debugging.
OPTIONS method: Can be used to know what all method concerned web server supports.
HttpServlet class implements
Serializable interface and extends
GenericServlet class. It provides the definition of
HTTP specific methods such as
The methods of
HttpServlet class are as follows:
- protected void doGet(HttpServletRequest request, HttpServletResponse response): It handles the GET request. It is invoked by the web container.
- protected void doPost(HttpServletRequest request, HttpServletResponse response): Similar to doGet() method, it also gets invoked by the web container and it handles the POST request.
- public void service(ServletRequest request,ServletResponse response): There are two types of service method, one is public and other one is protected. The purpose of this public method is to dispatch the request to the protected service method by converting the request and response object into HTTP type.
- protected void service(HttpServletRequest request, HttpServletResponse response): This method receives the request from the public service() method. It dispatches the request to the doXXX() method depending on the incoming http request type.
- protected void doHead(HttpServletRequest request, HttpServletResponse response): It handles the HEAD request.
- protected void doOptions(HttpServletRequest request, HttpServletResponse response): Performs the HTTP OPTIONS operation; the default implementation of this method automatically determines what HTTP Options are supported.
- protected void doPut(HttpServletRequest request, HttpServletResponse response): Performs the HTTP PUT operation; the default implementation reports an HTTP BAD_REQUEST error.
- protected void doTrace(HttpServletRequest request, HttpServletResponse response): Performs the HTTP TRACE operation; the default implementation of this method causes a response with a message containing all of the headers sent in the trace request.
- protected void doDelete(HttpServletRequest request, HttpServletResponse response): Performs the HTTP DELETE operation; the default implementation reports an HTTP BAD_REQUEST error.
- protected long getLastModified(HttpServletRequest request): Gets the time the requested entity was last modified; the default implementation returns a negative number, indicating that the modification time is unknown and hence should not be used for conditional GET operations or for other cache control operations as this implementation will always return the contents.