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 Static Web Pages
The architecture of the Web Head First Servlets and JSP (2nd edition), p. 3
Typical web browser/server interaction http://flic.kr/p/9ksxQa At the very least, what must to give your browser so that it can request a web page? A URL! Head First Servlets and JSP (2nd edition), p. 4
Anatomy of a URL http://www.wickedlysmart.com:80/beeradvice/select/beer1.html Port Path Server Protocol Resource
So what do requests and responses actually look like anyway? Head First Servlets and JSP (2nd edition), p. 4
Example HTTP GET request HTTP Method Path to resource Protocol version 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 Request headers
Text version of status code Example HTTP response Protocol version HTTP status code 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> Response headers Response body
Let’s see the request/response in a bit more detail… Head First Servlets and JSP (2nd edition), p. 18
Head First Servlets and JSP (2nd edition), p. 18
Dynamic Web Pages (Web Apps) Now, let’s talk about … Dynamic Web Pages (Web Apps)
Typical architecture of a web app Head First Servlets and JSP (2nd edition), p. 27
Typical architecture of a web app Head First Servlets and JSP (2nd edition), p. 27
Typical architecture of a web app Head First Servlets and JSP (2nd edition), p. 27
Typical architecture of a web app Head First Servlets and JSP (2nd edition), p. 27
But how do you pass parameters? Recall step #2… But how do you pass parameters? Head First Servlets and JSP (2nd edition), p. 27
GET requests can embed parameters in the URL Parameter list Head First Servlets and JSP (2nd edition), p. 14
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
Example HTTP POST request HTTP Method Path to resource Protocol version 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 Request headers Request body How do you make a POST request? HTML forms. More on that in a minute
Now, here’s how you implement web apps using … Servlets
Servlet web app architecture Java EE provides You write Head First Servlets and JSP (2nd edition), p. 39
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
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>”); }
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> <url-pattern>/Serv1</url-pattern> </servlet-mapping> </web-app> Name in DD Java class URL path
To make sure you got it, let’s walk through another servlet request/response
Head First Servlets and JSP (2nd edition), pp. 95–96
Head First Servlets and JSP (2nd edition), pp. 95–96
Head First Servlets and JSP (2nd edition), pp. 95–96
Head First Servlets and JSP (2nd edition), pp. 95–96
Head First Servlets and JSP (2nd edition), pp. 95–96
Head First Servlets and JSP (2nd edition), pp. 95–96
Summary Web architecture HTTP and HTML Web app architecture http://flic.kr/p/YSY3X Summary Web architecture HTTP and HTML Web app architecture Servlet web app architecture