1 / 89

Servlet , JSP

Servlet , JSP. Margus Hanni, Nortal AS. 11.03.2013. Viited varasematele materjalidele…. 2012 – TÜ - Servlets , JSP, Web Containers – Roman Tekhov 2010 – Webmedia - Java EE + containers – Anti Orgla. Kas on asjakohane?.

amity-ramos
Télécharger la présentation

Servlet , JSP

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Servlet, JSP Margus Hanni, Nortal AS 11.03.2013

  2. Viited varasematele materjalidele… • 2012 – TÜ - Servlets, JSP, WebContainers– Roman Tekhov • 2010 – Webmedia - Java EE + containers – Anti Orgla

  3. Kas on asjakohane? http://www.tiobe.com/index.php/content/paperinfo/tpci/index.html

  4. Kas on asjakohane? http://www.tiobe.com/index.php/content/paperinfo/tpci/index.html

  5. HelloWorld - C ja JAVA #include <stdio.h> intmain(){ printf("Hello World\n"); return 0; } publicclassHelloWorld{ publicstaticvoidmain(String[]args){ System.out.println("Hello, World"); } }

  6. Kas on asjakohane? • Võrreldes eelmise aastaga on JAVA populaarsus taas kasvanud • Jätkuvalt on JAVA populaarseim keel ning selles osas arvatavasti lähiajal muutusi ei toimu • JAVA on laialdaselt kasutuses erinevate veebilahenduste loomisel

  7. JAVA EE(EnterpriseEdition) • Kogum vahendeid erinevate lahenduste loomiseks: • Veebi rakendused • Veebi teenused • Sõnumivahetus • Andmebaasid • … http://en.wikipedia.org/wiki/Java_Platform,_Enterprise_Edition

  8. JAVA EE • Kogum vahendeid erinevate lahenduste loomiseks: • Veebi rakendused • Veebi teenused • Sõnumivahetus • Andmebaasis • … http://en.wikipedia.org/wiki/Java_Platform,_Enterprise_Edition WebContainer Servlet JSP

  9. WebContainer Manages component life cycles Accepts requests, sends responses Routes requests to applications http://tutorials.jenkov.com/java-servlets/overview.html

  10. Web Containers • ApacheTomcat • JBoss • WebLogic • Jetty • Glassfish • Websphere • …

  11. Web Containers Multiple applications inside one container http://tutorials.jenkov.com/java-servlets/overview.html

  12. Application structure

  13. Application structure Java source files

  14. Application structure Document root

  15. Application structure Static content

  16. Application structure Configuration, executable code

  17. Application structure Deployment descriptor

  18. Application structure Compiled classes

  19. Application structure Dependencies (JAR-s)

  20. Application structure Java Server Pages

  21. Deployment descriptor (web.xml) Instructs the container how to deal with this application <?xmlversion="1.0" encoding="UTF-8"?> <web-appxmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> </web-app>

  22. Deployment descriptor (web.xml) In Servlet API version 3.0 most components of web.xml are replaced by annotations that go directly to Java source code. We will see examples later

  23. Servlets • On JAVA klass, mis töötleb sissetulevat päringut ning tagastab vastuse • Enamasti kasutatakse HTTP päringute ja vastuste töötlemiseks • Servletid töötavad veebikonteineris, mis hoolitseb nende elutsükli ning päringute suunamise eest • javax.servlet.http.HttpServlet – abstraktne klass, mis sisaldab meetodeid doGet ja doPost HTTP päringute töötlemiseks

  24. Servlet example publicclassHelloServletextendsHttpServlet { @Override protectedvoiddoGet(HttpServletRequestreq, HttpServletResponse resp) throwsServletException, IOException { PrintWriterwriter = resp.getWriter(); writer.println("<html><head><title>Hello</title></head><body>"); writer.println("<p>HelloWorld!</p>"); writer.println("<p>Current time: " + new Date() + "</p>"); writer.println("</body></html>"); } }

  25. HttpServlet methods For each HTTP method there is corresponding HttpServlet method doPost doGet doPut …

  26. Servleti töö

  27. Servlet Mapping Before Servlet 3.0 in web.xml <servlet> <servlet-name>hello</servlet-name> <servlet-class>example.HelloServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>hello</servlet-name> <url-pattern>/hello</url-pattern> </servlet-mapping>

  28. Servlet Mapping In Servlet 3.0 via annotation @WebServlet("/hello") publicclassHelloServletextendsHttpServlet { ...

  29. Servlet life cycle http://tutorials.jenkov.com/java-servlets/servlet-life-cycle.html

  30. Üldine servleti elutsükkel • Kui veebikonteineris puudub Servleti instants • Laetakse Servleti klass • Luuakse isend • Initsialiseeritakse (init meetod) • Iga päringu jaoks kutsutakse välja servicemeetod • Servleti kustutamisel kutsutakse välja destroy meetod

  31. Sessions HTTP is a stateless protocol, but we often need the server to remember us between requests There are some ways Cookies URL rewriting

  32. Java HttpSession • HttpSession is a common interface for accessing session context • Java Servlet API abstract away the details of how the session is maintained

  33. Java HttpSession http://java.sun.com/developer/onlineTraining/JSPIntro/contents.html

  34. HttpSession example HttpSessionsession = req.getSession(); intvisit; if (session.isNew()) { visit = 0; } else { visit = (Integer) session.getAttribute("visit"); } session.setAttribute("visit", ++visit);

  35. HttpSession example HttpSessionsession = req.getSession(); intvisit; if (session.isNew()) { visit = 0; } else { visit = (Integer) session.getAttribute("visit"); } session.setAttribute("visit", ++visit); Either create a new session or get existing

  36. HttpSession example HttpSessionsession = req.getSession(); intvisit; if (session.isNew()) { visit = 0; } else { visit = (Integer) session.getAttribute("visit"); } session.setAttribute("visit", ++visit); Check if the session is fresh or not

  37. HttpSession example HttpSessionsession = req.getSession(); intvisit; if (session.isNew()) { visit = 0; } else { visit = (Integer) session.getAttribute("visit"); } session.setAttribute("visit", ++visit); Retrieveattribute

  38. HttpSession example HttpSessionsession = req.getSession(); intvisit; if (session.isNew()) { visit = 0; } else { visit = (Integer) session.getAttribute("visit"); } session.setAttribute("visit", ++visit); Update attribute

  39. HttpServletRequest Contains request information Also can be used to store attributes request.setAttribute(“key", value); request.getAttribute(“key”);

  40. HttpServletRequest: parameters request.getParameterNames(); Enumeration<String> String value = request.getParameter("name");

  41. HttpServletRequest: meta data request.getMethod(); “GET”, “POST”, … request.getRemoteAddr(); Remote client’s IP request.getServletPath(); “/path/to/servlet” …

  42. HttpServletRequest: headers request.getHeaderNames(); Enumeration<String> request.getHeader("User-Agent"); “Mozilla/5.0 (X11; Linux x86_64) …”

  43. RequestHeaders

  44. HttpServletRequest: cookies Cookie[] cookies = request.getCookies(); cookie.getName(); cookie.getValue(); cookie.setValue(“new value”);

  45. Cookie

  46. HttpServletResponse Allows to set response information response.setHeader("Content-Type", "text/html"); response.addCookie(newCookie("name", "value"));

  47. ResponseHeaders

  48. HttpServletResponse: content response.getWriter().println("..."); Write text response.getOutputStream().write(...); Write binary

  49. Problem with servlets Writing HTML in Java is hideous PrintWriterwriter = resp.getWriter(); writer.println("<html><head><title>Hello</title></head><body>"); writer.println("<p>HelloWorld!</p>"); writer.println("<p>Current time: " + new Date() + "</p>"); writer.println("</body></html>");

  50. Java Server Pages (JSP) • Write standard markup • Add dynamic scripting elements • Add Java code

More Related