Lecture 10 Object Oriented Programming in Java Advanced
Lecture 10 Object Oriented Programming in Java Advanced Topics Servlets June 1, 2000 Object Oriented Programming in Java (95 -707) Advanced Topics 1
Today’s Lecture • Trail: Servlets • Lesson: overview, Cient Interaction, Life. Cycle, . . . • http: //web 2. java. sun. com/docs/books/tutorial/servlets/index. html • Additional Link http: //developer. java. sun. com/developer/online. Training/Servlets/Fundamentals/introduction. ht ml June 1, 2000 Object Oriented Programming in Java (95 -707) Advanced Topics 2
Servlet Architecture Web server with servlet engine Client Browser 1: send http request HTTP Servlet 5: send HTTP response June 1, 2000 2: call servlet with request info 4: return information from servlet engine Object Oriented Programming in Java (95 -707) Advanced Topics 3: perform logic 3
1: send HTTP request • Handled by regular html tags • examples: – simple http: //localhost: 8080/servlet/snoop – with parameters http: //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 4
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 5
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 6
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 7
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 8
Overall Servlet API • base class is Http. Servlet • 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 9
Base Class: Http. Servlet Example from Hello. World. Servlet: import java. io. *; import javax. servlet. http. *; public class Hello. World. Servlet extends Http. Servlet { … } June 1, 2000 Object Oriented Programming in Java (95 -707) Advanced Topics 10
Initialization of Servlet • Example from Phone. Servlet. java public class Phone. Servlet extends Http. Servlet { private Hashtable phones = new Hashtable(); private String data. File. Path = "phonelist"; // Public Methods public void init(Servlet. Config conf) throws Servlet. Exception { super. init(conf); String param = get. Init. Parameter("phonelist"); if (param != null) { data. File. Path = param; }. . . read. From. File(); } } June 1, 2000 Object Oriented Programming in Java (95 -707) Advanced Topics 11
HTTP GET and POST • Example from Phone. Servlet: public class Phone. Servlet extends Http. Servlet { public void init(Servlet. Config conf) throws Servlet. Exception {. . . } public void do. Get(Http. Servlet. Request req, Http. Servlet. Response res) throws Servlet. Exception, IOException {. . . } public void do. Post(Http. Servlet. Request req, Http. Servlet. Response res) throws IOException {. . . } } • do. Get is called when we have HTTP GET Objectwhen Oriented Programming (95 -707) • do. Post is called we havein Java HTTP POST June 1, 2000 Advanced Topics 12
Inside do. Get and do. Post • 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 13
Get Request Data and Parameters • Example from Phone. Servlet public void do. Post (Http. Servlet. Request req, Http. Servlet. Response res)throws IOException {. . . String name, number, opcode; name = req. get. Parameter("name"); number = req. get. Parameter("number"); opcode = req. get. Parameter("opcode"); . . . <form action=“/servlet/Phone. Servlet” 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 14
Set Response Headers • Example from Phone. Servlet: public void do. Get (Http. Servlet. Request req, Http. Servlet. Response res) throws Servlet. Exception, IOException { res. set. Content. Type("text/html"); Servlet. Output. Stream out = res. get. Output. Stream(); String title = "Phones"; . . . out. println("<HEAD><TITLE> Phone Servlet Output </TITLE></HEAD>"); out. println("<BODY BGCOLOR="#FFFFFF">"); out. println("<h 1>Phone Records</h 1>"); out. println("<p>"); . . . ) June 1, 2000 Object Oriented Programming in Java (95 -707) Advanced Topics 15
Create response • Example from Phone. Servlet: public void do. Get (Http. Servlet. Request req, Http. Servlet. Response res) throws Servlet. Exception, IOException { res. set. Content. Type("text/html"); Servlet. Output. Stream out = res. get. Output. Stream(); String title = "Phones"; . . . out. println("<HEAD><TITLE> Phone Servlet Output </TITLE></HEAD>"); out. println("<BODY BGCOLOR="#FFFFFF">"); out. println("<h 1>Phone Records</h 1>"); out. println("<p>"); . . . ) June 1, 2000 Object Oriented Programming in Java (95 -707) Advanced Topics 16
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 17
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 Http. Session June 1, 2000 Object Oriented Programming in Java (95 -707) Advanced Topics 18
Http. Session • Example from Session. Servlet. java public void do. Get (Http. Servlet. Request req, Http. Servlet. Response res) throws Servlet. Exception, IOException { Http. Session session = req. get. Session(true); . . . //Here's the meat Integer ival = (Integer) session. get. Value("sessiontest. counter"); if (ival==null) ival = new Integer(1); else ival = new Integer(ival. int. Value() + 1); session. put. Value("sessiontest. counter", ival); . . . ) June 1, 2000 Object Oriented Programming in Java (95 -707) Advanced Topics 19
- Slides: 19