• No results found

Web Applications. Originals of Slides and Source Code for Examples:

N/A
N/A
Protected

Academic year: 2021

Share "Web Applications. Originals of Slides and Source Code for Examples:"

Copied!
23
0
0

Loading.... (view fulltext now)

Full text

(1)

© 2009 Marty Hall

Using and Deploying

Using and Deploying

Web Applications

pp

Originals of Slides and Source Code for Examples:

http://courses.coreservlets.com/Course-Materials/msajsp.html

Customized Java EE Training: http://courses.coreservlets.com/

Servlets, JSP, JSF 1.x & JSF 2.0, Struts Classic & Struts 2, Ajax, GWT, Spring, Hibernate/JPA, Java 5 & 6. Developed and taught by well-known author and developer. At public venues or onsite at your location.

(2)

Agenda

Purpose of Web applications

Structure of Web applications

Setting up Web applications with Tomcat

(3)

Idea of Web Applications

Single directory or file

Servlets, JSP pages, HTML files, utility classes, beans,

tag libraries, etc. are bundled together in a single

directory hierarchy or file

directory hierarchy or file

Common URL prefix

Access to content in the Web app is always through a

pp

y

g

URL that has a common prefix

http://host/

webAppPrefix

/blah/blah

eb ml controls man things

web.xml controls many things

Many aspects of Web application behavior controlled

through

deployment descriptor

(web.xml)

3

through

deployment descriptor

(web.xml)

• The deployment descriptor is covered in detail in the next section.

(4)

Purposes of Web Applications

Organization

Related files grouped together in a single file or directory

hierarchy.

• HTML files, JSP pages, servlets, beans, images, etc.p g g

Portability

All compliant servers support Web apps.

C

d

l

b

i

i

l fil

Can redeploy on new server by moving a single file.

Separation

Each Web app has its own:

Each Web app has its own:

• ServletContext • Class loader • Sessions 4 • Sessions • URL prefix • Directory structure

(5)

Structure of Tomcat (now)

(

)

Tomcat webapps ROOT MyApp WEB-INF Web.xml classes MyApp.class

(6)

How to Call Your Webapp

pp

Give it a public name in web.xml (later)

Let us say it is called serv (see later)

We need the so-called prefix, which here is

j

t

b

M A

just your webapp’s name:MyApp

In your browser you type:

htt //l

lh t 8080/M A

/

(7)

Summary

y

Write your servlet MyApp.java in your src

directory

directory

Compile it with the proper options:

javac -classpath

javac classpath

/usr/local/tomcat/common/lib/servle

t-api.jar -d classes

/M S

l t j

src/MyServlet.java

Copy MyServlet.class into classes in

Tomcat

Tomcat

Put the appropriate web.xml into WEB-INF

Start Tomcat (setting JAVA_HOME)

_

Type in the above URL

(8)

Structure of a Web Application

JSP and regular Web content

(HTML style sheets images etc ):

(HTML, style sheets, images, etc.):

– Main directory or a subdirectory thereof.

Servlets:

WEB INF/classes (if servlet is unpackaged i e in default package)

– WEB-INF/classes (if servlet is unpackaged – i.e. in default package)

– A subdirectory thereof that matches the package name.

Unjarred beans and utility classes:

Same place as servlets (but always use packages!)

Same place as servlets (but always use packages!)

JAR files:

– WEB-INF/lib.

web xml:

web.xml:

– WEB-INF

Tag Library Descriptor files:

WEB INF bdi t th f

8

– WEB-INF or subdirectory thereof

Files in WEB-INF not directly accessible to clients

(9)

Example Deployment Structure

(10)

Making Custom Web Apps

Manually

Manually

1.

Make a directory called app-blank

• app blank/WEB INF/web xml (copy from mine)

• app-blank/WEB-INF/web.xml (copy from mine)

• app-blank/WEB-INF/classes (empty)

2.

Copy/rename

E bl k d ll it A

• E.g., copy app-blank and call it myApp

3.

Put code in proper place in myApp

