Presentation is loading. Please wait.

Presentation is loading. Please wait.

Building Web Apps with Servlets

Similar presentations


Presentation on theme: "Building Web Apps with Servlets"— Presentation transcript:

1 Building Web Apps with Servlets

2 Why web apps? Create apps every bit as dynamic, interactive, and custom tailored as native apps Avoid deployment problems Reach people world wide

3 First, let’s review some basics about how 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 browser so that it can request a web page? A URL! Head First Servlets and JSP (2nd edition), p. 4

6 Anatomy of a URL Port Path Server Protocol Resource

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

8 Example HTTP GET request
HTTP Method Path to resource Protocol version GET /select/selectBeerTaste.jsp HTTP/1.1 Host: 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 ,utf-8;q=0.7,*;q=0.7 Keep-Alive: 300 Connection: keep-alive Request headers

9 Text version of status code
Example HTTP response Protocol version HTTP status code Text version of status code HTTP/ OK Content-Type: text/html Content-Length: 397 Date: Wed, 19 Nov :25:40 GMT Server: Apache-Coyote/1.1 Connection: close <html> </html> Response headers 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 Dynamic Web Pages (Web Apps)
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 But how do you pass parameters?
Recall step #2… But how do you pass 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
HTTP Method Path to resource Protocol version POST /select/selectBeerTaste.do HTTP/1.1 Host: 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 ,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

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

22 Servlet web app architecture
Java EE provides You write 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 ” ?> <web-app xmlns= 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

26 To make sure you got it, let’s walk through another servlet 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
Summary Web architecture HTTP and HTML Web app architecture Servlet web app architecture


Download ppt "Building Web Apps with Servlets"

Similar presentations


Ads by Google