Professional Documents
Culture Documents
Outline: Servlet Lifecycle Request and Response Session Management Javaserver Pages (JSP)
Outline: Servlet Lifecycle Request and Response Session Management Javaserver Pages (JSP)
Servlet API
javax.servlet
Servlet (interface)
GenericServlet (class)
javax.servlet.http
https://glassfish.dev.java.net/nonav/javaee5/api/index.html
service()
doGet()
doPost()
User-defined Servlets
To
Hello World
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
}
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
doGet(req, res);
End-to-end Process
Client
Web Server
Servlet Lifecycle
Servlet Container
Servlet
Servlet Container
Loading Servlet
After
init()
Servlet
initialization is defined
May be called
Three types
Standalone container
Add-on container
Embeddable container
When
When
12
Removing Servlet
After
destroy()
Resources
13
14
A Simple Counter
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
Main Process
Request for
Servlet1
Request for
Setvlet2
Request for
Servlet1
Thread
JVM
Servlet1
Thread
Thread
Servlet2
}
15
16
Multi-threading
Thread Safety
the example
count ++
count ++
out.println
out.println
17
// thread 1
// thread 2
// thread 1
// thread 2
Synchronization
Option 2
count++;
out.println("Since loading, this servlet has been accessed " + count + " times.");
}
out.println("Since loading, this servlet has been accessed " + count + " times.");
Reduced parallelism
Increased overhead to obtain the monitor
Option 4
19
20
HttpServletRequest
from ServletRequest
getParameter()
https://glassfish.dev.java.net/nonav/javaee5/api/index.html
getParameterNames()
getParameterValues()
getInputStream()
getReader()
21
22
HttpServletResponse
Convenience methods
getWriter(), getOutputStream()
23
POST
Primitive manipulation
GET
24
Session Management
Session Management
Hidden
variables
rewriting
User authentication
URL
getRemoteUser()
Provided by HttpServletRequest
Server-side
Servlets
25
26
Persistent Cookies
Adding a Cookie
As
name=value pairs
Browsers limit the size of cookies
Users can refuse to accept them
Included
Cookie
Cookie(String name,
String value)
setMaxAge(int expiry)
setDomain(String pattern)
setPath(String uri)
setSecure(boolean flag)
HttpServletResponse
addCoockie(Cookie cookie)
MaxAge
Domain
Path
Given in seconds
If negative, cookie persists until
browser exists
Secure flag
27
28
Retrieving Cookies
HttpServletRequest
public
Cookie[] getCookies()
Cookie
getName()
getValue()
getDomain()
getPath()
getSecure()
29
Session Attributes
31
32
Session Lifecycle
Default
boolean isNew()
Explicitly
30 min
void invalidate()
long getCreationTime()
invalidated by servlet
The
Returns true if the client does not yet know about the session or if
the client chooses not to join the session
long getLastAccessedTime()
Returns the last time the client sent a request associated with this
session, as the number of milliseconds since midnight January 1,
1970 GMT, and marked by the time the container received the
request
33
34
ServletContext
<session-config>
<session-timeout>
30 <! minutes -->
</session-timeout>
</session-config>
Configured individually
Terminating a session
session.invalidate()
Deletes session related attributes form server
Removes cookie from client
35
JavaServer Pages
(JSP)
<body>
<h1>JSP Example</h1>
<hr/>
<p>
<% out.println("Hello " + request.getParameter("name")); %>
<%= "Hello again, " + request.getParameter("name") + "!"%>
</p>
</body>
</html>
37
38
JavaServer Pages
Embed
.jsp file
Purposes
Web Server
request
response
Server
<html>
<head>
</head>
<body>
<% %>
</body>
</html>
Java
Compiler
Servlet
39
40
XHTML
Scriptlet
Forward a request
Directives
<jsp:include>
Action tags
42
Microsoft-specific technology
Similar to JSP
Let a JSP page contain XML tags that cause specific Java
code to be executed
<%@ taglib prefix=c
uri=http://java.sun.com/jsp/jstl/core %>
Use
Can
<c:out value=${param.address}/>
44