Upload
christal-warner
View
216
Download
0
Tags:
Embed Size (px)
Citation preview
Advanced JavaSession 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
3
Web Applications Architecture
clientWebserver
Appserver
BackendServices such as db, quote server
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
5
Application Server Model
services
Servlet container
servlets
6
Classes and Interfaces
• Servlet
• ServletConfig
• ServletContext
• GenericServlet
• ServletRequest
• ServletResponse
• HttpServlet
• HttpServletRequest
• HttpServletResponse
• HttpSession
• Cookie
7
GenericServlet
• Abstract class
• public void init() throws ServletException
• public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException
• public void destroy()
8
HttpServlet
• Abstract class
• protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
• protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
9
BasicServlet example
• tomcat env• javac BasicServlet.java• servit BasicServlet.class• tomcat start• http://localhost:8080/servlet/BasicServlet
10
Accessing Form Data
• String getParameter(String)• Enumeration getParameterNames()• ShowParameters.java
11
Accessing HTTP Headers
• String getMethod()
• String getRequestURI()
• String getProtocol()
• Enumeration getHeaderNames()
• ShowRequestHeader.java
12
Accessing CGI Variables
• String getAuthType()
• String getContentLength()
• String getContentType()
• …….
• More complete list is in the handout.
• ShowCGIVariables.java
13
Generating Response
• setContentType()
• getWriter()
• ……
• More in the handout.
14
Handling Sessions
• Object getAttribute(String name)• void setAttribute(String name, Object
value)• Enumeration getAttributeNames()• void removeAttribute(String name)• void Invalidate()• ShowSession.java
15
Java Server Pages
• Use embedded java code in HTML files
• “automatically generated” servlet
• Part of the J2EE framework
• Supported by most Application Servers
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
17
Embedded Tags
• <% …. Java code ….. %>
• <%@ page …. Page directive …. %>
• <%! Java methods/instance vars %>
• <%= … output …. %>
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>
19
<%@ page directives
Language
Import
contentType
errorPage
Extends
isErrorPageisThreadSafeSessionautoFlushBuffer
20
Using Java Beans in Java Server Pages
Basic Bean Use<jsp:useBean id=“book1” class=“Book”/>
21
Accessing Bean Properties
<jsp:getProperty name=“book1” property=“title”/>
<jsp:setProperty name=“book1” property=“title”
value=“Developing Applications in J2EE”/>
22
Associating properties with HTML input parameters
<jsp:setProperty name=“book1” property=“title” param=“title”/>
BookEntry example
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
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
25
Thank you