Servlet II Inhalt: Konfiguration: web.xml ; server.xml Workflow: Weiterleitung von Requests Lektion II-IV Lektion V-VI 3-1 1. Grundlagen 2. Servlets 3. JSP 4 1.1. JAR Files 4 1.2. TCP/IP, Sockels 4 1.3. HTTP 4 1.4. Java Web Service Developer Pack (WSDP) 4 1.5. Ant 4 1.6. JAVA Fehlermeldungen 4 2.1. Servlet Framework 4 2.2. Tomcat Servlet Container 4 2.3. Lebenszyklus 4 2.4. Hello World Servlet 4 2.5. Implizite Objekte 4 2.6. Einfache Servlets 4 2.7. Konfiguration (web.xml) 4 2.8. Anfrage weiterleiten 4 2.9. Filter 4 2.10. Cookies 4 2.11. Session Tracking 4 3.1. Architektur 4 3.2. Syntax, Skripte, Direktiven 4 3.3. Implizite Objekte 4 3.4. Aktionen 4 3.5. JavaBeans 4 3.6. Custom Tags 4. Servlet JSP Framework Struts 5. XML 4 4.1. Architektur 4 4.2. Planung einer Webanwendung 4 4.3. Implementierung 4 5.1. Grundlagen 4 5.2. DTD & Schemata 4 5.3. Parser (SAX, DOM) 4 5.4. XPath 4 5.5. XSLT 4 5.6. XSL-FO 6. XML Framework Cocoon 4 6.1. Architektur 4 6.2. Cocoon Beispiel 4 6.3. Sitemap 4 6.4. XSP 7. JAVA Webservices 4 7.1. SOAP 4 7.2. UDDI / ebxml 4 7.3. WSDL 4 7.4. Implementierung eines Webservice 3-2 1
Ablauf http://..index.html Get Request Webserver Servlet Container Response doget() 3-3 Konfiguration Webserver Servlet Container Web Anwendung server.xml Request Response web.xml Context Session 3-4 2
Struktur einer Webanwendung JaxWebApp/ index.html main.jsp images/ company.jpg people.jpg WEB-INF/ web.xml classes/ jax/ lib/ HelloWorld.class counter.class xerces.jar Webanwendung HTML Seiten Java Server Pages Konfiguration Classes Files jar Files 3-5 WAR web application archive WAR => JAR-File Struktur einer Webanwendung Bsp: mit ANT <property name="war.dist" value= lekt3.war"/> <jar jarfile="${war.dist}" basedir="${build.dir}"/> Kommando Zeile: jar -cfv lekt3.war PATH/build 3-6 3
Deployment einer Webanwendungen Eine Webanwendung wird auf einem Server installiert, indem das WAR- File in den webapp Ordner kopiert wird. Tomcat 4.0 http://jakarta.apache.org WAR-File 3-7 2.7 Konfiguration (web.xml; server.xml) server.xml => Konfiguration des Servlet Containers web.xml => Konfiguration einer Webanwendung 3-8 4
server.xml <Server> JAVA VM <Service> Sammlung mehrer Connectors <Connector /> Endpunkt von REQ und RES <Engine> Repräsentiert Catalina Objekt <Host> Virtueller Host <Context> Definiert die individuellen Webapplikationen </Context> </Host> </Engine> </Service> </Server> 3-9 Bsp. <Server classname="org.apache.catalina.core.standardserver" port="8005" debug="0" shutdown="shutdown"> <Listener classname="org.apache.catalina.mbeans.serverlifecyclelistener" debug="0"/> <Listener classname="org.apache.catalina.mbeans.globalresourceslifecyclelistener" debug="0"/> <GlobalNamingResources> <Environment name="jwsdp.home" override="true" type="java.lang.string" description="absolute Pathname of the JWSDP Installation" value="/users/guggi/jwsdp-1_0"/> <Resource name="userdatabase" type="org.apache.catalina.userdatabase" scope="shareable" description="users and Groups Database" auth="container"/> <ResourceParams name="userdatabase"> <parameter> <name>factory</name> <value>org.apache.catalina.users.memoryuserdatabasefactory</value> </parameter> <parameter> <name>pathname</name> <value>conf/tomcat-users.xml</value> </parameter> </ResourceParams> </GlobalNamingResources> <Service classname="org.apache.catalina.core.standardservice" debug="0" name="internal Services"> <Connector classname="org.apache.coyote.tomcat4.coyoteconnector" redirectport="443" buffersize="2048" port="8081" connectiontimeout="60000" scheme="http" enablelookups="false" secure="false" protocolhandlerclassname="org.apache.coyote.http11.http11protocol" debug="0" proxyport="0" tcpnodelay="true" minprocessors="5" maxprocessors="75" acceptcount="10" useurivalidationhack="true"> <Factory classname="org.apache.catalina.net.defaultserversocketfactory"/> </Connector>... 3-10 5
Konfiguration Ein Webservice wird im Servletcontainer mittels eines Context Knoten angesteuert. Die Angaben der Konfiguration des Servletcontainers stehen in der server.xml Datei. 3-11 Installation eines neuen Service 3-12 6
Dynamische Konfiguration des Servletcontainers Web Application Manager List Install http://<host>:8080/manager/list http://<host>:8080/manager/install? path=/mywebapp&war=file:/path/to/mywebapp http://<host>:8080/manager/install? path=/mywebapp& war=jar:file:/path/to/mywebapp.war!/ Reload Remove http://<host>:8080/manager/reload?path=/mywebapp http://<host>:8080/manager/remove?path=/mywebapp 3-13 Konfiguration web.xml Deployment Descriptor Verteilungs- oder Installationsbeschreibung Definition der Servlets oder Filter (Name,Klasse,Mapping) Initialisierungs-Parameter Fehler Seiten Startseiten 3-14 7
Bsp.: web.xml <?xml version="1.0" encoding="iso-8859-1"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"> <web-app> <servlet> <servlet-name>helloworld</servlet-name> <servlet-class>jax.helloworld</servlet-class> </servlet> <servlet-mapping> <servlet-name>helloworld</servlet-name> <url-pattern>/hello</url-pattern> </servlet-mapping> </web-app> 3-15 URL Mapping <servlet-mapping> <servlet-name>login</servlet-name> <url-pattern>*.log</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>wetter1</servlet-name> <url-pattern>/wetter/*</url-pattern> </servlet-mapping> 3-16 8
Mapping Eine Webanwendung wird via Context assoziiert. /Jax File: PATH/JaxWebApp/* Servlets in der Webanwendung, werden relativ zu diesem Context angesprochen. http://host/jax/hello jax.helloworld.class 3-17 web.xml <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> </welcome-file-list> <error-page> <error-code>404</error-code> <location>/404.html</location> </error-page> </web-app> 3-18 9
Error Page <error-page> <exception-type> java.lang.arithmeticexception </exception-type> <location> /error.html </location> </error-page> 3-19 2.8 Anfrage weiterleiten out.println( <a href=\ http://www.unibas.ch\ > Uni Basel </a> ); res.setstatus(res.sc_moved_temporarily); res.setheader( Location, http://www.new.site ); res.sendredirect( http://www.new.site ); RFC 2616 Hypertext Transfer Protocol (HTTP/1.1) http://www.rfc-editor.org 3-20 10
res.sendredirect CLIENT http://..formular1 Get Request Response Status 302 Location ant2.html SERVER Servlet: formular1 res.sendredirect( ant2.html ); http://..ant2.html Get Request Response ant2.html <HTML> </HTML> 3-21 public class randomforward2 extends HttpServlet { private static final String CONTENT_TYPE = "text/html"; private Vector wetter = new Vector(); Random random = new Random(); /**Initialize global variables*/ public void init() throws ServletException { wetter.add("/html/gut.html"); wetter.add("/html/schlecht.html"); wetter.add("/html/nebel.html"); wetter.add("/html/kalt.html"); } /**Process the HTTP Get request*/ public void doget(httpservletrequest req, HttpServletResponse res) throws ServletException, IOException { res.setcontenttype(content_type); PrintWriter out = res.getwriter(); int index = Math.abs(random.nextInt())% wetter.size(); String go = (String)wetter.elementAt(index); 3-22 11
// Methode 1 //res.sendredirect(req.getcontextpath()+go); //res.flushbuffer(); // Methode 2 //res.setstatus(res.sc_moved_temporarily); //res.setheader("location",req.getcontextpath()+go); //res.flushbuffer(); // Methode 3 String dummy = "3; URL="+req.getContextPath()+go; res.setheader("refresh",dummy); res.flushbuffer(); out.println("<html><head><title>randomforward</title></head>"); out.println("<body>hierhin kommen Sie nicht</body></html>"); 3-23 RequestDispatcher RequestDispatcher rd; rd = getservletcontext(). \\ getrequestdispatcher( /display.html ); rd.forward(req,res); RequestDispatcher disp = req.getrequestdispatcher("/display.html"); disp.forward(req, res); 3-24 12
res.sendredirect CLIENT SERVER http://..formular1 Get Request Servlet: formular1 res.sendredirect( ant2.html ); Response ant2.html <HTML> </HTML> 3-25 13