1 dickson k.w. chiu phd, smieee reference: sun j2ee 1.4 tutorial csit600b: xml programming xml...

21
1 Dickson K.W. Chiu PhD, SMIEEE Reference: Sun J2EE 1.4 Tutorial CSIT600b: XML Programming XML Programming Guide: Getting Started

Post on 21-Dec-2015

221 views

Category:

Documents


1 download

TRANSCRIPT

1

Dickson K.W. ChiuPhD, SMIEEE

Reference: Sun J2EE 1.4 Tutorial

CSIT600b: XML ProgrammingXML Programming Guide:

Getting Started

Dickson Chiu 2004 CSIT600b 1p-2

J2EE Environment – a Standard

Distributed multi-tiered applications Widely deployed and supported by many products

IBM Websphere, Sun Java studio, Borland Jbuilder, etc.

Dickson Chiu 2004 CSIT600b 1p-3

J2EE Containers and API

Container = platform / runtime environment

Dickson Chiu 2004 CSIT600b 1p-4

Simplified Systems Integration Platform-independent, not to lock customers into their

technologies The J2EE APIs enable systems and applications integration

through the following: Unified application model across tiers with enterprise beans Simplified request-and-response mechanism with JSP pages

and servlets Reliable security model with JAAS XML-based data interchange integration with JAXP, SAAJ,

and JAX-RPC Simplified interoperability with the J2EE Connector

architecture Easy database connectivity with the JDBC API Enterprise application integration with message-driven

beans and JMS, JTA, and JNDI

Dickson Chiu 2004 CSIT600b 1p-5

Packaging J2EE Application

Deployment Descriptors can be changed for software configuration without changing the source code

Dickson Chiu 2004 CSIT600b 1p-6

Java Web Application Overview Servlets are basics Web Components in

web containers

Dickson Chiu 2004 CSIT600b 1p-7

Advantages of Java Servlet

Performance: VM always running, servlet objects persist instead of continually created/destroyed as CGI are

Stateful: can more easily track session information since same servlet object used

Portable: since written in Java Security: SecurityManager can constraint

servlets in manner similar to applets Servlets also have all the advantages of Java:

runtime safety, built on extensive class libraries, networking, threads, etc.

Dickson Chiu 2004 CSIT600b 1p-8

javax.servlet package Servlet interface: declares servlet

methods (init, service, etc.) GenericServlet implements Servlet

HttpServlet subclass adds features specific to HTTP

Technically, an servlet is a program that extends either GenericServlet or HttpServlet.

Servlets

GenericServlet

HttpServlet

YourServlet

Dickson Chiu 2004 CSIT600b 1p-9

javax.servlet.http Package

Methods HTTP Requests Comments

doGet GET, HEAD Usually overridden

doPost POST Usually overridden

doPut PUT Usually not overridden

doOptions OPTIONS Almost never overridden

doTrace TRACE Almost never overridden

HTTP requests include – GET (default), conditional GET, HEAD, POST, PUT, DELETE, TRACE, OPTIONS

All methods take two arguments: an HttpServletRequest object an HttpServletResponse object

Return a BAD_REQUEST (400) error by default (i.e., if you don’t have your own implementation)

Implement your own

Dickson Chiu 2004 CSIT600b 1p-10

Java Servlet Example – Hello World

package servlets;import java.io.*;import java.util.*;import java.sql.*;import javax.servlet.*;import javax.servlet.http.*;public class GreetingServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); response.setBufferSize(8192); PrintWriter out = response.getWriter(); // then write the data of the response out.println("<html>" + "<head><title>Hello</title></head>"); out.println("<body bgcolor=\"#ffffff\">" + "<img src=\"duke.waving.gif\" alt=\"Duke waving\">" + "<h2>Hello, my name is Duke. What's yours?</h2>" + "<form method=\"get\">" + "<input type=\"text\" name=\"username\" size=\"25\">" + "<p></p>" + "<input type=\"submit\" value=\"Submit\">" + "<input type=\"reset\" value=\"Reset\">" + "</form>");

Specify target page if

necessary

Dickson Chiu 2004 CSIT600b 1p-11

Java Servlet Example – contString username = request.getParameter("username");

if ((username != null) && (username.length() > 0)) { RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/response"); if (dispatcher != null) { dispatcher.include(request, response); } } out.println("</body></html>"); out.close(); }

public String getServletInfo() { return "The Hello servlet says hello."; }}

Stuff the output of another servlet to here

Dickson Chiu 2004 CSIT600b 1p-12

Java Servelet Example - Response

// import not shown herepublic class ResponseServlet extends HttpServlet { public void doGet(HttpServletRequest request,

