OCEJWCD 6 Tutorials: Introduction to Java Servlets


This tutorial is one of the certification exam topics which gives attention into Java Servlet Technology . This will help your preparation to pass the 1Z0-899 Java EE 6 Web Component Developer Certified Expert Exam.

Servlet is a Java class that supplies compatible mechanism used to extend the ability of a Web-server that can support dynamic content generation. A servlet can almost be considered an applet that runs on the server side. Servlet provides a platform-independent methods to build Web-based applications. Servlets is a highly efficient CGI programs written in Java. When a Servlet-capable Web server receives a request for a URL that corresponds to a Java Servlet, the server hands the request off to the Servlet for processing. The Servlet dynamically produces a response to the request (typically an HTML Web page) and sends it back to the requesting browser.

Describe web applications, CGI, and the role of Java

Java Web application:

Web application is an computer application which is then deployed on the web server, the user can access that application via the Internet or an intranet using a browser. Java has strong support for building web applications using Java Enterprise technology. A Java web application is a collection of  HTML pages, XML files, webservices, servlets, JSPs Java classes and jars to create dynamic web project.

Java CGI - Common Gateway Interface:

CGI is a scripts to receive the request and generate a response for transferring dynamic information between server and client. A CGI program can be written in any language, including Java, that can be executed by your Web server. CGI programs are commonly used to add search engines, guest-book applications, database-query engines, interactive-user forums, and other interactive applications to Web sites. Most of the input to a CGI program is passed into it through environment variables.

Role of Java:

Following different roles are divided based on the Servlet performance. Receive the explicit data sent by the client, receive the implicit HTTP request data sent by the browser, Generate the results, Dispatch the explicit information to the client and Dispatch the implicit HTTP response data.

Describe benefits of Java servlet technology

The advantages of using servlets is their fast performance and ease of use combined with more power over traditional CGI (Common Gateway Interface). Traditional CGI scripts written in Java have a number of disadvantages when it comes to performance: When a HTTP request is made, a new process is created for each call of the CGI script. This overhead of process creation can be very system-intensive, especially when the script does relatively fast operations. Thus, process creation will take more time than CGI script execution. Java servlets solve this, as a servlet is not a separate process. Each request to be handled by a servlet is handled by a separate Java thread within the Web server process, omitting separate process forking by the HTTP daemon.

Simultaneous CGI request causes the CGI script to be copied and loaded into memory as many times as there are requests. However, with servlets, there are the same amount of threads as requests, but there will only be one copy of the servlet class created in memory that stays there also between requests.

The Web container calls the init()method. This method initializes the servlet and must be called before life of a servlet, the init()method is called only once.

After initialization, the servlet can service client requests. Each requestis serviced in its own separate thread. The Web container calls the service() method of the servlet for every request. The service()method determines the kind of request being made and dispatches it to an appropriate method to handle the request. The developer of the servlet must provide an implementation for these methods. If a request for a method that is not implemented by the servlet is made, the method of the parent class is called, typically resulting in an error being returned to the requester.
Finally, the Web container calls the destroy() method that takes the servlet out of service. The destroy() method, like init(), is called only once in the lifecycle of a servlet.

Create a simple Java Servlet

public class MyServlet extends HttpServlet {
    public void doGet (HttpServletRequest req, HttpServletResponse res) {
                ....
    }
}

Deployment descriptor (web.xml)

<web-app>
  <servlet>
          <servlet-name>MyServlet</servlet-name>
          <servlet-class>samples.MyServlet</servlet-class>
  </servlet>

  <servlet-mapping>
          <servlet-name>MyServlet</servlet-name>
          <url-pattern>/MyApp</url-pattern>
  </servlet-mapping>
...
</web-app>

Define three-tier architecture

3-Tier client-server architectures have 3 essential components:

  1. A Client PC 
  2. An Application Server 
  3. A Database Server

3-Tier Architecture Considerations: 


  • Client program contains presentation logic only 
  • Less resources needed for client workstation 
  • No client modification if database location changes 
  • Less code to distribute to client workstations 
  • One server handles many client requests 
  • More resources available for server program 
  • Reduces data traffic on the network

Define Model-View-Controller (MVC) architecture

One of the most common Design Patterns is Model-View-Controller (MVC). The model does all the computational work. It is input/output free. All communication with the model is via methods. The controller tells the model what to do. User input goes to the controller. The view shows results; it is a “window” into the model. The view can get results from the controller, or The view can get results directly from the model

The model, as usual, does all the computational work, and no Input / Output. The model can consist of multiple classes. The servlet class (the one that extends HttpServlet) acts as the controller. The servlet gives any relevant information from the user request to the model. The servlet takes the results and passes them on to the view. The view—that is, the HTML page that is returned to the user—is frequently created by JSP.

Model: The domain-specific representation of the information on which the application operates. The model is another name for the application logic layer (sometimes also called the domain layer). Application (or domain)logic adds meaning to raw data (e.g., calculating iftoday is the user’s birthday, or the totals, taxes and shipping charges for shopping cart items). Many applications use a persistent storage mechanism (such as a database) to store data. MVC does not specifically mention the resource management layer because it is understood to be underneath or encapsulated by the Model.

View: Renders the model into a form suitable for interaction, typically a user interface element. MVC is often seen in web applications, where the view is the HTML page and the code which gathers dynamic data for the page.

Controller: Processes and responds to events, typically user actions, and may invoke changes on the model and view

Popular posts from this blog

Best Oracle Java Certification & Training

OCAJP - Java SE 8 Tutorial For Beginners & Professionals 2018