Web Component Development and JSP technologies session 08

31
Ver. 1.0 Slide 1 of 31 Web Component Development With Servlet and JSP™ Technologies Objectives In this session, you will learn to: Describe the purpose of DD Determine the context root for a web application Create servlet mappings to allow invocation of a servlet Create and access context and init parameters Use the @WebServlet and @WebInitParam annotations to configure your servlets

Transcript of Web Component Development and JSP technologies session 08

Page 1: Web Component Development and JSP technologies  session 08

Ver. 1.0 Slide 1 of 31

Web Component Development With Servlet and JSP™ TechnologiesObjectives

In this session, you will learn to:Describe the purpose of DDDetermine the context root for a web applicationCreate servlet mappings to allow invocation of a servletCreate and access context and init parametersUse the @WebServlet and @WebInitParam annotations to configure your servlets

Page 2: Web Component Development and JSP technologies  session 08

Ver. 1.0 Slide 2 of 31

Web Component Development With Servlet and JSP™ Technologies

Java uses a JAR to package multiple class and resource files into a single file.The JAR technique is used to support deployment of all kinds of libraries and applications in a web container according to the Java EE specification.The JAR file that is used to package a web application has a specific set of structural and content rules.The JAR file is given the .war extension.

Packaging Web Applications

Page 3: Web Component Development and JSP technologies  session 08

Ver. 1.0 Slide 3 of 31

Web Component Development With Servlet and JSP™ Technologies

Java EE web containers supports multiple web applications, therefore, each application is given a context root. The context root is the base URL that is used as a prefix when locating pages and servlets within the application.

The Default Context Root

Page 4: Web Component Development and JSP technologies  session 08

Ver. 1.0 Slide 4 of 31

Web Component Development With Servlet and JSP™ Technologies

For example, if a war file called an AnApplication.war contains a file startingPoint.html in the root of the archive, then when deployed to a local server, the URL used to reach the startingPoint.html file may be:

http://localhost:8080/AnApplication/startingPoint.htmlThe context root of the preceding URL is /AnApplication.

The Default Context Root (Contd.)

Page 5: Web Component Development and JSP technologies  session 08

Ver. 1.0 Slide 5 of 31

Web Component Development With Servlet and JSP™ Technologies

The HTML files, JSPs, images, and other resources can be placed in the root of the archive and the directories below the root.A special directory called WEB-INF is also placed in the root of the archive.Inside WEB-INF is a file called web.xml. This file is called the DD. The web.xml file:

Is an XML structured file.Provides configuration information for the application and its components. Is the place where instructions can be given to make the resources under WEB-INF available.

Essential Structure of a WAR File

Page 6: Web Component Development and JSP technologies  session 08

Ver. 1.0 Slide 6 of 31

Web Component Development With Servlet and JSP™ Technologies

Java classes can be placed under the WEB-INF/classes path.Library JARs can be placed under the WEB-INF/lib path.Resources, such as HTML pages and images, located in the META-INF/resources directory of JAR files that are placed in WEB-INF/lib directories, are also available in the application.

Essential Structure of a WAR File (Contd.)

Page 7: Web Component Development and JSP technologies  session 08

Ver. 1.0 Slide 7 of 31

Web Component Development With Servlet and JSP™ Technologies

The following figure shows the structure of a WAR file.

Essential Structure of a WAR File (Contd.)

Page 8: Web Component Development and JSP technologies  session 08

Ver. 1.0 Slide 8 of 31

Web Component Development With Servlet and JSP™ Technologies

In prior versions of Java EE, deployment configuration was supported using the web.xml file. This file is required and is generally known as the DD.This type of external DD facilitates the reuse of code in differing environments.It overcomes the problems arising from frequent change in hard-coded values.However, it may be inconvenient to have configuration information maintained in a separate file.Therefore, in Java EE, many configuration items can be specified using annotations embedded in the source file.Another method of specifying deployment information is through the use of web-fragment.xml files in libraries.

Deployment Descriptors

Page 9: Web Component Development and JSP technologies  session 08

Ver. 1.0 Slide 9 of 31

Web Component Development With Servlet and JSP™ Technologies

The web-fragment.xml file provides a mechanism to include deployment information that is specific to a library, without having to edit the main DD for the whole web application.A web-fragment.xml file is located under the META-INF directory of a JAR file placed within the WEB-INF/lib directory of a WAR archive.This file is read and the configuration contained within it is integrated into the configuration of the web application as a whole.

The web-fragment.xml Files

Page 10: Web Component Development and JSP technologies  session 08

Ver. 1.0 Slide 10 of 31

Web Component Development With Servlet and JSP™ Technologies

With three elements capable of providing configuration information, web.xml, web-fragment.xml, and annotations, it is possible for these to provide conflicting information.To address this problem, the following rules and controls are provided:

