Upload
meira
View
22
Download
0
Embed Size (px)
DESCRIPTION
Lecture 10 Object Oriented Programming in Java. Advanced Topics Servlets. Today’s Lecture. Trail: Servlets Lesson:overview,Cient Interaction, LifeCycle,... http://web2.java.sun.com/docs/books/tutorial/servlets/index.html Additional Link - PowerPoint PPT Presentation
Citation preview
June 1, 2000 Object Oriented Programming in Java (95-707)Advanced Topics
1
Lecture 10Object Oriented Programming in Java
Advanced TopicsServlets
June 1, 2000 Object Oriented Programming in Java (95-707)Advanced Topics
2
Today’s Lecture
• Trail: Servlets• Lesson:overview,Cient Interaction, LifeCycle,... • http://web2.java.sun.com/docs/books/tutorial/servlets/index.html
• Additional Linkhttp://developer.java.sun.com/developer/onlineTraining/Servlets/Fundamentals/
introduction.html
June 1, 2000 Object Oriented Programming in Java (95-707)Advanced Topics
3
Servlet Architecture
ClientBrowser
Web server with servlet engine
HTTP Servlet
1: send http request
2: call servlet with request info
5: send HTTP response
4: return informationfrom servlet engine 3: perform logic
June 1, 2000 Object Oriented Programming in Java (95-707)Advanced Topics
4
1: send HTTP request
• Handled by regular html tags• examples:
– simplehttp://localhost:8080/servlet/snoop
– with parametershttp://localhost:8080/servlet/snoop?username=alain
– from a form post<form action=“/servlet/snoop” method=“post”>
<input type="text" name="username" value=""><input type="text" name=”password" value="">
</form>
June 1, 2000 Object Oriented Programming in Java (95-707)Advanced Topics
5
2: call servlet with request info
• Handled by the web server• transparent to the developer
June 1, 2000 Object Oriented Programming in Java (95-707)Advanced Topics
6
3: perform logic
• Handled by a custom java class which inherits from the Servlet class
June 1, 2000 Object Oriented Programming in Java (95-707)Advanced Topics
7
4: return response from servlet engine
• Handled by the web server• transparent to the developer
June 1, 2000 Object Oriented Programming in Java (95-707)Advanced Topics
8
5: final step
• The browser renders the html page which is returned
• In this class we will only deal with HTML, but others types could be returned by servlets– I.e.: audio, images, ...
June 1, 2000 Object Oriented Programming in Java (95-707)Advanced Topics
9
Overall Servlet API
• base class is HttpServlet• APIs to:
– initialize servlets– receive http requests– retrieve information on the request– send a response to the browser– maintain session across multiple invocations
(remember that HTTP is session-less)
June 1, 2000 Object Oriented Programming in Java (95-707)Advanced Topics
10
Base Class: HttpServlet
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HelloWorldServlet extends HttpServlet {
…
}
Example from HelloWorldServlet:
June 1, 2000 Object Oriented Programming in Java (95-707)Advanced Topics
11
Initialization of Servlet
• Example from PhoneServlet.javapublic class PhoneServlet extends HttpServlet {
private Hashtable phones = new Hashtable();
private String dataFilePath = "phonelist";
// Public Methods
public void init(ServletConfig conf)
throws ServletException {
super.init(conf);
String param = getInitParameter("phonelist");
if (param != null) {
dataFilePath = param;
}...
readFromFile();
}
}
June 1, 2000 Object Oriented Programming in Java (95-707)Advanced Topics
12
HTTP GET and POST• Example from PhoneServlet:public class PhoneServlet extends HttpServlet {
public void init(ServletConfig conf)throws ServletException {
...
}
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
...
}
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws IOException
{
...
}
}
• doGet is called when we have HTTP GET• doPost is called when we have HTTP POST
June 1, 2000 Object Oriented Programming in Java (95-707)Advanced Topics
13
Inside doGet and doPost
• Three usual steps:– read request data such as input parameters– set response headers
• length, type
– write the response data
June 1, 2000 Object Oriented Programming in Java (95-707)Advanced Topics
14
Get Request Data and Parameters
• Example from PhoneServlet
public void doPost (HttpServletRequest req, HttpServletResponse res)throws IOException
{
...
String name, number, opcode;
name = req.getParameter("name");
number = req.getParameter("number");
opcode = req.getParameter("opcode");
...
} <form action=“/servlet/PhoneServlet” method=“post”><input type="text" name=”name" value=""><input type="text" name=”number" value=""><input type="text" name=”oncoder" value="">
</form>
June 1, 2000 Object Oriented Programming in Java (95-707)Advanced Topics
15
Set Response Headers
• Example from PhoneServlet: public void doGet (HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{
res.setContentType("text/html");
ServletOutputStream out = res.getOutputStream();
String title = "Phones";
...
out.println("<HEAD><TITLE> Phone Servlet Output </TITLE></HEAD>");
out.println("<BODY BGCOLOR=\"#FFFFFF\">");
out.println("<h1>Phone Records</h1>");
out.println("<p>");
...
)
June 1, 2000 Object Oriented Programming in Java (95-707)Advanced Topics
16
Create response
• Example from PhoneServlet: public void doGet (HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{
res.setContentType("text/html");
ServletOutputStream out = res.getOutputStream();
String title = "Phones";
...
out.println("<HEAD><TITLE> Phone Servlet Output </TITLE></HEAD>");
out.println("<BODY BGCOLOR=\"#FFFFFF\">");
out.println("<h1>Phone Records</h1>");
out.println("<p>");
...
)
June 1, 2000 Object Oriented Programming in Java (95-707)Advanced Topics
17
HTTP protocol is session-less
• HTTP protocol is session-less– between two requests the web server does not
remember the caller
• protocol is as follows:– browser connects to HTTP server– browser makes a request for a web resource– web server looks for the resource– web server forwards the resource to the browser– web server disconnects the browser
June 1, 2000 Object Oriented Programming in Java (95-707)Advanced Topics
18
Sessions with Servlets
• The servlet engine fills-in the gap and provides a way to keep a session across multiple requests to a web server
• Mechanism is handled by the servlet engine “on demand” by the servlets
• The base class is HttpSession
June 1, 2000 Object Oriented Programming in Java (95-707)Advanced Topics
19
HttpSession
• Example from SessionServlet.java public void doGet (HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{
HttpSession session = req.getSession(true);
...
//Here's the meat
Integer ival = (Integer) session.getValue("sessiontest.counter");
if (ival==null) ival = new Integer(1);
else ival = new Integer(ival.intValue() + 1);
session.putValue("sessiontest.counter", ival);
...
)