HttpServletResponse response) throws ServletException, IOException { PrintWriter out = response.getWriter(); // then write the data of the response String username = request.getParameter("username"); if ((username != null) && (username.length() > 0)) { out.println("<h2>Hello, " + username + "!</h2>"); } }

public String getServletInfo() { return "The Response servlet says hello."; }}

Parameters passed with request / response

objects

Dickson Chiu 2004 CSIT600b 1p-13

Servlet Life Cycle

Servlets are controlled by servers A server loads and initializes the servlet The servlet handles zero

or more client requests The server terminates the servlet

Dickson Chiu 2004 CSIT600b 1p-14

Servlet Life Cycle (2)The server will automatically called : public void init():

Called only once when serlvet is being created.

Good place for set up, open Database, etc.

public void service(): Called once for each request. In HttpServlet, it delegates

requests to doGet, doPost, etc. public void destroy():

Called when server decides to terminate the serverlet.

Release resources.

Dickson Chiu 2004 CSIT600b 1p-15

Environment Installation Visit: http://java.sun.com/j2ee/1.4/download.html Download and install J2EE 1.4 / JEE5 at say:

H:\Sun\AppServer\ Start the default server from the program menu. Download and extract the J2EE 1.4 examples update 2 (.zip

file) under the same root: H:\Sun\AppServer\ The main references text are inside:

H:\Sun\AppServer\j2eetutorial14\doc Very Important - edit the file:

H:\Sun\AppServer\j2eetutorial14\examples\common\build.properties

set the first line: j2ee.home=H:/Sun/AppServer/ Set the port if you have changed the default one

You may need a text editor (e.g. wordpad) other than notepad to convert UNIX file formats (line terminator problem)

Dickson Chiu 2004 CSIT600b 1p-16

Compiling hello2

In a terminal window, go to H:/Sun/AppServer/j2eetutorial14/examples/web/hello2/

Run asant build. This target will compile the servlets to the directory H:/Sun/AppServer/j2eetutorial14/examples/web/hello2/build/

Double check if your environment path includes H:/Sun/AppServer/bin

asant is a build tool conceptually similar to makefile but modernize with XML

See apache ant documentation too: http://ant.apache.org/

Dickson Chiu 2004 CSIT600b 1p-17

For JEE5

Edit the web.xml / sun-web.xml file if necessary

Create the .war file with: asant create-war You may open it with Winzip or Winrar

and you can add other files into it manually.

Deploy the .war with the web-based admin tool copy the .war to the auto-deploy directory run: asant deploy-war

Dickson Chiu 2004 CSIT600b 1p-18

Creating the .WAR file and Descriptors

Start deploytool from the program menu Create a Web application called hello2 by running the

New Web Component wizard. Select File->New->Web Component

In the New Web Component wizard: Select the Create New Stand-Alone WAR Module radio button. In the WAR Location field, enter

H:/Sun/AppServer/j2eetutorial14/examples/web/hello2/hello2.war

In the WAR Name field, enter hello2 In the Context Root field, enter /hello2 Click Edit Contents to add the content files In the Edit Contents dialog box, navigate to

H:/Sun/AppServer/j2eetutorial14/examples/web/hello2/build/. Select duke.waving.gif and the servlets package and click

Add. Click OK. Click Next. Select the Servlet radio button. Click Next. Select GreetingServlet from the Servlet Class combo box. Click Finish.

Dickson Chiu 2004 CSIT600b 1p-19

Configure the Second Servlet

Select File New Web Component. Click the Add to Existing WAR Module radio

button and select hello2 from the combo box. Because the WAR contains all the servlet classes, you do not have to add any more content.

Click Next. Select the Servlet radio button. Click Next. Select ResponseServlet from the Servlet Class

combo box. Click Finish.

Dickson Chiu 2004 CSIT600b 1p-20

Setting Aliases Before Running Select the GreetingServlet Web component.

Select the Aliases tab. Click Add to add a new mapping. Type /greeting in the aliases list. Select the ResponseServlet Web component. Click Add. Type /response in the aliases list. Select File Save.

Deploy the Web module Select the WAR file hello2

Select from menu: tools -> deploy You need the admin password

Running - open the URL in a browser: http://localhost:8080/hello2/greeting

Dickson Chiu 2004 CSIT600b 1p-21

Changing Your Program Recompile with asant build Command Line

Run: asant undeploy-war Run: asant deploy-war

Use admin tool to redeploy copy the .war to the auto-deploy directory

In the deploytool Select hello2.war Click edit content Add the changed class files, overwriting the old one Save changes Deploy again