Precedence<metadata-complete> tagOrdering

Controlling Configuration

Page 11: Web Component Development and JSP technologies  session 08

Ver. 1.0 Slide 11 of 31

Web Component Development With Servlet and JSP™ Technologies

The <metadata-complete> tag may be used to indicate that an XML file specifies everything related to the current item being configured.The <metadata-complete> tag in a web.xml file will prevent any web-fragment.xml files or annotations being used to configure the application.In a webfragment.xml file, this tag indicates that no annotations or other webfragment.xml files should be applied to the configuration of this library.You can apply a <name> tag to an XML file, and using that name, specify either an <absolute-ordering> or an <ordering> tag to indicate the order in which configuration elements should be applied.

Controlling Configuration (Contd.)

Page 12: Web Component Development and JSP technologies  session 08

Ver. 1.0 Slide 12 of 31

Web Component Development With Servlet and JSP™ Technologies

Java EE adds features that allow the programmatic configuration of servlets, filters, and related components during the initialization of the servlet context.

Dynamic Configuration of Web Applications

Page 13: Web Component Development and JSP technologies  session 08

Ver. 1.0 Slide 13 of 31

Web Component Development With Servlet and JSP™ Technologies

HTML and JSP pages that exist outside WEB-INF are located using a URL that maps through the context root and down to the specific file that is being requested.However, to access a servlet, a mapping must be created.A mapping is an information that specifies that a particular URL, or group of URLs, refer to a particular named servlet.The name is not the class name, but is an internal reference within the web container to a particular servlet class configured in a particular way.

Servlet Mapping

Page 14: Web Component Development and JSP technologies  session 08

Ver. 1.0 Slide 14 of 31

Web Component Development With Servlet and JSP™ Technologies

The following code illustrates the servlet mapping in the web.xml file:<web-app [...] ><servlet><servlet-name>MyServletName</servlet-name><servlet-class>SL314.package.MyServlet</servlet-class></servlet><servlet-mapping><servlet-name>MyServletName</servlet-name><url-pattern>/MyServlet</url-pattern></servlet-mapping></web-app>

Servlet Mapping (Contd.)

Page 15: Web Component Development and JSP technologies  session 08

Ver. 1.0 Slide 15 of 31

Web Component Development With Servlet and JSP™ Technologies

A single servlet can be used to respond to a variety of URLs.This can be supported in the following two ways:

The <url-pattern> XML element may be repeated.The pattern specified by a URL may be a wildcard, ending with an asterisk.

