Thursday 14 May 2015

OCEJWCD - 5.Container facilities for servlets and JSPs (part1)

OCEJWCD (SCWCD) - 1Z0-899 - Web Component Developer Certification

5.1. Understand the purpose and structure of deployment descriptors

Deployment descriptor(web.xml) is a XML file that contains the configuration parameters for an application. This file is a JavaEE standard that describes the contents of a WAR file.

Note: a WAR file is a Web Application Archive, is basically a JAR file used to contain JSPs, servlets, java classes,XML, tag libraries, and all the resources that constitute a web application.

The web.xml file is located directly under WEB-INF directory.

Basic structure of the deployment descriptor (web.xml):

Namespace declaration and schema location
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns="http://java.sun.com/xml/ns/javaee" 
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
 http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">

Welcome files
A welcome file is invoked automatically by the server if no file name is specified. By default the list is as follows:
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>

Session timeout
<session-config>
  <session-timeout>30</session-timeout>
</session-config>

Mime mapping
When serving static resources, ther server generates a content-type header based on these mappings.

<mime-mapping>
  <extension>abs</extension>
  <mime-type>audio/x-mpeg</mime-type>
</mime-mapping>
In the following topics we will include more attributes that are defined in DD.

5.2. Control context root and servlet mapping

The context root for an application is determined by how it is deployed. 
A context root identifies a web application in a JavaEE server.

<module>
  <web>
    <web-uri>myWebapp.war</web-uri>
    <context-root>myapp</context-root>
  </web>
</module>

The context root is specified when the web module is deployed, it must start with "/" ending with a string. It is matched against the context roots of other web applications hosted on this container.

To obtain context root programatically:
ServletContext getContext(String uripath)
Returns a ServletContext object that corresponds to a specified URL on the server.
Servlet mapping
As developers the servlet will be identify with the servlet name. The servlet class defines the actual location of the servlet file (including package) and the url pattern will be the address displayed in the URL.
  <servlet>
    <servlet-name>MyServlet</servlet-name>
    <servlet-class>example.MyServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>MyServlet</servlet-name>
    <url-pattern>/MyServlet</url-pattern>
  </servlet-mapping> 
See next post: Container facilities for servlets and JSPs (part2)

OCEJWCD (SCWCD) - 1Z0-899 - Web Component Developer Certification

No comments:

Post a Comment