Servlet III Java Webanwendung Webcontainer Web.xml Weiterleitung von Requests Cookies

Preview:

Citation preview

Servlet III

Java Webanwendung

Webcontainer

Web.xml

Weiterleitung von Requests

Cookies

Struktur einer Webanwendung

WurzelWebanwendung

Klassen Files

jar Files

Eigene Anwendung

eigeneFehlermeldung

Passwort File

Eigene Anwendung im JBuilder

WebanwendungWAR FileBeschreibung der

Anwendung

Servlet KlassenStatische SeitenFehlerseitePasswörter

Web Application Archive File

• Das WAR-File verpackt ein komplette Webanwendung.

• Das WEB-INF Verzeichnis ist speziell, es enthält Konfigurationen und class files für die Webanwendung.

• Format = jar-File

WAR-File

Installation von 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

Container

Servlet Container

Webserver

Request

Response

Context

Session

eigeneServlets

Konfigurationdatei für den Servlet Container

• ServletContext Init Parameters• Servlet Declaration• Servlet Mappings• Welcome File list• Error Pages• MIME Type Mappings• Session Configuration• Application Lifecyle Listener classes• Filter Definitions and Filter Mappings

web.xml

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE web-app PUBLIC "-//Sun Microsystems,

Inc.//DTD Web Application 2.2//EN" "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">

<web-app> <servlet> <servlet-name>login</servlet-name> <servlet-class>servletiii.protectedpage</servlet-class> <init-param> <param-name>passwordFile</param-name> <param-value>pw.properties</param-value> </init-param> </servlet>

web.xml

<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> <servlet-mapping> <servlet-name>wetter2</servlet-name> <url-pattern>/wetter2</url-pattern> </servlet-mapping>

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>

Seiten weiterleiten

out.println(“<a href=\“http://www.unibas.ch\“> Uni Basel </a>);

res.setHeader(“Refresh“,“3“);res.setHeader(“Refresh“,“3; URL=http://www.unibas.ch“);

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

res.sendRedirect

Servlet: formular1…res.sendRedirect(“ant2.html“);…

http://..formular1

ResponseStatus 302Location ant2.html

Get Request

CLIENT SERVER

http://..ant2.html Get Requestant2.html<HTML>…</HTML>

Response

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);

// 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>");…

RequestDispatcher rd;

rd = getServletContext(). \\

getRequestDispatcher(“/display.html“);

rd.forward(req,res);

RequestDispatcher disp = req.getRequestDispatcher("/display.html");

disp.forward(req, res);

res.sendRedirect

Servlet: formular1…res.sendRedirect(“ant2.html“);…

http://..formular1 Get Request

CLIENT SERVER

ant2.html<HTML>…</HTML>

Response

Was ist ein Cookie?Ein Cokie sind kleine

Textinformationvom Server zum Client gesendetJedes Mal, wenn der Browser die

Seite wieder besucht wird das Cookie geschickt.

Cookies ermöglichen anhaltende (persistent) Sitzungen via http

RFC 2109

Cookies

Vorteile?Identifizierung eine UsersPersonifizierte Homepages,

Portale, User Profiles Automatisches Login

Cookies

Servlet Cookie APIErzeugen mit:

c = new Cookie(name, value)

Verschieden Attribute:

public String getComment()

public void setComment(String comment)

public String getDomain()

public void setDomain(String domainPattern)

c.setDomain(.unibas.ch)

Cookies

Servlet Cookie APIpublic String getMaxAge()

public void setMaxAge(int lifetime in Sekunden)

lifetime < 0 das Cookie nicht speichern

lifetime = 0 das Cookie von der Disk löschen

public String getPath()

public void setPath(String path)

path =“/ “ Alle Seiten des Servers erhalten das Cookie

path = “/ifi/user “ Bei allen Seiten unterhalb dieses Pfades schickt der User-Agent das Cookie

Cookies

Beispiel SetCookiespackage cookies;

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

public class SetCookies extends HttpServlet {

public void doGet(HttpServletRequest request,

HttpServletResponse response)

throws ServletException, IOException {

for(int i=0; i<3; i++) {

// Default maxAge is -1, indicating cookie

// applies only to current browsing session.

Cookie cookie = new Cookie("Session-Cookie-" + i,

"Cookie-Value-S" + i);

response.addCookie(cookie);

Cookies

Beispiel SetCookies

cookie = new Cookie("Persistent-Cookie-" + i,

"Cookie-Value-P" + i);

// Cookie is valid for an hour, regardless of whether

// user quits browser, reboots computer, or whatever.

cookie.setMaxAge(3600);

response.addCookie(cookie);

}

response.setContentType("text/html");

PrintWriter out = response.getWriter();

String title = "Setting Cookies";

Cookies

Beispiel SetCookiesout.println (ServletUtilities.headWithTitle(title) +

"<BODY BGCOLOR=\"#FDF5E6\">\n" +

"<H1 ALIGN=\"CENTER\">" + title + "</H1>\n" +

"There are six cookies associated with this page.\n" +

"To see them, visit the\n" +

"<A HREF=\"/servlet/cookies.ShowCookies\">\n" +

"<CODE>ShowCookies</CODE> servlet</A>.\n" +"<P>\n" +

"Three of the cookies are associated only with the\n" +

"current session, while three are persistent.\n" +

"Quit the browser, restart, and return to the\n" +

"<CODE>ShowCookies</CODE> servlet to verify that\n" +

"the three long-lived ones persist across sessions.\n" +

"</BODY></HTML>");

}

}

Cookies

Praktikum

Kundenangaben mit PW Abfrage:

1. http Passwortabfrage

2. Formular mit Passwortabfrage

3. Eingabe von weiteren Daten

4. Alle Daten anzeigen

5. Eingabe von weiteren Daten

6. Cookie für AutoLogin

7. Möglichkeit das Cookie zu löschen

3.) Eingabe von Daten

Ihr Job

1.)

4.) Anzeige der Daten2.)

Recommended