For example, consider the following servlet mapping code:<servlet-mapping><servlet-name>MyServlet</servlet-name><url-pattern>/MyServlet</url-pattern><url-pattern>/YourServlet</url-pattern><url-pattern>/HisServlet/*</url-pattern></servlet-mapping>

Multiple and Wildcard URL Patterns

Page 16: Web Component Development and JSP technologies  session 08

Ver. 1.0 Slide 16 of 31

Web Component Development With Servlet and JSP™ Technologies

For the preceding mapping code, the servlet would respond to any of the following URLs:

http://localhost:8080/MyAppRoot/MyServlethttp://localhost:8080/MyAppRoot/YourServlethttp://localhost:8080/MyAppRoot/HisServlethttp://localhost:8080/MyAppRoot/HisServlet/onehttp://localhost:8080/MyAppRoot/HisServlet/twenty

Multiple and Wildcard URL Patterns (Contd.)

Page 17: Web Component Development and JSP technologies  session 08

Ver. 1.0 Slide 17 of 31

Web Component Development With Servlet and JSP™ Technologies

Servlet mappings can be achieved using annotations.To perform servlet mapping, a servlet should be given the javax.servlet.annotation.WebServlet annotation.

Mapping Using Annotations

Page 18: Web Component Development and JSP technologies  session 08

Ver. 1.0 Slide 18 of 31

Web Component Development With Servlet and JSP™ Technologies

A servlet can be invoked directly by its name through a RequestDispatcher, in a forward or include call. The necessary dispatcher is obtained using the servlet context which offers a getNamedDispatcher(String)method.

Invoking a Servlet by Name

Page 19: Web Component Development and JSP technologies  session 08

Ver. 1.0 Slide 19 of 31

Web Component Development With Servlet and JSP™ Technologies

The DD can include configuration information for the servlet.Two configuration elements of frequent utility are context parameters and init parameters.

Servlet Context Information

Page 20: Web Component Development and JSP technologies  session 08

Ver. 1.0 Slide 20 of 31

Web Component Development With Servlet and JSP™ Technologies

The servlet context term refers to the web application and the container in which the servlet runs.The javax.servlet.ServletContext interface provides the following two methods that allow read-only access to a map of context parameters:

String getInitParameter(String name)Enumeration<String> getInitParameterNames()

Context Parameters

Page 21: Web Component Development and JSP technologies  session 08

Ver. 1.0 Slide 21 of 31

Web Component Development With Servlet and JSP™ Technologies

Context parameters cannot be supplied using annotations, because they relate to the entire web application, rather than to a single element of it.To specify a context parameter using a web.xml, or web-fragment.xml file, the specification is quite simple.For example, the following code represents aweb-fragment.xml file specifying a context parameter:<web-fragment [...]><context-param><param-name>fragmentContext</param-name><param-value>fragment Context value</param-value></context-param></web-fragment>

Supplying Context Parameters

Page 22: Web Component Development and JSP technologies  session 08

Ver. 1.0 Slide 22 of 31

Web Component Development With Servlet and JSP™ Technologies

Context parameters can be read in Java code using statements of the following form:String paramValue =this.getServletContext().getInitParameter("paramName");

Reading Context Parameters

Page 23: Web Component Development and JSP technologies  session 08

Ver. 1.0 Slide 23 of 31

Web Component Development With Servlet and JSP™ Technologies

The init parameters are the initialization parameters that are associated with the servlet individually.For example, this association can be achieved using the DD file, as shown in the following code:<servlet><servlet-name>MyServlet</servlet-name><servlet-class>SL314.package.MyServlet</servlet-class><init-param><param-name>name</param-name><param-value>Fred Jones</param-value></init-param><init-param></servlet>

Servlet Initialization Parameters

Page 24: Web Component Development and JSP technologies  session 08

Ver. 1.0 Slide 24 of 31

Web Component Development With Servlet and JSP™ Technologies

The init parameters are associated directly with the servlet, therefore, annotations can be used to specify the servlet initialization parameter.For example:@WebServlet(name=”MyServlet”,urlPatterns=({“/MyServ”}),initParams ={@WebInitParam(name="name", value="Fred Jones")})

Servlet Initialization Parameters (Contd.)

Page 25: Web Component Development and JSP technologies  session 08

Ver. 1.0 Slide 25 of 31

Web Component Development With Servlet and JSP™ Technologies

The servlet itself provides direct access to servlet initialization parameters using the getInitParameter(String) method.For example, the two ways to read servlet initialization parameters are:

name = this.getInitParameter("name");name = getServletConfig().getInitParameter("name");

Reading Servlet Initialization Parameters

Page 26: Web Component Development and JSP technologies  session 08

Ver. 1.0 Slide 26 of 31

Web Component Development With Servlet and JSP™ Technologies

The DD can contain elements of many other types.Similarly, several other annotations exist for configuration purposes.You should know that the DD is used to configure security, and connections to other aspects of a Java EE system, such as Enterprise JavaBeans (EJB),message queues, and databases.

Other Configuration Elements

Page 27: Web Component Development and JSP technologies  session 08

Ver. 1.0 Slide 27 of 31

Web Component Development With Servlet and JSP™ Technologies

Investigate the deployment descriptor and the container’s servlet support.

Demo 5-1: Container Facilities For Servlets & JSPs

Page 28: Web Component Development and JSP technologies  session 08

Ver. 1.0 Slide 28 of 31

Web Component Development With Servlet and JSP™ Technologies

Solution:1. Investigate projects using web.xml.2. Code the servlet to use the configuration information.3. Prepare the index page and run the application.

Demo 5-1: Container Facilities For Servlets & JSPs (Contd.)

Page 29: Web Component Development and JSP technologies  session 08

Ver. 1.0 Slide 29 of 31

Web Component Development With Servlet and JSP™ Technologies

In this session, you learned that:The JAR technique is used to support deployment of all kinds of libraries and applications in a web container according to the Java EE specification.The JAR file is given the .war extension.The context root is the base URL that is used as a prefix when locating pages and servlets within the application. The web-fragment.xml file provides a mechanism to include deployment information that is specific to a library, without having to edit the main DD for the whole web application.A mapping is an information that specifies that a particular URL, or group of URLs, refer to a particular named servlet.

Summary

Page 30: Web Component Development and JSP technologies  session 08

Ver. 1.0 Slide 30 of 31

Web Component Development With Servlet and JSP™ Technologies

The javax.servlet.ServletContext interface provides the following two methods that allow read-only access to a map of context parameters:

String getInitParameter(String name)Enumeration<String> getInitParameterNames()

Summary (Contd.)

Page 31: Web Component Development and JSP technologies  session 08

Ver. 1.0 Slide 31 of 31

Web Component Development With Servlet and JSP™ Technologies

The next session covers Lab@Home. In this session, you need to perform exercise given in the following table.

Ensure to take the required input files from the faculty for performing this exercise.

What’s Next?

Chapter Number Exercise NumberChapter 3 (Book 4) Exercise 1