• Web content (HTML , JSP, images, etc.) goes in the top-level directory (myApp) or any subdirectory other than WEB INF (e g directory (myApp) or any subdirectory other than WEB-INF (e.g., myApp/someDir)

• Servlets and other classes go in a subdirectory of WEB-INF/classes that matches the package name.N /c asses t at atc es t e pac age a e.

4.

Copy app to deployment directory

On Tomcat, entire directory goes in install_dir/webapps

5

Update your CLASSPATH

10

5.

Update your CLASSPATH.

This is where the API library for tomcat resides: This is where the API library for tomcat resides:

(11)

Manual Web App Development

Strategy with Tomcat

Strategy with Tomcat

Development

Keep the original of your Web app directory in your

development directory. Have all the files in the proper

location within that Web app directory.

location within that Web app directory.

Deployment

Copy the entire Web app directory to the server's

py

pp

y

deployment location (e.g., to install_dir/webapps).

• I keep a shortcut to webapps and drag the Web app dir onto the shortcut with the R mouse and then say "Copy".y py

CLASSPATH

Must include the top-level development directory

11

• That now means WEB-INF/classes dir of your Web app

• If your CLASSPATH has "..", you can leave CLASSPATH unchanged as long as you avoid nested packages

(12)

Defining Custom URLs

Java code

package myPackage;

package myPackage; ...

public class MyServlet extends HttpServlet { ... }

web.xml entry (in <web-app...>...</web-app>)

Gi

t

l t

Give name to servlet

<servlet>

<servlet-name>MyName</servlet-name>

<servlet class>myPackage MyServlet</servlet class> <servlet-class>myPackage.MyServlet</servlet-class> </servlet>

Give address (URL mapping) to servlet

<servlet-mapping> <servlet-mapping> <servlet-name>MyName</servlet-name> <url-pattern>/MyAddress</url-pattern> </servlet-mapping> 12 pp g

Resultant URL

http://hostname/webappPrefix/MyAddress

(13)

Defining Custom URLs: Example

(Assume Eclipse Project is "test")

(Assume Eclipse Project is test )

<?xml version="1.0" encoding="UTF-8"?> <web-app

Don't edit this manually. Should refer to version 2.4

<web app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" ... or 2.5 (Tomcat 6 only). version="2.5">

<!-- Use the URL http://hostname/intro/hi2 instead

of http://hostname/intro/servlet/HelloServlet --> of http://hostname/intro/servlet/HelloServlet --> <servlet>

<servlet-name>Second Hello Servlet</servlet-name>

<servlet-class>coreservlets.HelloServlet2</servlet-class> </servlet>

<servlet-mapping>

<servlet-name>Second Hello Servlet</servlet-name> < l tt >/hi2</ l tt >

Any arbitrary name.

But must be the same both times. Fully qualified classname.

13

<url-pattern>/hi2</url-pattern> </servlet-mapping>

(14)

Defining Custom URLs: Result

(15)

Failing to Define Custom URLs

You should always use custom URLs on

d

l

d

j

deployed projects

URLs look cleaner and simpler and shorter

URLs have more meaningful names

URLs have more meaningful names

You don't expose possibly proprietary class file names

You can use web.xml to assign init params later

g

p

• Does not work with …/servlet/myPackage.MyServlet

You can apply filters and security settings later (via

web xml) in a more predictable and controllable manner

web.xml) in a more predictable and controllable manner

Most importantly of all, you can avoid being added to

Marty’s “Hall of Shame”

15

• The kiss of death for any self-respecting Java EE developer

(16)

Mapping URLs to Servlets

Allows you to customize your webapp

i h

hi

h

!

without touching the source!

You don’t have to be a Java programmer here!

Only the class name is in here The container has a

Only the class name is in here. The container has a

specific place where it looks

You can use wildcards in the url-pattern.

The url-pattern is what the client sees

(17)

The Hall of Shame (Deployed Sites

with Ugly

/servlet/

URLs)

with Ugly …/servlet/… URLs)

(18)

The Art of WAR (Files)

Idea

This is a compressed and portable version of your

webapp (it’s really a JAR)

For Tomcat the name of the WAR is the same as the

For Tomcat the name of the WAR is the same as the

