260 likes | 448 Vues
Links. http://www.joller-voss.ch/ndkjava/. JSP & Komponenten. Java Entwickler. Anwendungs Logik. Daten Präsentation. DB. Daten Zugriff. WEB Designer. <jsp:forward> <jsp:include> <jsp:param> <jsp:useBean> <jsp:setProperty> <jsp:getProperty> <jsp:plugin> <jsp:fallback>.
E N D
Links • http://www.joller-voss.ch/ndkjava/
JSP & Komponenten Java Entwickler Anwendungs Logik Daten Präsentation DB Daten Zugriff WEB Designer
<jsp:forward> <jsp:include> <jsp:param> <jsp:useBean> <jsp:setProperty> <jsp:getProperty> <jsp:plugin> <jsp:fallback> Steuerung des Kontrollfluss zwischen den Seiten. Zusammenspiel mit Beans Spezifikation für Applets. Actions
Forward • <jsp:forward page=‘<%= “message“ + statusCode + “.html“ %> /> <jsp:forward page=“localURL“ /> Request --------- --- ---------- ------- ----- ---- --------- - Original Page Request Forwarded Page Response
Anwendungen forward • Parameterübergabe • <jsp:forward page=“localURL“> • <jsp:param name=“parameterName1“ • value=“parameterValue1“ /> • </jsp:forward> • Kontrollfluss • <% if (! Database.isAvailable()) { %> • <%-- Informieren den User zur Wartung der DB --%> • <jsp:forward page=“db-wartung.html“ /> • <% } %> • <%-- Datenbank ist verfügbar --%>
Include <jsp:include page=“localURL“ flush=„true“/> Request --------- --- ---------- ------- ----- ---- --------- - Original Page Request Included Page Response Response
Action vs. Direktive • Direktive <@ include …> • Im Moment der Seitengenerierung wird der • Inhalt eingefügt. • Action <jsp:include …> • Der Request wird direkt auf die neue Seite umgeleitet und der Response wird wieder zurückgeleitet.
Problematik • Servlets • streng definiert • „pure Java“ • mit viel Dokument-Quellcode oft unübersichtlich • JSP • Schwammige Spezifikationen, • Durchmischung zweier Technologien • Mit viel Java-Code oft unübersichtlich JSP
Java Beans, Part 1 (java.sun.com) • Support for introspection allowing a builder tool to analyze how a bean works. • Support for customization allowing a user to alter the appearance and behavior of a bean. • Support for events allowing beans to fire events, and informing builder tools about both the events they can fire and the events they can handle. • Support for properties allowing beans to be manipulated programatically, as well as to support the customization mentioned above. • Support for persistence allowing beans that have been customized in an application builder to have their state saved and restored. Typically persistence is used with an application builder's save and load menu commands to restore any work that has gone into constructing an application. • http://developer.java.sun.com/developer/onlineTraining/Beans
heloworld Bean • package jspcr; • import java.io.*; • public class heloworldbean implements Serializable { • private String message; • public String getMessage() • { • return message; • } • public void setMessage(String message) • { • this.message = message; • } • public String getCount10() • { • String s; • s=""; • for (int i=0;i < 10 ; i++ ) { • s += i+" "; } • return s; • }
helloworld bean jsp • <html><head><title>helloworld</title></head><body> • <h1>Helloworld JSP</h1> • <jsp:useBean id="hello" class="jspcr.heloworldbean"> • <jsp:setProperty • name="hello" • property="message" • value="Guten Tach, wie lauefts ????"/> • </jsp:useBean> • hier kommt die Meldung:<br><br> • <jsp:getProperty • name="hello" • property="message"/> • <br>und nun auf 10:<br> • <jsp:getProperty • name="hello" • property="count10"/> • <br></body></html>
Web Service: Flugwetter • Aktuelle Daten • Web Architektur • Logik (Business Objekt) • Implementierung • Bean • Präsentation (JSP, CSS, HTML)
Architektur SetAirport- Code.jsp sendRedirect style.css portal.jsp Cookie? Airport- Selection.html weather observation
getAirportCode() setAirportCode(String airportCode) getLocation() getTemperature() getTime() getURL() Hilfsmethoden load(InputStream stream) loadFromURL(URL url) load(InputStream stream) parseLocation(String line) parseTime(String line) parseTemperature(String line) Business Logic
Bean: Weather • package jspcr.beans.weather; • import java.io.*; • import java.net.*; • import java.text.*; • import java.util.*; • public class Observation implements Serializable • { • private static final String BASEURL = • "http://weather.noaa.gov/weather/current"; • private static final SimpleDateFormat DATEFMT = • new SimpleDateFormat("MMM dd, yyyy - hh:mm aa zzz"); • private String airportCode; • private String location; • private Date time; • private Double temperature;
Bean Methoden • // Bean accessor methods • public String getAirportCode() • { • return airportCode; • } • public void setAirportCode(String airportCode) • throws IOException • { • this.airportCode = airportCode; • loadFromURL(getURL()); • } • public String getLocation() • { • return location; • } • protected void setLocation(String location) • { • this.location = location; • }
public URL getURL() throws MalformedURLException { StringBuffer sb = new StringBuffer(); sb.append(BASEURL); sb.append("/K"); sb.append(airportCode.toUpperCase()); sb.append(".html"); return new URL(sb.toString()); } protected void loadFromURL(URL url) throws IOException { load(url.openStream()); } protected void load(InputStream stream) throws IOException { location = null; time = null; temperature = null; BufferedReader in = new BufferedReader( new InputStreamReader(stream)); for (;;) { // while(true) String line = in.readLine(); if (line == null) break; if (location == null) parseLocation(line); if (time == null) parseTime(line); if (temperature == null) parseTemperature(line); } in.close(); } Load
Parse Temperature • protected void parseTemperature(String line) • { • final String TOKEN1 = "("; • final String TOKEN2 = "C)"; • int q = line.lastIndexOf(TOKEN2); • if (q != -1) { • int p = line.lastIndexOf(TOKEN1); • if (p != -1) { • p += TOKEN1.length(); • String token = line.substring(p, q).trim(); • try { • setTemperature(Double.parseDouble(token)); • } • catch (NumberFormatException e) { • e.printStackTrace(); • } • } • } • }
style.css .whiteOnBlue, .blueOnWhite { font-family: Verdana; font-size: 9pt; text-decoration: none; } .whiteOnBlue { background-color: #005A9C; color: #FFFFFF; } .blueOnWhite { background-color: #FFFFFF; color: #005A9C; } AirportSelection.html <HTML> <HEAD> <TITLE>Airport Selection</TITLE> </HEAD> <BODY> <FORM METHOD="POST" ACTION="SetAirportCode.jsp"> Select airport: <INPUT TYPE="SUBMIT" VALUE="Set Airport Code"> <P> <SELECT NAME="airportCode" SIZE=10> <OPTION VALUE="CLT">Charlotte Douglas International Airport <OPTION VALUE="DFW">Dallas Fort Worth International Airport <OPTION VALUE="DEN">Denver International Airport <OPTION VALUE="JFK">Kennedy International Airport <OPTION VALUE="LMT">Klamath Falls International Airport <OPTION VALUE="LGA">La Guardia Airport <OPTION VALUE="TVL">Lake Tahoe Airport <OPTION VALUE="LAX">Los Angeles International Airport <OPTION VALUE="EWR">Newark International Airport <OPTION VALUE="SWF">Newburgh Stewart Airport <OPTION VALUE="ROW">Roswell Industrial Air Center Airport <OPTION VALUE="SAF">Santa Fe County Municpal Airport <OPTION VALUE="SLC">Salt Lake City International Airport <OPTION VALUE="SFO">San Francisco International Airport <OPTION VALUE="SEA">Seattle Tacoma International Airport <OPTION VALUE="EAT">Wenatchee Pangborn Memorial Airport <OPTION VALUE="YKM">Yakima Air Terminal </SELECT> </FORM> </BODY> </HTML> statische Dateien
setAirPort Code.jsp • <%@ page session="false" %> • <% • String airportCode = request.getParameter("airportCode"); • if (airportCode != null) { • Cookie cookie = new Cookie("airportCode", airportCode); • final int ONE_YEAR = 60 * 60 * 24 * 365; • cookie.setMaxAge(ONE_YEAR); • response.addCookie(cookie); • } • response.sendRedirect("Portal.jsp"); • %>
Portal.jps • <%@ page session="false" %> • <HTML><HEAD><TITLE>LyricNote Portal</TITLE> • <LINK REL="stylesheet" HREF="style.css"> • </HEAD><BODY> • <IMG SRC="images/lyric_note.png"> • <HR COLOR="#000000"> • <%-- Get weather cookie --%> • <% • String airportCode = "RDU"; • Cookie[] cookies = request.getCookies(); • if (cookies != null) { • for (int i = 0; i < cookies.length; i++) { • Cookie cookie = cookies[i]; • if (cookie.getName().equals("airportCode")) { • airportCode = cookie.getValue(); • break; • } • } • } • %>
Portal.jps (II) • <%-- Get the weather observation bean for that location --%> • <jsp:useBean id="wobs" class="jspcr.beans.weather.Observation"> • <jsp:setProperty • name="wobs" • property="airportCode" • value="<%= airportCode %>"/> • </jsp:useBean> • <%-- Show weather information --%> • <SPAN CLASS="whiteOnBlue"> Weather </SPAN> • <SPAN CLASS="blueOnWhite"> • <jsp:getProperty name="wobs" property="location"/> • <jsp:getProperty name="wobs" property="time"/> • <jsp:getProperty name="wobs" property="temperature"/> C° • </SPAN> • <A CLASS="whiteOnBlue" HREF="AirportSelection.html"> Select City </A> • <HR COLOR="#000000"> • <%-- Show the rest of the web page --%> • </BODY> • </HTML>
Praktikum • JSP – Java Bean Anwendung • Begrüssungsseite (jsp) • Portalseite (jsp) • Auswahl (html, jsp) • Bean • Bsp.: (Schweizer Wetter, Börsenkurse, • 5-10 Bahnverbindungen mit aktuellen Zeiten)