Download presentation
Presentation is loading. Please wait.
Published byEarl Cross Modified over 9 years ago
2
Advanced Java Session 6 New York University School of Continuing and Professional Studies
3
2 Objectives Web Application Architecture and HTTP Java Servlets –BasicServlet –Accessing Form Data, HTTP Headers, CGI Variables –Cookies and Sessions Java Server Pages –Overview of Java Server Pages –Using Java Beans in JSP Integrating Java Servlets and JSP
4
3 Web Applications Architecture client Web server App server Backend Services - db, quote server IE, safari,.. Apache, IIS,.. Tomcat, jBoss, WebLogic, WebSphere, IIS,.. Tier 1 Tier 2Tier 3
5
4 HTTP Client makes an HTTP request – –http:// : / / Server simply picks up the document and returns it back to the client
6
5 Sample Request GET / / HTTP/1.0 User-agent: Internet Explorer Host: mymachine Request Headers End of Headers
7
6 Sample Response HTTP/1.1 200 OK Content-Length: 1433 Content-Type: text/html Last-Modified: Fri, 21 Feb 2003 22:48:30 GMT Server: Microsoft-IIS/6.0 Date: Sun, 08 Jul 2007 01:08:52 GMT Under Construction Headers End of Headers Body
8
7 CGI Common Gateway Interface allows a response to be generated by a program Server simply invokes the program and passes it the “socket stream”. Program writes the response to the stream – and thereby to the client Parameters are sent on the URL (or in the body in POST) GET http://www.google.com/search?q=servletshttp://www.google.com/search?q=servlets POST http://www.google.com/search - body after the headers will contain “q=servlets”http://www.google.com/search Special characters must be “encoded”
9
8 Java Servlets Modern replacement for CGIs Server components written in Java Advantages of Servlets –Efficient –State Management –Portable –Robust –Extensible –Quick Development –Widespread Acceptance
10
9 Application Server Model services Servlet container servlets
11
10 Java Servlets Are not complete programs with a “main” Run inside a “container” such as Tomcat, or WebLogic When the “container” receives the first request for a servlet, it creates an instance of it and calls its “init” method and then calls the “service()” method which in turn calls doGet() or doPost All subsequent requests directly call the service() method (doGet method or doPost method depending on the type of request) If multiple requests are received simultaneously then the container creates multiple threads and calls the “same” instance of the servlet from multiple threads Current Version of the Specification is 2.5
12
11 Java Servlets Container GET /myservlet Load class myservlet Constructor init() service() destroy() (depends)
13
12 Classes and Interfaces Servlet ServletConfig ServletContext GenericServlet ServletRequest ServletResponse HttpServlet HttpServletRequest HttpServletResponse HttpSession Cookie
14
13 GenericServlet Abstract class public void init() throws ServletException public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException public void destroy()
15
14 HttpServlet Abstract class protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
16
15 BasicServlet example tomcat env javac BasicServlet.java servit BasicServlet.class tomcat start http://localhost:8080/servlet/BasicServlet
17
16 Accessing Form Data String getParameter(String) Enumeration getParameterNames() ShowParameters.java
18
17 Accessing HTTP Headers String getMethod() String getRequestURI() String getProtocol() Enumeration getHeaderNames() ShowRequestHeader.java
19
18 Accessing CGI Variables String getAuthType() String getContentLength() String getContentType() ……. More complete list is in the handout. ShowCGIVariables.java
20
19 Generating Response setContentType() getWriter() …… More in the handout.
21
20 Handling Sessions Object getAttribute(String name) void setAttribute(String name, Object value) Enumeration getAttributeNames() void removeAttribute(String name) void Invalidate() ShowSession.java
22
21 Java Server Pages Use embedded java code in HTML files “automatically generated” servlet Part of the J2EE framework Supported by most Application Servers Current version of specification is 2.1
23
22 Jsp Servlet Java Server Pages turn into a servlet Engine translates JSP into a java servlet, compiles it and executes it. HttpJspPage interface is a subclass of Servlet JSP engine provides a class that implements HttpJspPage interface _jspService method is the main entry point
24
23 Embedded Tags
25
24 Embedded Java code hello item number
26
25 <%@ page directives Language Import contentType errorPage Extends isErrorPage isThreadSafe Session autoFlush Buffer
27
26 Using Java Beans in Java Server Pages Basic Bean Use
28
27 Accessing Bean Properties <jsp:setProperty name=“book1” property=“title” value=“Developing Applications in J2EE”/>
29
28 Associating properties with HTML input parameters BookEntry example
30
29 Extending JSP with custom tags body text
31
30 Making a Tag Handler Write a class that extends SimpleTagSupport Implement doTag() method Implement any “set…” methods if your tag expects attributes (e.g setAttr1) Create a TLD (tag library descriptor) for the tag Deploy the tag handler and the TLD (put it in WEB-INF/classes using the package directory structure)
32
31 Integrating Servlets and Java Server Pages Use servlets for presentation logic User jsp for layout Jsp can also generate XML Examples: ShowPage.java ShowError.java
33
32 Web Applications Collection of Servlets, JSPs, other Java Classes, HTML pages, CSS, images, and any other elements (such as Flash, Applets, and others) Packaged together in a single “war” file for easy transportability and deployment
34
33 Attributes “Temporary” storage for Servlet and JSP components Can be placed in ServletContext, Request, or Session Any object can be placed using the setAttribute( key, value ) call and can be reetrieved using the getAttribute(key) call
35
34 Scopes ServletContext: available throughout your application to all components of your application Request: available only during the processing of a request to the components which have the “request” object Session: available to all components of your application during its lifetime that begins with the first request from a client (end is determined by configuration)
36
35 HttpSessionBindingListener interface Classes that are used to “store” temporary data using setAttribute can implement this interface. valueBound is called when an instance is “bound” to an active HttpSession valueUnbound is called when an instance is “unbound” from a HttpSession – in other words – when the “session” is closed or is destroyed
37
36 Thread Safety issues Servlets are called from multiple threads by the container – therefore must be coded in a Thread Safe manner All access to context variables must be “synchronized” All access to session variables must also be “synchronized”
38
37 Clustering Issues Application Servers may be “clustered” – i.e. more than one server may be set up to service clients In that event, sessions must be migrated to all “active” servers
39
38 Thank you
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.