servlet-name

Download Report

Transcript servlet-name

Struts Deployment
Objectives:
1. Investigate the Web application
deployment descriptor
2. Install and deploy Tomcat
and struts
3. Design and deploy a struts
application
Topics







To learn how to create and deploy WAR files
Deployment Descriptor (web.xml)
Installing and Configuring Tomcat
Installing Struts
The struts-config.xml file
Designing a Struts Application
Deploying a Struts Application
Packaging a Web
Application for
Deployment
 Servlet Specification 2.2 specified the use of a
single web archive file - *.war
An extension of the .jar file
Has the same form as the .zip file (.jar and .war)
Why use a WAR file?
 Simplify deployment
Easy to install
Single file to each server in cluster
Improve security
No access between Web applications
Packaging for third-party applications
Structure of a WAR File
JSP pages, HTML documents, image files
app.war
Content
directories
JSP pages, HTML documents, image files
web.xml
WEB-INF
classes
Class files
beans
Package
directories
lib
tlds
Class files
JAR files
TLD files
Creating a WAR file
 Use the jar command line tool
 Use .war for the extension of the file name
Accessing the WAR File
 All WAR contents are associated with a top-level
URL directory:
http://myserver.com/app/…
URLs for assets in top-level and content
directories are assigned automatically.
URLs for WEB-INF assets must be explicitly
specified.
Configuring WEB-INF
Assets
 Primarily controlled via the deployment
descriptor:
WEB-INF/web.xml
Deployment descriptor is an XML document:
<?xml version=”1.0” encoding=”UTF-8” ?>
<!DOCTYPE web-app
PUBLIC -//Sun Microsystems, Inc.//DTD Web Application
2.2//EN"
"http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">
Has the root element of:
<web-app>
…
Elements of a Web
Application
 Application configuration
 Context parameters
 Servlet configuration
 Session Configuration
 Servlet Mapping
 MIME types
 Default pages
 Custom Tag Libraries
Application Configuration
<icon>
<smallicon>wdk/widget/image/illustration/button/dctmlogo16
x16.gif</small-icon>
<largeicon>wdk/widget/image/illustration/button/dctmlogo32
x32.gif</large-icon>
</icon>
<display-name>Sample Library Services Client
</display-name>
<description>This web application provides an example of
how to leverage library services.</description>
Context Parameters
 Name/value pairs that become available in the
ServletContext object using the
getInitParameter() method
<context-param>
<param-name>dbUser</param-name>
<param-value>joe</param-value>
</context-param>
<context-param>
<param-name>dbPwd</param-name>
<param-value>zebra</param-value>
</context-param>
Servlet Configuration
 A web application’s servlets are specified in the
deployment descriptor via the <servlet> tag and its
subelements
 Mandatory tags for each servlet definintion:
<servlet-name> tag - specifies a logical name for the
servlet
<servlet-class> tag - specifies the Java class that
implements the servlet
 Optional tags:
<description>, <display-name>, <icon>
 <init-param> parameters passed to the init() method of the
servlet
 <load-on-startup> signifies that the servlet should be
loaded into JVM at container startup. Value of the element
Servlet Configuration
<servlet>
<servlet-name>MyGreatServlet</servlet-name>
<servlet-class>
com.BigCompany.RJServlet
</servlet-class>
<description>Great Servlet</description>
<init-param>
<param-name>GreatnessLevel</param-name>
<param-value>6</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
Servlet Mapping
 Used to hide the implementation of the
application by giving a servlet a logical name in
the form of a URI
<web-apps>
<servlet-mapping>
<servlet-name>MyGreatServlet</servlet-name>
<url-pattern>/GServ</url-pattern>
</servlet-mapping>
</web-apps>
Session Configuration
 Sets the session timeout
<web-app>
…
<session-config>
<session-timeout>60</session-timeout>
</session-config>
…
</web-app>
MIME Types
 Maps file extensions to MIME Types
<mime-mapping>
<extension>pdf</extension>
<mime-type>application/pdf</mime-type>
</mime-mapping>
<mime-mapping>
<extension>html</extension>
<mime-type>text/html</mime-type>
</mime-mapping>
Default Pages and
Distributable Servlets
 <welcome-file-list>
<welcome-file>default.html</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file-list>
<distributable/>
 <welcome-file-list> specifies which file within an
application directory should be displayed when a URL is
requested that contains only a directory
 <distributable/>
has no content
signals whether an application can run in multiple JSP
containers simultaneously
Custom Tag Libraries
<webapp>
…
<taglib>
<taglib-uri>/greatTags</taglib-uri>
<taglib-location>/WEBINF/tlds/greatTags_1_0.tld</taglib-location>
</taglib>
…
</webapp>
Tomcat JSP/Servlet
Container
 Open-source Java-based Web application
container
 Initially through the Jakarta project of the Apache
Software Foundation
 Runs servlets on Catalina container portion
 Runs JSP on Jasper container portion
 Sun’s reference implementation for servlet and
JSP specifications
 http://tomcat.apache.org
