HTTP protocol

Example client request

Request Line : GET /images/help.jpg HTTP 1.1 Header : Connection : Keep-Alive Referrer : http://localhost:8080/index.html User-Agent : Mozilla/4.0 Host : nplus1.net Content-Type : application/x-www-form-urlencoded Content-Length : 28 Body : name=images&value=bar

Example server response

Status Line HTTP/1.1 200 OK

Header : Date : Mon, 10 June 2013 06:04:34 GMT Server : Apache/2.4.4 Content-Type : text/html Content-Length : 2456 Last-Modified : Mon, 10 June 2013 02:43:45 GMT Body : <html> <header><title>Welcome Image</title> ..... </html>

Types of HTTP Request

HEAD - requests header information.

GET - retrieves a single resource

POST – Send the client information to server.

PUT - Send a resource to be stored on the server.

DELETE - removes resource from specified URL.

TRACE - returns the entire network route from client to server and back.

OPTIONS - returns list of server's supported HTTP methods.

CONNECT - Converts the request connection to a transparent TCP/IP tunnel.

PATCH - Is used to apply partial modifications to a resource.

HTML forms

HTML Page Structure

HEAD element, TITLE element and Meta data.

The body element has following major fields: Identification element (class, id), Grouping element (span, p), Header element (H1, h3, h4...)

Simple HTML document:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"

"http://www.w3.org/TR/html4/strict.dtd">

<HTML>

<HEAD>

<TITLE>Welcome to Kate Java Tutorials</TITLE>

</HEAD>

<BODY>

<P>Simple HTML page document.</p>

</BODY>

</HTML>

HTML Form:

Sample HTML Form :

<FORM action="http://katejava.com/exam/adduser" method="post">

<P>

First name: <INPUT type="text" name="firstname"><BR>

Last name: <INPUT type="text" name="lastname"><BR>

email: <INPUT type="text" name="email"><BR>

<INPUT type="radio" name="sex" value="Male"> Male<BR>

<INPUT type="radio" name="sex" value="Female"> Female<BR>

<INPUT type="submit" value="Send"> <INPUT type="reset">

</P>

</FORM>

HttpServlet and related APIs

Servlet API

javax.servlet Support generic, protocol-independent servlets Servlet (interface) GenericServlet (class) service() ServletRequest and ServletResponse Provide access to generic server requests and responses

javax.servlet.http Extended to add HTTP-specific functionality HttpServlet (extends GenericServlet ) doGet() doPost() HttpServletRequest and HttpServletResponse Provide access to HTTP requests and responses



User-defined Servlets

Inherit from HttpServlet Override doGet() and doPost()

To handle GET and POST requests Have no main() method



doGet, if the servlet supports HTTP GET requests

doPost, for HTTP POST requests

doPut, for HTTP PUT requests

doDelete, for HTTP DELETE requests

init and destroy, to manage resources that are held for the life of the servlet

getServletInfo, which the servlet uses to provide information about itself

doGet() and doPost()

protected void doGet( HttpServletRequest req, HttpServletResponse resp)

protected void doPost(HttpServletRequest req, HttpServletResponse resp)

Sample HttpServlet

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

public class HelloWorld extends HttpServlet {

public void doGet(HttpServletRequest req, HttpServletResponse res)

throws ServletException, IOException {

res.setContentType("text/html");

PrintWriter out = res.getWriter();

out.println("<html>");

out.println("<head><title>hello world</title></head>");

out.println("<body>");

out.println("<big>hello world</big>");

out.println("</body></html>");

}



public void doPost(HttpServletRequest req, HttpServletResponse res)

throws ServletException, IOException {

doGet(req, res);

}

}

Servlet related APIs

JavaTM Platform, Enterprise Edition 6 API Specification

http://docs.oracle.com/javaee/6/api/

http://docs.oracle.com/javaee/6/api/ What is servlet API in java?

http://wiki.answers.com/Q/What_is_servlet_API_in_java

Exam Simulator

Hypertext Transfer Protocol is a stateless protocol. Generally this HTTP protocol is used to transfer the HTML and other information. Uniform Resource Locator (URL) is a universal address for the HTML documents and other resources. This HTTP is belongs to the application layer.Initially the client creates a connection to HTTP server. The HTTP client sends a message based on the HTTP standard (HTTP Request) and then waits for the answer. Server answers in the form of HTTP response to the client. HTTP client read the response. Then close/release the connection from the server.Following HTTP types used for perform some actionsA HTML page has global structure. The html page has two main element: 1. head and 2. body.The Head element has following major parts:HTML form has following control types: buttons, checkboxes, radio buttons, menus, text input, file select and object controls.HTML form control element has following attributes: accept, accept-charset, action, enctype, method, name and targetHTML form input element has following attributes: type, name, value, size, maxlength, checked, src.The Servlet API consists of two packages: javax.servlet and javax.servlet.http. The base functionality is defined in the javax.servlet package whose classes and interfaces outline a generic, protocol-independent implementation., usually one of these:For more information refer following documentsMyExamCloud's OCEJWCD 6 Training Lab