Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Servlets 1: Lecture Slides - Introduction Software Engineering | ECS 160, Study notes of Software Engineering

Material Type: Notes; Class: Intro Software Eng; Subject: Engineering Computer Science; University: University of California - Davis; Term: Spring 2006;

Typology: Study notes

Pre 2010

Uploaded on 07/30/2009

koofers-user-d36
koofers-user-d36 🇺🇸

10 documents

1 / 23

Toggle sidebar

Related documents


Partial preview of the text

Download Servlets 1: Lecture Slides - Introduction Software Engineering | ECS 160 and more Study notes Software Engineering in PDF only on Docsity! 1 Servlets1 Fatemeh Abbasinejad abbasine@cs.ucdavis.edu 2 What are Servlets? A program that runs on a web server acting as middle layer between requests coming from a web browser and databases or applications on the Http server 2 3 Where are they? Client Server Servlets HTTP Thin clients applications that require minimal client side support 4 Their job Read any data sent by the user Generate results (from db,…) Format the result inside a document (embedding inside HTML) Send the document back to the client 5 9 Interfaces Defines a protocol behavior that can be implemented by any class anywhere It defines a set of methods But…. does not implement them A collection of methods without implementation 10 Interfaces… A class that implements the interface agrees to implement all the methods Then what were abstract classes?! Interface can not implement any methods, abstract class can implement any A class can implement many interfaces but can only have one super class 6 11 Servlet’s Lifecycle Servlet container loads the servlet into memory in response to a request Servlet container invokes the servlet’s init() method When init() finishes… servlet responds with its service() method service() receives request, processes it, sends response 12 Servlet Lifecycle… service() is only called once per request New requests results in a new thread When servlet container terminates … destroy() is called to release resources. 7 13 Servlet Package Servlet package has 2 abstract classes that implement the interface servlet Servlet GenericServlet HttpServlet implements implements an abstract class an abstract class 14 Servlet Package… The classes have default implementations of “all” servlet’s methods Why all?? Most servlets override these methods by extending them 10 19 Exceptions thrown If doGet() and doPost() are unable to handle a client’s request it throws an exception of type ServletException If doGet() and doPost() encounter an error during system processing (reading/writing) it throws a IOException 20 Get and Post Overview The most common request types (request methods) <form method = “Post” action = “/someplace”> 11 21 Post appends form data to the browser request then a script on the server can access the form Browsers cache web pages…but not the server’s response to a post request Because: the next post might not return the same result Example: surveys ; each response changes the overall survey 22 Get appends the data directly to the end of the URL Such pages are often cached Example: search engines; in case you perform the search again! Action : specifies the URL of a script on the server 12 23 Simple Example <head> <title> Handling an HTTP Get Request </title> </head> <body> <form action = "welcome1" method = "get"> <p> <label> Click the button to invoke the servlet <input type = "submit" value = "Get HTML Document" /> </label> </p> </form> </body> </html> 24 Simple Example… 15 29 import javax.servlet.*; import javax.servlet.http.*; import java.io.*; public class WelcomeServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException { PrintWriter out = response.getWriter(); response.setContentType( "text/html" ); // head section of document out.println( "<head>" ); out.println( "<title>A Simple Servlet Example</title>" ); out.println( "</head>" ); // body section of document out.println( "<body>" ); out.println( "<h1>Welcome to Servlets!</h1>" ); out.println( "</body>" ); // end XHTML document out.println( "</html>" ); out.close(); // close stream to complete the page } } object obtained from HttpServletResponse 30 What and How do we send? Use response’s setContentType() to specify the content of the data to be sent In this example we use “txt/html” response.setContentType("text/html"); 16 31 import javax.servlet.*; import javax.servlet.http.*; import java.io.*; public class WelcomeServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException { PrintWriter out = response.getWriter(); response.setContentType( "text/html" ); // head section of document out.println( "<head>" ); out.println( "<title>A Simple Servlet Example</title>" ); out.println( "</head>" ); // body section of document out.println( "<body>" ); out.println( "<h1>Welcome to Servlets!</h1>" ); out.println( "</body>" ); // end XHTML document out.println( "</html>" ); out.close(); // close stream to complete the page } } Specify content to be sent 32 What and How do we send? With the out object’s println() method create an HTML document out.println (“<html_tag> somethig <html_tag>”) out.close() Flushes the output buffer and sends the informstion to the client 17 33 import javax.servlet.*; import javax.servlet.http.*; import java.io.*; public class WelcomeServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException { PrintWriter out = response.getWriter(); response.setContentType( "text/html" ); // head section of document out.println( "<head>" ); out.println( "<title>A Simple Servlet Example</title>" ); out.println( "</head>" ); // body section of document out.println( "<body>" ); out.println( "<h1>Welcome to Servlets!</h1>" ); out.println( "</body>" ); // end XHTML document out.println( "</html>" ); out.close(); } } declare head section close stream declare body section 34 Deployment Descriptor stored in “web.xml” specifies various config. parameter such as: the name used to invoke the servlet description of the servlet the class name of the servlet class 20 39 Another Example <head> <title> Handling an HTTP Post Request </title> </head> <body> <form action = "welcome2" method = “get”> <p> <label> Type your first name and press the Submit button <input type = “text" name = “firstname" /> <input type = "submit" value = “Submit" /> </label> </p> </form> </body> </html> invokes the servlet 40 Another Example… 21 41 Requests containing data parameters are passed as name/value pairs in get request request object’s getParameter() receives parameter name as argument String firstname = request.getParameter(“firstname”); 42 import javax.servlet.*; import javax.servlet.http.*; import java.io.*; public class WelcomeServlet2 extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException { String firstname = request.getParameter(“firstname”); PrintWriter out = response.getWriter(); response.setContentType( "text/html" ); // head section of document out.println( "<head>" ); out.println( "<title>Another Servlet Example</title>" ); out.println( "</head>" ); // body section of document out.println( "<body>" ); out.println( "<h1>Hello” + firstname + ”!</h1>" ); out.println( "</body>" ); // end XHTML document out.println( "</html>" ); out.close(); // close stream to complete the page } } receive parameter from request object 22 43 <?xml version="1.0" encoding="ISO-8859-1"?> <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4"> <web-app> <description> CS160 Example </description> <display-name> My Example2 </display-name> <servlet> <servlet-name> welcome2 </servlet-name> <servlet-class> WelcomeServlet2 </servlet-class> </servlet> <servlet-mapping> <servlet-name> welcome2 </servlet-name> <url-pattern> /welcome2 </url-pattern> </servlet-mapping> </web-app> map servlet to URL class’s name name chosen for servlet 44 get Requests with Data Note: the browser appends ?firstame=myname at the end of the action URL http://localhost:8080/servlets /WelcomeServlet2?firstname=myname What happens if you type in the above URL? more data: separated by &
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved