Servlets and Java Server Pages and SCWCD Notes

by Sandeep Desai (http://www.thedesai.net/sandeep)

 

This document can be used as a reference for Servlets and JSP. It also can be used for the Sun Certified Web Components Developers Certification 1.4 exam

 

Book Reference:

Head First Servlets and Java Server Pages by Kathy Sierra, Bert Bates and Bryan Basham

 

Mock exams

Jdiscuss, J2EECertificate, Whizlabs

 

Download Tomcat Servlet and JSP container 5.0 or greater

 

Browser (IE, Mozilla)

  Send requests to web server

  mime type (type of documents that browser understand e.g html, text, mpeg etc)

URL (Uniform Resource Locator)

HTTP (Hypertext Tranfer Protocol, stateless protocol)

  GET-> Send parameters as part of URL  (e.g http://www.thdesai.net/get?s=a)

      get less secure

  POST -> Send parameters as part of HTTP post 

CGI (Common Gateway Interface)

 

Tomcat Servlet container

 

Distributing servlet

 

Thread safe

 

Not Thread safe

 

Web application has

 

 

myservlet Web Application

 

Web Application directory structure

webapps

  MyApp

    *.jsp, *.html (directly accessible)

    client applet jar files

    WEB-INF

      web.xml (Deployment Descriptor)

      tags

      classes

      lib

        x.jar

          META-INF

             tags

             tlds

     

*.jsp and *.html under the app directory or subdirectory are directly accessible by a browser   (Invalid request produce 404 not found error)            

*.jsp and *.html can also be under WEB-INF or subdirectory under WEB-INF. These are not directly accessible by URL

*.tld under WEB-INF directory or subdirectory

*.tld in a jar file under META-INF directory or subdirectory

.tag or .tagx under tags directory or WEB-INF\lib\*.jar\META-INF\tags directory or subdirectory

servlet classes under WEB-INF\classes or WEB-INF\lib\*.jar

tag handler class under WEB-INF\classes or WEB-INF\lib\*.jar

*.jar under WEB-INF\lib

 

WAR (Web Archive) file structure

 

 

XML compliant JSP document, used by tool vendors

Enclose document with jsp:root, taglib in jsp:root itself

jsp:root optional in JSP 1.2 version attribute mandatory

<jsp:root version ="1.2" xmlns:test="foo.tld">

 

 

Normal JSP syntax

JSP document syntax

Directives

(except taglibs)

<%@ page import="java.io.*" %>

<jsp:directive.page import="java.io.*"/>

Declaration

<%! int x=0; %>

<jsp:declaration> int x=0; </jsp:declaration>

scriptlet

<% ++x; %>

<jsp:scriptlet> ++x; </jsp:scriptlet>

Text

Hello World

<jsp:text>Hello World<jsp:text>

Scripting expression

<%= x %>

<jsp:expression> x </jsp:expression>

 

 

 

Security

 

Responsiblity

Authentication (userid/password)

Admin

Authorization (roles e.g guest, admin)

Deployer

Confidentiality (e.g using public keys)

Deployer

Data Integrity  (encryption using https)

Deployer

 

Realm is a place where authentication information is stored

Tomcat stores users list in conf/tomcat-users.xml file. The storage mechanism and data structure is vendor specific

 

<?xml version='1.0' encoding='utf-8'?>

<tomcat-users>

  <role rolename="tomcat"/>

  <role rolename="manager"/>

  <role rolename="admin"/>

  <user username="tomcat" password="tomcat" roles="tomcat"/>

  <user username="admin" password="admin" roles="admin,manager"/>

</tomcat-users>

 

In DD

<security-role>

  <role-name>admin</role-name>

</security-role>

<login-config>

  <auth-method>BASIC</auth-method>

</login-config>

 

Design Principles

·        Code to interface

·        separation of concern.

·        Cohesion, degree to which class is designed for one task or purpose

·        Hide complexity

·        Loose coupling, keep classes less interdependent

·        Proxy

·        Make app more declarative i.e. easier to modify at deploy time

 

Patterns

See Patterns document for more details

 

Composite

·        Used for combining UI elements in the form of a tree

 

Business Delegate (hide remote proxy business object)

 

Service Locator (used for registry (JNDI) lookup of components)

 

 

Transfer Object (minimize network traffic by providing local representation of remote object)

 

Intercepting Filter (modify requests before going to servlet or response before it sent back to browser)

·        Intercept and/or modify requests before they reach servlet

·        Intercept and/or modify request before they are returned to browser

·        Filters are deployed declaratively in DD

·        Filters can be chained

·        Filter lifecycle is managed by the container

·        Filter must implement init() destroy() and doFilter() callback

·        Principles

o       OO Principles

·        cohesion,

·        loose coupling

o       Declarative control allows filter to be added or remove easily

o       Declarative control allows filter chaining sequence to changed easily

 

Data Access Object

·        specify data source at deploy time

·        data client independant of data source API

 

Model, View, Controller (MVC)

 

Front Controller (gather redundant request processing into single component)

 

Struts Framework implementation of Front Controller

Hosted by www.Geocities.ws

1