25
Advanced Java Session 6 New York University School of Continuing and Professional Studies

Advanced Java Session 6 New York University School of Continuing and Professional Studies

Embed Size (px)

Citation preview

Page 1: Advanced Java Session 6 New York University School of Continuing and Professional Studies

Advanced JavaSession 6

New York University

School of Continuing and Professional Studies

Page 2: Advanced Java Session 6 New York University School of Continuing and Professional Studies

2

Objectives

• 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

Page 3: Advanced Java Session 6 New York University School of Continuing and Professional Studies

3

Web Applications Architecture

clientWebserver

Appserver

BackendServices such as db, quote server

Page 4: Advanced Java Session 6 New York University School of Continuing and Professional Studies

4

Java Servlets

• Modern replacement for CGIs• Server components written in Java• Advantages of Servlets

– Efficient– State Management– Portable– Robust– Extensible– Quick Development– Widespread Acceptance

Page 5: Advanced Java Session 6 New York University School of Continuing and Professional Studies

5

Application Server Model

services

Servlet container

servlets

Page 6: Advanced Java Session 6 New York University School of Continuing and Professional Studies

6

Classes and Interfaces

• Servlet

• ServletConfig

• ServletContext

• GenericServlet

• ServletRequest

• ServletResponse

• HttpServlet

• HttpServletRequest

• HttpServletResponse

• HttpSession

• Cookie

Page 7: Advanced Java Session 6 New York University School of Continuing and Professional Studies

7

GenericServlet

• Abstract class

• public void init() throws ServletException

• public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException

• public void destroy()

Page 8: Advanced Java Session 6 New York University School of Continuing and Professional Studies

8

HttpServlet

• Abstract class

• protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException

• protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException

Page 9: Advanced Java Session 6 New York University School of Continuing and Professional Studies

9

BasicServlet example

• tomcat env• javac BasicServlet.java• servit BasicServlet.class• tomcat start• http://localhost:8080/servlet/BasicServlet

Page 10: Advanced Java Session 6 New York University School of Continuing and Professional Studies

10

Accessing Form Data

• String getParameter(String)• Enumeration getParameterNames()• ShowParameters.java

Page 11: Advanced Java Session 6 New York University School of Continuing and Professional Studies

11

Accessing HTTP Headers

• String getMethod()

• String getRequestURI()

• String getProtocol()

• Enumeration getHeaderNames()

• ShowRequestHeader.java

Page 12: Advanced Java Session 6 New York University School of Continuing and Professional Studies

12

Accessing CGI Variables

• String getAuthType()

• String getContentLength()

• String getContentType()

• …….

• More complete list is in the handout.

• ShowCGIVariables.java

Page 13: Advanced Java Session 6 New York University School of Continuing and Professional Studies

13

Generating Response

• setContentType()

• getWriter()

• ……

• More in the handout.

Page 14: Advanced Java Session 6 New York University School of Continuing and Professional Studies

14

Handling Sessions

• Object getAttribute(String name)• void setAttribute(String name, Object

value)• Enumeration getAttributeNames()• void removeAttribute(String name)• void Invalidate()• ShowSession.java

Page 15: Advanced Java Session 6 New York University School of Continuing and Professional Studies

15

Java Server Pages

• Use embedded java code in HTML files

• “automatically generated” servlet

• Part of the J2EE framework

• Supported by most Application Servers

Page 16: Advanced Java Session 6 New York University School of Continuing and Professional Studies

16

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

Page 17: Advanced Java Session 6 New York University School of Continuing and Professional Studies

17

Embedded Tags

• <% …. Java code ….. %>

• <%@ page …. Page directive …. %>

• <%! Java methods/instance vars %>

• <%= … output …. %>

Page 18: Advanced Java Session 6 New York University School of Continuing and Professional Studies

18

Embedded Java code

<html>

<head><title>hello</title></head>

<body bgcolor=#ffffff>

<% for( int I=0; I<10;I++) { %>

<li>item number <%=I%>

<% } %>

</body>

</html>

Page 19: Advanced Java Session 6 New York University School of Continuing and Professional Studies

19

<%@ page directives

Language

Import

contentType

errorPage

Extends

isErrorPageisThreadSafeSessionautoFlushBuffer

Page 20: Advanced Java Session 6 New York University School of Continuing and Professional Studies

20

Using Java Beans in Java Server Pages

Basic Bean Use<jsp:useBean id=“book1” class=“Book”/>

Page 21: Advanced Java Session 6 New York University School of Continuing and Professional Studies

21

Accessing Bean Properties

<jsp:getProperty name=“book1” property=“title”/>

<jsp:setProperty name=“book1” property=“title”

value=“Developing Applications in J2EE”/>

Page 22: Advanced Java Session 6 New York University School of Continuing and Professional Studies

22

Associating properties with HTML input parameters

<jsp:setProperty name=“book1” property=“title” param=“title”/>

BookEntry example

Page 23: Advanced Java Session 6 New York University School of Continuing and Professional Studies

23

Extending JSP with custom tags<%@ taglib uri=“/hello”

prefix=“mytag”%>

<mytag:DoSomething>

body text

</mytag:DoSomething>TagSupport class

implements Tag

BodyTagSupport class implements BodyTag

Tag Library Descriptor

Page 24: Advanced Java Session 6 New York University School of Continuing and Professional Studies

24

Integrating Servlets andJava Server Pages

• Use servlets for presentation logic

• User jsp for layout

• Jsp can also generate XML

• Examples: ShowPage.java ShowError.java

Page 25: Advanced Java Session 6 New York University School of Continuing and Professional Studies

25

Thank you