ppt - Teaching Web Server

Download Report

Transcript ppt - Teaching Web Server

CSIT600b: XML Programming
XML Programming Guide:
Getting Started
Dickson K.W. Chiu
PhD, SMIEEE
Reference: Sun J2EE 1.4 Tutorial
1
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-2
J2EE Containers and API
Container = platform / runtime environment
Dickson Chiu 2004
CSIT600b 1p-3
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 JAXRPC
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-4
Packaging J2EE Application

Deployment Descriptors can be changed for software
configuration without changing the source code
Dickson Chiu 2004
CSIT600b 1p-5
Java Web Application Overview


Servlets are basics
Web Components in
web containers
Dickson Chiu 2004
CSIT600b 1p-6
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-7
javax.servlet package




Servlet interface: declares servlet
Servlets
methods (init, service, etc.)
GenericServlet implements
GenericServlet
Servlet
HttpServlet subclass adds
features specific to HTTP
HttpServlet
Technically, an servlet is a program
that extends either GenericServlet
or HttpServlet.
YourServlet
Dickson Chiu 2004
CSIT600b 1p-8
Implement
your own
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)
Dickson Chiu 2004
CSIT600b 1p-9
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\">" +
Specify target
"<h2>Hello, my name is Duke. What's yours?</h2>" +
page if necessary
"<form method=\"get\">" +
"<input type=\"text\" name=\"username\" size=\"25\">" + "<p></p>" +
"<input type=\"submit\" value=\"Submit\">" +
"<input type=\"reset\" value=\"Reset\">" + "</form>");
Dickson Chiu 2004
CSIT600b 1p-10
Java Servlet Example – cont
String 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>");
Stuff the output of another
out.close();
servlet to here
}
public String getServletInfo() {
return "The Hello servlet says hello.";
}
}
Dickson Chiu 2004
CSIT600b 1p-11
Java Servelet Example - Response
// import not shown here
public class ResponseServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse
response)
Parameters passed with
throws ServletException, IOException {
request / response objects
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.";
}
Dickson Chiu 2004
CSIT600b 1p-12
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-13
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-14
Environment Installation






Visit: http://java.sun.com/j2ee/1.4/download.html
Download and install J2EE 1.4 at say:
H:\Sun\AppServer\
Start the default server from the program menu.
Download and extract the J2EE 1.4 examples (.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\buil
d.properties


set the first line: j2ee.home=H:/Sun/AppServer/
You may need a text editor other than notepad to
convert UNIX file formats (line terminator problem)
Dickson Chiu 2004
CSIT600b 1p-15
Compiling hello2

In a terminal window, go to
H:/Sun/AppServer/j2eetutorial14/examples/w
eb/hello2/

Run asant build. This target will compile
the servlets to the directory
H:/Sun/AppServer/j2eetutorial14/examples/w
eb/hello2/build/

asant is a build tool conceptually similar to
makefile but modernize with XML 
Dickson Chiu 2004
CSIT600b 1p-16
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-17
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-18
Setting Aliases Before Running

Select the GreetingServlet Web component.








Deploy the Web module




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.
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-19
Changing Your Program

Recompile with asant build

In the deploytool





Select hello2.war
Click edit content
Add the changed class files, overwriting the old
one
Save changes
Deploy again
Dickson Chiu 2004
CSIT600b 1p-20