OCEJWCD 6 Tutorial: The servlet’s environment

The OCEJWCD 6 is the new exam for Java EE web component developers. This tutorial explains “The servlet’s environment” exam topic in detail with code examples.
Disclaimer : The contents are taken from free resources available on web including Oracle site and Wikipedia.
Servlets communicate with web users over a request response pattern executed by the servlet container. HTTP protocol can process the request−response pattern. Servlet container is essentially a part of a web server that interacts with the servlets.

The servlet container is used for managing other features of the servlet lifecycle. This article shows how to communicate with Servlets. And also understand the HTTP protocols, HTML forms, HttpServlet, related APIs and sample program to manage client sessions and cookies.

HTTP protocol

Understand more details of the HTTP protocol
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.

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

Following HTTP types used for perform some actions
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

Understand fundamentals of HTML forms

HTML Page Structure

A HTML page has global structure. The html page has two main element: 1. head and 2. body.
The Head element has following major parts:
  • 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:

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 target
HTML form input element has following attributes: type, name, value, size, maxlength, checked, src.

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>
Understand fundamentals of the HttpServlet and related APIs
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.

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
HttpServlet must override at least one method, usually one of these:
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);
}
}
For more information refer following documents

Exam Simulator

Commercial Exam Simulator for OCEJWCD 6 Certification 
MyExamCloud's OCEJWCD 6 Training Lab

Comments

Popular posts from this blog

Best Oracle Java Certification & Training

OCAJP - Java SE 8 Tutorial For Beginners & Professionals 2018