1 / 33

Building Web Apps with Servlets

Building Web Apps with Servlets. http://flic.kr/p/hArj5. Why web apps?. Create apps every bit as dynamic, interactive, and custom tailored as native apps Avoid deployment problems Reach people world wide. http://flic.kr/p/9DTDXi. First, let ’ s review some basics about how the Web works

Download Presentation

Building Web Apps with Servlets

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. Building Web Apps with Servlets http://flic.kr/p/hArj5

  2. Why web apps? • Create apps every bit as dynamic, interactive, and custom tailored as native apps • Avoid deployment problems • Reach people world wide http://flic.kr/p/9DTDXi

  3. First, let’s review some basics abouthow the Web works Static Web Pages

  4. The architecture of the Web Head First Servlets and JSP (2nd edition), p. 3

  5. Typical web browser/server interaction At the very least, what must to give your browserso that it can request a web page? A URL! http://flic.kr/p/9ksxQa Head First Servlets and JSP (2nd edition), p. 4

  6. Anatomy of a URL http://www.wickedlysmart.com:80/beeradvice/select/beer1.html Port Path Server Protocol Resource

  7. So what do requests and responsesactually look like anyway? Head First Servlets and JSP (2nd edition), p. 4

  8. Example HTTP GET request Protocolversion Path toresource HTTPMethod GET /select/selectBeerTaste.jsp HTTP/1.1 Host: www.wickedlysmart.com User-Agent: Mozilla/5.0 (Macintosh; U; PPC Mac OS X… Accept: text/xml,application/xml,application/xhtml+xml… Accept-Language: en-us,en;q=0.5 Accept-Encoding: gzip,deflate Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7 Keep-Alive: 300 Connection: keep-alive Requestheaders

  9. Example HTTP response HTTP status code Protocolversion Text version of status code HTTP/1.1 200 OK Content-Type: text/html Content-Length: 397 Date: Wed, 19 Nov 2003 03:25:40 GMT Server: Apache-Coyote/1.1 Connection: close <html> … </html> Responseheaders Response body

  10. Let’s see the request/response in a bit more detail… Head First Servlets and JSP (2nd edition), p. 18

  11. Head First Servlets and JSP (2nd edition), p. 18

  12. Now, let’s talk about … Dynamic Web Pages (Web Apps)

  13. Typical architecture of a web app Head First Servlets and JSP (2nd edition), p. 27

  14. Typical architecture of a web app Head First Servlets and JSP (2nd edition), p. 27

  15. Typical architecture of a web app Head First Servlets and JSP (2nd edition), p. 27

  16. Typical architecture of a web app Head First Servlets and JSP (2nd edition), p. 27

  17. Recall step #2… But how do youpass parameters? Head First Servlets and JSP (2nd edition), p. 27

  18. GET requests can embed parameters in the URL Parameter list Head First Servlets and JSP (2nd edition), p. 14

  19. Limitations of GET parameters • Total characters limited (varies by server) • Parameters are exposed • Better not do passwords A better way to pass parameters:The HTTP POST method

  20. Example HTTP POST request HTTPMethod Path toresource Protocolversion POST /select/selectBeerTaste.do HTTP/1.1 Host: www.wickedlysmart.com User-Agent: Mozilla/5.0 (Macintosh; U; PPC Mac OS X… Accept: text/xml,application/xml,application/xhtml+xml… Accept-Language: en-us,en;q=0.5 Accept-Encoding: gzip,deflate Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7 Keep-Alive: 300 Connection: keep-alive color=dark&taste=malty Requestheaders Requestbody How do you make a POST request?HTML forms. More on that in a minute

  21. Now, here’s how you implement web apps using … Servlets

  22. Servlet web app architecture You write Java EE provides Head First Servlets and JSP (2nd edition), p. 39

  23. How do you create a Java EE web app?Use Eclipse A few key steps: • Create Dynamic Web project • Create servlet class(es) • Configure Deployment Descriptor (DD; aka web.xml) • Compile, then deploy, then run Allow me to demonstrate

  24. Some key features of a servlet import javax.servlet.*; import javax.servlet.http.*; import java.io.*; public class Ch1Servlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { PrintWriter out = response.getWriter(); java.util.Date today = new java.util.Date(); out.println(“<html> “ + “<body>” + “<h1 align=center>HF\’s Chapter1 Servlet</h1>” + “<br>” + today + “</body>” + “</html>”); } }

  25. Some key features of a DD <?xml version=”1.0” encoding=”ISO-8859-1” ?> <web-app xmlns=http://java.sun.com/xml/ns/j2ee xmlns:xsi=…> <servlet> <servlet-name>Chapter1 Servlet</servlet-name> <servlet-class>Ch1Servlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>Chapter1 Servlet</servlet-name> <url-pattern>/Serv1</url-pattern> </servlet-mapping> </web-app> Namein DD Javaclass URLpath

  26. To make sure you got it,let’s walk through anotherservlet request/response

  27. Head First Servlets and JSP (2nd edition), pp. 95–96

  28. Head First Servlets and JSP (2nd edition), pp. 95–96

  29. Head First Servlets and JSP (2nd edition), pp. 95–96

  30. Head First Servlets and JSP (2nd edition), pp. 95–96

  31. Head First Servlets and JSP (2nd edition), pp. 95–96

  32. Head First Servlets and JSP (2nd edition), pp. 95–96

  33. Summary • Web architecture • HTTP and HTML • Web app architecture • Servlet web app architecture http://flic.kr/p/YSY3X

More Related