Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "1 Dickson K.W. Chiu PhD, SMIEEE Reference: Sun J2EE 1.4 Tutorial CSIT600b: XML Programming XML Programming Guide: Getting Started."— Presentation transcript:

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

2 Dickson Chiu 2004CSIT600b 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.

3 Dickson Chiu 2004CSIT600b 1p-3 J2EE Containers and API Container = platform / runtime environment

4 Dickson Chiu 2004CSIT600b 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

5 Dickson Chiu 2004CSIT600b 1p-5 Packaging J2EE Application Deployment Descriptors can be changed for software configuration without changing the source code

6 Dickson Chiu 2004CSIT600b 1p-6 Java Web Application Overview Servlets are basics Web Components in web containers

7 Dickson Chiu 2004CSIT600b 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.

8 Dickson Chiu 2004CSIT600b 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

9 Dickson Chiu 2004CSIT600b 1p-9 javax.servlet.http Package MethodsHTTP RequestsComments doGet GET, HEADUsually overridden doPost POSTUsually overridden doPut PUTUsually not overridden doOptions OPTIONSAlmost never overridden doTrace TRACEAlmost 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

10 Dickson Chiu 2004CSIT600b 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(" " + " Hello "); out.println(" " + " " + " Hello, my name is Duke. What's yours? " + " " + " " + " " + " " + " " + " "); Specify target page if necessary

11 Dickson Chiu 2004CSIT600b 1p-11 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(" "); out.close(); } public String getServletInfo() { return "The Hello servlet says hello."; } Stuff the output of another servlet to here

12 Dickson Chiu 2004CSIT600b 1p-12 Java Servelet Example - Response // import not shown here public 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(" Hello, " + username + "! "); } public String getServletInfo() { return "The Response servlet says hello."; } Parameters passed with request / response objects

13 Dickson Chiu 2004CSIT600b 1p-13 Servlet Life Cycle l Servlets are controlled by servers l A server loads and initializes the servlet l The servlet handles zero or more client requests l The server terminates the servlet

14 Dickson Chiu 2004CSIT600b 1p-14 Servlet Life Cycle (2) The server will automatically called : public void init(): l Called only once when serlvet is being created. l Good place for set up, open Database, etc. public void service(): l Called once for each request. In HttpServlet, it delegates requests to doGet, doPost, etc. l public void destroy(): l Called when server decides to terminate the serverlet. l Release resources.

15 Dickson Chiu 2004CSIT600b 1p-15 Environment Installation Visit: http://java.sun.com/j2ee/1.4/download.htmlhttp://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)

16 Dickson Chiu 2004CSIT600b 1p-16 Compiling hello2 In a terminal window, go to H:/Sun/AppServer/j2eetutorial14/examples/web/hello 2/ Run asant build. This target will compile the servlets to the directory H:/Sun/AppServer/j2eetutorial14/examples/web/hello 2/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/ http://ant.apache.org/

17 Dickson Chiu 2004CSIT600b 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

18 Dickson Chiu 2004CSIT600b 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.

19 Dickson Chiu 2004CSIT600b 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.

20 Dickson Chiu 2004CSIT600b 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

21 Dickson Chiu 2004CSIT600b 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


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

Similar presentations


Ads by Google