Installing and Configuring
Tomcat
 Make sure a current and compatible version of
the Java SDK, Standard Edition is installed
 Acquired at http://java.sun.com/javase
 Extract Tomcat server from downloaded archive
 Set JAVA_HOME to location of Java SE
installation
Testing Tomcat
Installation
 Open up a command prompt window or shell
 Navigate to the Tomcat installation directory
 Start the Tomcat server
bin\startup
 Open your browser and type in the following
URL
http://localhost:8080
Verify JSP Container
Operation
 Select JSP Examples link
Jakarta Struts Project
 Open-source Java-based Web application
development framework
 Initially developed through the Jakarta project of
the Apache Software Foundation
 Provides control layer based on standard
technologies
 Servlets
 JavaBeans
 ResourceBundles
 XML
http://struts.apache.org
Supporting Web
Applications with Struts
Extract Struts files from downloaded archive
 For each Web Application
 Copy JAR files to /WEB-INF/lib directory
 Make sure a web.xml file exists in /WEB-INF
 Create a struts-config.xml file and store in
/WEB-INF
• Deployment descriptor for Struts applications
• Integrates the MVC components into a working
application
Struts and JBoss
 Go to http://struts.apache.org
 Download version 1 release
 Extract Struts files from downloaded archive
 Copy struts-taglib and struts-core file to:
 $JBOSS/server/all/deploy/jboss-web.deployer
 $JBOSS/server/all/lib
 Copy all commons*.jar files to:
 $JBOSS/server/all/deploy/jboss-web.deployer
 When creating Web application:
 copy struts tlds to WEB-INF/tlds directory in
application war file
 create appropriate web.xml file
 add struts-config.xml file to WEB-INF directory
A basic struts-config.xml
file
<?xml version=“1.0” encoding=“ISO-8859-1” ?>
<!DOCTYPE struts-config PUBLIC “-//Apache Software
Foundation//DTD Struts Configuration 1.3//EN”
“http://jakarta.apache.org/struts/dtds/strutsconfig_1_3.dtd”>
<struts-config>
<message-resources
parameter=“company.ApplicationResources” />
</struts-config>
Tag-Library Descriptors
To use specialized struts tags
 Specify a taglib entry in web.xml
 Copy struts-html.tld to /WEB-INF/tlds
<taglib>
<taglib-uri>/struts-html.tld</taglib-uri>
<taglib-location>/WEB-INF/tlds/strutshtml.tld</taglib-location>
</taglib>
Steps for Designing a
Struts Application
Define and create all the Views
 Add ActionForms used to support views
Create the controller components
 ActionServlet
 Action
Define View-Controller relationships in strutsconfig.xml
Describe the struts components to the Web server
 web.xml
Run the application
Creating the Views
 Views composed of
HTML
JSP
Struts tag libraries
• Bean
• HTML
• Logic
Struts-specific Form Tags
 The struts HTML tag library offers strutsspecific functionality
<html:form action=“/Lookup.do">
Product ID: <html:text
property="product" /><br>
<html:submit/>
</html:form>
Flow of Control
 Upon submission of JSP View, ActionForm object
will be created, populated with the request
parameters, and stored in the session.
 The action referenced by the <html:form/> will be
invoked and passed a reference to the populated
ActionForm.
ActionForm
 Properties are populated by request parameters
of the same name
 Struts uses JavaBean reflection
 Design patterns must be followed
private String product;
public void setProduct(String prod);
public String getProduct();
 reset() method restores baseline state
Define the Form Bean
 Make the form bean known to the struts
framework
 Declare the following element in struts-config.xml
<form-beans>
<form-bean name="lookupForm"
type="ssps.LookupForm"/>
</form-beans>
Create the Controller
 Composed of two components
 A single implementation of
org.apache.struts.action.ActionServlet
• Dispatching component
 One or more implementations of
org.apache.struts.action.Action
• Performs business logic
• Entry point is execute() method
Controller-View Flow
 Within the execute() method, targets are
determined which dictate the next page in the Web
application sequence.
The execute() method returns an instance of
ActionForward
..
target = new String(“success”);
return (mapping.findForward(target));
Deploying Actions
 Add an entry to the <action-mappings> section of strutsconfig.xml
<action path="/Lookup"
type="ssps.LookupAction"
name="lookupForm”
input="/prompt.jsp">
<forward name="success" path="/results.jsp"/>
<forward name="failure" path="/prompt.jsp"/>
</action>
Deploy the Struts
Application
 Define the ActionServlet to the Web application
 Inform the ActionServlet of the location of
struts-config.xml
 Specify that the ActionServlet is preloaded
Deploy the Struts
Application – web.xml
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>
org.apache.struts.action.ActionServlet
</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</paramvalue>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
Deploy the Struts
Application – web.xml
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
Review
To learn how to create and deploy WAR files
 Deployment Descriptor (web.xml)
 Installing and Configuring Tomcat
 Installing Struts
 The struts-config.xml file
 Designing a Struts Application
 Deploying a Struts Application