1. Introduction to Java Servlets




1.1 Describe web applications, CGI, and the role of Java

Web applications

  • Web applications are programs that run on a web server. Multiple clients can connect to them across the Internet.
  • Clients send a request and the web server hosting the web application send a response.
  • Clients send requests using HTTP, by accessing URLs and submitting forms. The most common response type is HTML, although multimedia can be sent too.

See also:

CGI

Common Gateway Interface is an older way of creating web applications. When the server receives a request it spawns a separate program to deal the with request and return a result, usually HTML. Scalability is a big problem, as a new process is needed everytime.

Role of Java

A Java Runtime Environment is provided to web applications hosted on Java Web Application Servers.


1.2 Describe benefits of Java servlet technology

  • Requests for servlet resources are handled in separate threads, increasing performance
  • Servlets are Java EE components, so benefit from container services
  • Unlike CGI, no new processes are spawn


1.3 Create a simple Java Servlet

See also: 4.3 Understand fundamentals of the HttpServlet and related APIs

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;
 
public class BasicServlet extends HttpServlet {
 
    /*
        Handle GET requests
     */
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        PrintWriter out = response.getWriter();
        java.util.Date today = new java.util.Date();
        out.println("<html> " +
                "<body>" +
                "<h1>Basic Servlet</h1>"
                +"<br>" + today  +"</body>" + "</html>");
    }
 
    /*
        Handle POST requests
        throws IOException not needed, as not using PrintWriter
     */
    public void doPost(HttpServletRequest request, HttpServletResponse response) { 
        // not implemented
    }
 
}

In web.xml

  <servlet>
    <servlet-name>Basic Servlet</servlet-name>
    <servlet-class>com.example.servlets.BasicServlet</servlet-class>
  </servlet>
 
  <servlet-mapping>
    <servlet-name>Basic Servlet</servlet-name>
    <url-pattern>/BasicServlet</url-pattern>
  </servlet-mapping>



1.4 Define three-tier architecture

The three tiers are:

  1. Presentation: what the user sees
  2. Business: code implementing business rules
  3. Data/Persistence: how data is saved and retrieved behind the scenes


1.5 Define Model-View-Controller (MVC) architecture

See also:

Model

Non-servlet objects containing business logic

View

JSPs and tags used for presentation.

Controller

Servlets used to direct client requests to appropriate business logic and return a view.

There may also be a Front Controller, which sits before all other controllers, and handles common functionality.


Other design patterns

Design principles

Unclear are to whether these on exam:

  • Code to interfaces
  • Separation of concerns
  • Cohesion
  • Hide complexity
  • Loose coupling (via interfaces)
  • Declarative control via deployment descriptor

Design patterns

Remote proxy

Proxy (stub) communicates with real object via RMI. The server may also use a proxy, called a skeleton. The client only sees an interface retrieved via JNDI or RMI registry.

Business Delegate

It is called by the Controller. The single BD invokes the Remote Proxy (Stub), rather than each Controller having to do it. Use the Business Delegate pattern to shield your web tier controllers from the fact that some of your app’s model components are remote.

Service Locator

It can replace stubs and reduce complexity, by doing all the JNDI/UDDI lookup stuff. Use the Service Locator pattern to perform registry lookups so you can simplify all of the other components (such as Business Delegates) that have to do JNDI (or other registry types) lookups.

Data Transfer Object

A serialized object containing data. Use the Transfer Object pattern to minimize network traffic by providing a local representation of a fine-grained remote component (usually an entity).

Front Controller

Implemented by Struts, Spring, etc. Use the Front Controller pattern to gather common, often redundant, request processing code into a single component. This allows the application controller to be more cohesive and less complex. An application will use a front controller to:

  • dispatch requests to the appropriate JSP pages.
  • manage the workflow process among the pages.
  • load the behavior of the controller dynamically for flexibility.

Intercepting Filter

Use the Intercepting Filter pattern to modify requests being sent to servlets, or to modify responses being sent to users. From enthuware:
Create pluggable filters to process common services in a standard manner without requiring changes to core request processing code. The filters intercept incoming requests and outgoing responses, allowing preprocessing and post-processing. We are able to add and remove these filters unobtrusively, without requiring changes to our existing code.

We are able, in effect, to decorate our main processing with a variety of common services, such as security, encryption, compression, debugging, and so forth. These filters are components that are independent of the main application code, and they may be added or removed declaratively. For example, a deployment configuration file may be modified to set up a chain of filters. The same configuration file might include a mapping of specific URLs to this filter chain. When a client requests a resource that matches this configured URL mapping, the filters in the chain are each processed in order before the requested target resource is invoked.





Comments

If you have questions, corrections, information or examples to add, please comment below.

Add a New Comment
or Sign in as Wikidot user
(will not be published)
- +
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License