name of the webapp.

• All servers must support WAR files

fil

i

l

fil

WAR files are simply ZIP files

JAR up everything from WEB-INF on down.

Building WAR files

Building WAR files

You will get an extra META-INF directory with a

MANIFEST.MF. Don’t worry!

y

You can do it manually with “jar” or a ZIP utility

Deploying WAR files

(19)

Velocity, WebMacro, and Other

Alternatives to JSP Technology

Alternatives to JSP Technology

Issues

Standardization

Portability

Integration

Integration

Industry support

Technical features

Arguments for alternatives focus almost

exclusively on last issue

Even if proponents were right about all their technical

arguments, would that matter?

(20)

Alternatives to JSP Technology:

Integration Issues

Integration Issues

Web apps give standard location for:

S l t JSP d l W b t t

– Servlets, JSP pages, and regular Web content

– Not for Velocity or WebMacro pages

Security settings apply to

– Servlets, JSP pages, and regular Web content

– Not Velocity or WebMacro pages

Initialization parameters defined for

p

– Servlets and JSP pages

– Not Velocity or WebMacro pages

Filters apply to

Filters apply to

– Servlets, JSP pages, and regular Web content

– Not Velocity or WebMacro pages

Listeners apply to

20

Listeners apply to

– Servlets, JSP pages, and regular Web content

(21)

Sharing Data Among Web

Applications

Applications

Failure:

Sessions Each Web app has its own set of sessions

Sessions. Each Web app has its own set of sessions.

Standard ServletContext. Each Web app has a separate one.

Static methods or fields. Each Web app uses a different ClassLoader.

ClassLoader.

Success:

Explicit cookies. Cookies are shared by the whole site (even the whole top-level domain if set appropriately).p pp p y)

Be sure to do

cookie.setPath("/")

, however.

ServletContext associated with a specific URL.

ServletContext myContext =

y

getServletContext();

String url = "/someWebAppPrefix";

ServletContext otherContext =

21

myContext.getContext(url);

Object someData =

(22)

Summary

Web application benefits

Easy organization and deployment

– Easy organization and deployment

– Isolation from other applications

Structure

Top level directory or subdirectory other than WEB INF:

– Top-level directory or subdirectory other than WEB-INF:

• JSP, HTML, other Web content

– WEB-INF

• web.xml

– WEB-INF/classes/directoryMatchingPackage

• Servlets, beans, utilities

.

C

ti

W b

i T

t

Creating a Web app in Tomcat

– Make a directory with proper structure (e.g. WEB-INF and WEB-INF/classes subdirectories)

C t t t di / b

22

(23)

© 2009 Marty Hall

Questions?

Customized Java EE Training: http://courses.coreservlets.com/

Servlets, JSP, JSF 1.x & JSF 2.0, Struts Classic & Struts 2, Ajax, GWT, Spring, Hibernate/JPA, Java 5 & 6. Developed and taught by well-known author and developer. At public venues or onsite at your location.

References

Related documents

The aim of this thesis is to develop a software that, using an existing mathematical model [2], allows for a simulation of the behavior of a type 1 diabetic regarding his glucose

Using constructivist learning theory we examined where students gain their confidence when solving problems, what role peers and instructors had in facilitating problem

 “CDPHE …to plan for performing updates to the state’s greenhouse gas emissions inventory, with the first update scheduled to be completed no later than 2012 and. repeated

Words and Music by FRANK SULLIVAN and JIM PETERIK Arranged by JOHNNIE VINSON.. EYE OF

The SW algorithm computes the optimal local alignment between two sequences following a dynamic programming approach and can be divided in two stages: (1) similarity matrix (also

We present a method called Causal Relationship- based Economical Action Mining (CREAM) that, given an object and the causal network, predicts optimal (i.e., maximally

Marine Air Ground Task Force (MAGTF) Command, Control, Communications (PM MC3) provides the warfighter a full spectrum of equipment and systems to sustain current and

Il presente modulo è da ritornare, debitamente compilato e corredato degli allegati richiesti, al seguente indirizzo: Istituto delle assicurazioni sociali, Settore