Einführung in die Java-Webentwicklung - Part II - [1 of 3] - Servlets (in german)

780 views

Published on

Einführung in den Technologienstack der Java Enterprise Edition (JEE) zum Bauen von komplexen Webapplikationen.

In der Prsäntation wurden die folgenden Technologien betrachtet: reilnes HTTP-Format, Servlets, JSP's (Java Server Pages) und letzendlich JSF (2.0, Java Server Faces).

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
780
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Einführung in die Java-Webentwicklung - Part II - [1 of 3] - Servlets (in german)

  1. 1. Dresden · Frankfurt/Main · Leipzig · München · Hamburg · Görlitz · Berlin Einführung in die Java-Webentwicklung Part II: JEE-Webkomponenten – (1/3) – (HTTP) Servlet
  2. 2. Einführung in die Java-Webentwicklung JEE-Webkomponenten - Servlet HttpServlet - Interface <ul><li>public class HttpServlet extends GenericServlet { </li></ul><ul><ul><li>@Override </li></ul></ul><ul><ul><li>protected void doGet (HttpServletRequest req, </li></ul></ul><ul><ul><ul><li>HttpServletResponse resp) throws ServletException, IOException { </li></ul></ul></ul><ul><ul><ul><li>… </li></ul></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>… </li></ul></ul><ul><li>} </li></ul><ul><li>Weitere HTTP-Servlet Methods : </li></ul><ul><ul><li>doPost(…), doHead(…) </li></ul></ul><ul><ul><li>doPut(…), doDelete(…) </li></ul></ul><ul><ul><li>doOptions(…), doTrace(…) </li></ul></ul>
  3. 3. Einführung in die Java-Webentwicklung JEE-Webkomponenten - Servlet HttpServlet – Grundklassen - HttpServletRequest <ul><li>public interface HttpServletRequest extends ServletRequest { </li></ul><ul><li>public Cookie[] getCookies(); </li></ul><ul><li>public String getHeader(String name); </li></ul><ul><li>public Enumeration<String> getHeaders(String name); </li></ul><ul><li>public Enumeration<String> getHeaderNames(); </li></ul><ul><li>public String getMethod(); </li></ul><ul><li>public HttpSession getSession( boolean create); </li></ul><ul><li>public HttpSession getSession(); </li></ul><ul><ul><li>… </li></ul></ul><ul><li>} </li></ul>
  4. 4. Einführung in die Java-Webentwicklung JEE-Webkomponenten - Servlet HttpServlet – Grundklassen - HttpServletRequest <ul><li>public interface ServletRequest { </li></ul><ul><li>public Object getAttribute(String name); </li></ul><ul><li>public Enumeration<String> getAttributeNames(); </li></ul><ul><li>public void setAttribute(String name, Object o); </li></ul><ul><li>public void removeAttribute(String name); </li></ul><ul><li>public int getContentLength(); </li></ul><ul><li>public String getContentType(); </li></ul><ul><li>public String getCharacterEncoding(); </li></ul><ul><li>public void setCharacterEncoding(String env) … </li></ul><ul><li>public String getParameter(String name); </li></ul><ul><li>public Enumeration<String> getParameterNames(); </li></ul><ul><li>public String[] getParameterValues(String name); </li></ul><ul><li>public Map<String, String[]> getParameterMap(); </li></ul><ul><ul><li>… </li></ul></ul><ul><li>} </li></ul>
  5. 5. Einführung in die Java-Webentwicklung JEE-Webkomponenten - Servlet HttpServlet – Grundklassen - HttpServletResponse <ul><li>public interface HttpServletResponse extends ServletResponse { </li></ul><ul><li>public void addCookie(Cookie cookie); </li></ul><ul><li>public void sendError( int sc, String msg) throws IOException; </li></ul><ul><li>public int getStatus(); </li></ul><ul><li>public void setStatus( int sc); </li></ul><ul><li>public boolean containsHeader(String name); </li></ul><ul><li>public String getHeader(String name); </li></ul><ul><li>public Collection<String> getHeaders(String name); </li></ul><ul><li>public Collection<String> getHeaderNames(); </li></ul><ul><li>public void setHeader(String name, String value); </li></ul><ul><li>public void addHeader(String name, String value); </li></ul><ul><ul><li>… </li></ul></ul><ul><li>} </li></ul>
  6. 6. Einführung in die Java-Webentwicklung JEE-Webkomponenten - Servlet HttpServlet – Servlet Deklaration – WEB-INF/web.xml <ul><li><! DOCTYPE … > </li></ul><ul><li>< web-app > </li></ul><ul><ul><li>< display-name > SaxSys Education - Web Application </ display-name > </li></ul></ul><ul><ul><li>< servlet > </li></ul></ul><ul><ul><ul><li>< servlet-name > HelloWorld Servlet </ servlet-name > </li></ul></ul></ul><ul><ul><ul><li>< servlet-class > </li></ul></ul></ul><ul><ul><ul><ul><li>de.saxsys.edu.java.jee.servlet.HelloWorldServlet </li></ul></ul></ul></ul><ul><ul><ul><li></ servlet-class > </li></ul></ul></ul><ul><ul><li></ servlet > </li></ul></ul><ul><ul><li>< servlet-mapping > </li></ul></ul><ul><ul><ul><li>< servlet-name > HelloWorld Servlet </ servlet-name > </li></ul></ul></ul><ul><ul><ul><li>< url-pattern > /helloworld.html </ url-pattern > </li></ul></ul></ul><ul><ul><li></ servlet-mapping > </li></ul></ul><ul><li></ web-app > </li></ul>
  7. 7. Einführung in die Java-Webentwicklung JEE-Webkomponenten - Servlet HttpServlet – Übungsbeispiele <ul><li>Zu implementieren : </li></ul><ul><ul><li>HelloWorldServlet </li></ul></ul><ul><ul><ul><li>muss eine HTML-Seite mit der Meldung “Hallo Welt!” ausgeben </li></ul></ul></ul><ul><ul><li>GreetingServlet </li></ul></ul><ul><ul><ul><li>muss anhand des ermittelten “userName”-Parameters aus dem GET-Request eine HTML-Seite mit der Meldung: “Hallo <userName>!” ausgeben </li></ul></ul></ul><ul><ul><li>SpyServlet </li></ul></ul><ul><ul><ul><li>- gibt Name und Adresse des Requesters aus </li></ul></ul></ul><ul><ul><ul><li>- gibt alle Header-Informationen aus </li></ul></ul></ul><ul><ul><ul><li>- zählt wie oft dieser Servlet aufgerufen worden ist </li></ul></ul></ul>
  8. 8. Einführung in die Java-Webentwicklung JEE-Webkomponenten - Servlet HttpServlet – Übungsbeispiel – HelloWorldServlet – Musterlösung <ul><li>public class HelloWorldServlet extends HttpServlet { </li></ul><ul><ul><li>@Override </li></ul></ul><ul><ul><li>protected void doGet(HttpServletRequest req, </li></ul></ul><ul><ul><ul><li>HttpServletResponse resp) throws ServletException, IOException { </li></ul></ul></ul><ul><ul><ul><li>final ServletOutputStream out = resp.getOutputStream(); </li></ul></ul></ul><ul><ul><ul><li>out.print( &quot;<html>&quot; ); </li></ul></ul></ul><ul><ul><ul><li>out.print( &quot;<body>&quot; ); </li></ul></ul></ul><ul><ul><ul><li>out.print( &quot;<h2>&quot; ); </li></ul></ul></ul><ul><ul><ul><li>out.print( &quot;Hello World!&quot; ); </li></ul></ul></ul><ul><ul><ul><li>out.print( &quot;</h2>&quot; ); </li></ul></ul></ul><ul><ul><ul><li>out.print( &quot;</body>&quot; ); </li></ul></ul></ul><ul><ul><ul><li>out.print( &quot;</html>&quot; ); </li></ul></ul></ul><ul><ul><li>} </li></ul></ul><ul><li>} </li></ul>
  9. 9. Einführung in die Java-Webentwicklung JEE-Webkomponenten - Servlet HttpServlet – Übungsbeispiel – GreetingServlet– Musterlösung <ul><li>public class GreetingServlet extends HttpServlet { </li></ul><ul><ul><li>@Override </li></ul></ul><ul><ul><li>protected void doGet(HttpServletRequest req, </li></ul></ul><ul><ul><ul><li>HttpServletResponse resp) throws ServletException, IOException { </li></ul></ul></ul><ul><ul><ul><li>final String userName = req.getParameter( &quot;userName&quot; ); </li></ul></ul></ul><ul><ul><ul><li>final ServletOutputStream out = resp.getOutputStream(); </li></ul></ul></ul><ul><ul><ul><li>out.print( &quot;<html>&quot; ); </li></ul></ul></ul><ul><ul><ul><li>out.print( &quot;<body>&quot; ); </li></ul></ul></ul><ul><ul><ul><li>out.print( &quot;<h2>&quot; ); </li></ul></ul></ul><ul><ul><ul><li>out.print( &quot;Hello &quot; + userName + &quot;!&quot; ); </li></ul></ul></ul><ul><ul><ul><li>out.print( &quot;</h2>&quot; ); </li></ul></ul></ul><ul><ul><ul><li>out.print( &quot;</body>&quot; ); </li></ul></ul></ul><ul><ul><ul><li>out.print( &quot;</html>&quot; ); </li></ul></ul></ul><ul><ul><li>} </li></ul></ul><ul><li>} </li></ul>
  10. 10. Einführung in die Java-Webentwicklung JEE-Webkomponenten - Servlet Weiterführende Informationen und Referenzen <ul><li>API – javadoc </li></ul><ul><ul><li>http://javadoc.glassfish.org/v3/apidoc </li></ul></ul><ul><li>Java EE 5 Tutorial - Java Servlet Technology (Chapter 4) </li></ul><ul><ul><li>http://java.sun.com/javaee/5/docs/tutorial/doc/bnafd.html </li></ul></ul>

×