objectives
play

Objectives Review Servlets Deployment Configuration Sessions, - PDF document

Objectives Review Servlets Deployment Configuration Sessions, Cookies Handling multiple requests Apr 29, 2019 Sprenkle - CS335 1 Servlets Review What application do we need to execute servlets? What class do all web


  1. Objectives • Review Servlets • Deployment • Configuration • Sessions, Cookies • Handling multiple requests Apr 29, 2019 Sprenkle - CS335 1 Servlets Review • What application do we need to execute servlets? • What class do all web servlets extend? • What methods do servlets need to override to handle GET and POST requests? • How do servlets send an HTML document/response to the client? • How do servlets get data from the client? • Put it all together: how do you create a dynamic web page, i.e., a web page that processes a request from a form? • What tricks did you learn to help you with debugging? Apr 29, 2019 Sprenkle - CS335 2 1

  2. Example Servlet Flow HTML s u b m Form i t HTTP Server Request, data doGet Web Client Application Response Server Web Browser HTML Document Apr 29, 2019 Sprenkle - CS335 3 Servlet Development Discussion • Distributed applications are difficult to debug and test Ø Multiple components: Client code? Server code? • Suggestions Ø Use Eclipse to help you find errors in HTML Ø Check response’s HTML source code • Shows you what was written to output • Location of error Ø Use Eclipse’s debugger Web App Server Client Apr 29, 2019 Sprenkle - CS335 4 2

  3. More on Java-based Web Applications • Structure • Other classes • Initialization, customization • Synchronization Apr 29, 2019 Sprenkle - CS335 5 Web App Directory Structure • Different from • projectname projectname / Eclipse code organization • When Eclipse deploys Ø HTML, CSS, and JSP files the web application, it • projectname projectname/WEB /WEB-INF INF organizes it this way. Ø Other resources, e.g., web.xml web.xml • projectname projectname/WEB /WEB-INF/classes INF/classes Ø Servlet and utility (data structures, etc) Ø Why we put our servlets in servlets package • projectname projectname/WEB /WEB-INF/lib INF/lib Ø Jar files that application depends on Apr 29, 2019 Sprenkle - CS335 6 3

  4. Servlet Interface Methods • init(ServletConfig init(ServletConfig config config) Ø Web app server calls once to initialize the servlet Ø Typically opening DB connection, files • ServletConfig ServletConfig getServletConfig getServletConfig() () Ø Returns a reference to a ServletConfig ServletConfig • void void service(ServletRequest service(ServletRequest, , ServletResponse ServletResponse) Ø Called to respond to a client request • String String getServletInfo getServletInfo() () Ø Returns a String that describes the servlet (name, version, etc.) • void destroy() void destroy() Ø Called by the server to terminate a servlet Ø Should close open files, close DB connections, etc. Apr 29, 2019 Sprenkle - CS335 7 Servlet Life Cycle in SurveyServlet Web Application Server Parameter Servlet Web Application Server • Web application server creates one instance of servlet Ø Calls init init method of servlet created • As requests come in, WAS calls service service method of appropriate servlet Ø In turn, servlet calls appropriate doMethod doMethod • When web application server shuts down, calls destroy method of each servlet destroy Apr 29, 2019 Sprenkle - CS335 8 4

  5. Lab 4: Refactoring SurveyServlet • Currently: Inefficient implementation Ø Read, write survey data file every time request is executed • In init init Ø Automatically called by server on start up Ø Open file, read/initialize votes • In destroy destroy Ø Automatically called by server Ø Write file Apr 29, 2019 Sprenkle - CS335 9 Servlet Data Servlet • ServletConfig ServletConfig – initialization and startup parameters for this servlet Ø Example methods: • String getInitParameter String getInitParameter (String name) • String String getServletName getServletName() () Same method name, different context • ServletContext ServletContext – servlet container information Ø Example methods: • Object getAttribute getAttribute (String name) • String getInitParameter getInitParameter (String name) Apr 29, 2019 Sprenkle - CS335 10 5

  6. ServletContext ServletContext • One ServletContext per web application per JVM Ø If you have both Lab3 and FirstServlets running on Tomcat, they will each have their own ServletContext • Share state among multiple clients Ø Allow multiple users to interact in, e.g., chat rooms, online meeting, reservation systems • Info about servlet’s environment Ø E.g., server’s name • log() : method to write to a log file • Context attributes Ø getAttribute, setAttribute, removeAttribute Apr 29, 2019 Sprenkle - CS335 11 web.xml File web.xml • Describes how to deploy the web application • XML file <tag attr="value"> Content Ø Used for data </tag> Ø Marked up with elements Ø Same rules as XHTML: close most recently opened tag, attributes in quotes • DTD: Document Type Definition Ø Define elements that can be in a particular XML document Ø Includes specification of attributes, nesting Apr 29, 2019 Sprenkle - CS335 12 6

  7. Annotations • In Servlets 3.x, we can easily configure a web application using annotations Ø Don’t need to directly update web.xml Ø Provide defaults, can be overridden in web.xml • Example: @WebServlet("/SurveyServlet") public class SurveyServlet extends HttpServlet { Ø Means the URL pattern “/SurveyServlet” maps to this servlet (servlets.SurveyServlet) Apr 29, 2019 Sprenkle - CS335 13 Add init parameters Apr 29, 2019 Sprenkle - CS335 14 7

  8. Another Annotation Example @WebServlet( urlPatterns = { "/SurveyServlet" }, initParams = { @WebInitParam(name = "surveyFile", value = "survey.dat") }) public class SurveyServlet extends HttpServlet { Default values Can override these in the web.xml Why would we want to be able to override these values in a separate (text) file? Apr 29, 2019 Sprenkle - CS335 15 Why web.xml overriding? • Can modify behavior of application without modifying the Java code and recompiling Ø May not have access to source code Apr 29, 2019 Sprenkle - CS335 16 8

  9. web.xml File web.xml • Top-level: <webapp webapp> • <servlet servlet> element describes a servlet • <servlet <servlet-mapping> mapping> element maps URLs to servlets Ø May want to have shorthands, aliases Ø Restrict users’ direct access to servlets Apr 29, 2019 Sprenkle - CS335 17 web.xml File: Subelements of web.xml <servlet> <servlet> <servlet servlet-name> name> canonical name of the deployed servlet <servlet servlet-class> class> fully qualified class name of the servlet <init <init-param param> optional parameter containing a name-value pair that is passed to the servlet on initialization. Contains elements, <param-name> and <param-value> , which contain the name and value, respectively, to be passed to the servlet. Apr 29, 2019 Sprenkle - CS335 18 9

  10. Example of Configuring web.xml • Configure SurveyServlet to use a given file • Add the following to web.xml file: <init-param> <param-name>surveyFile</paramname> <param-value>survey.dat</param-value> </init-param> • Note that <init-param> is a child of <servlet> , which means your web.xml file would look like what? Apr 29, 2019 Sprenkle - CS335 19 Note about init-params in web.xml • If you set init-param in web.xml, you need to annotate the servlet with its name • You can have multiple configurations for the same [servlet] class Ø Using the name lets the application server know that the annotations and the web.xml configurations are both part of the same configuration Apr 29, 2019 Sprenkle - CS335 21 10

  11. Using Init Parameter • Configure SurveyServlet to use a given file Ø Either in annotation or web.xml • Modify init method to call HttpServlet HttpServlet ’s getInitParameter method getInitParameter // calls HttpServlet method, i.e., this’s method filename = getInitParameter("surveyFile"); // open file … Apr 29, 2019 Sprenkle - CS335 22 MAINTAINING STATE ACROSS REQUESTS Apr 29, 2019 Sprenkle - CS335 23 11

  12. Maintaining State • If you have multiple pages, how can you save or accumulate data? Ø Example scenario: buying a book Login Which Credit Verify Form Book Card D1, D2, D1, D2, D1 D1, D2 D3 D3, D4 D1 D1, D2 D1, D2, D3 Server Server Server Server Apr 29, 2019 Sprenkle - CS335 24 Maintaining State • If you have multiple pages, how can you save or accumulate data? Ø Hidden fields ( type=hidden type=hidden ) Ø Cookies Ø Sessions Login Which Credit Verify Form Book Card D1, D2, D1, D2, D1 D1, D2 D3 D1 D1, D2, D3, D4 D1, D2 D3 Server Server Server Server Apr 29, 2019 Sprenkle - CS335 25 12

  13. Hidden Fields <input type="hidden" name="userid" value="superfly"/> • Data is coming from client • Users can see the hidden fields Ø View HTML Source • Users can change the data ➨ Useful in limited situations Apr 29, 2019 Sprenkle - CS335 26 COOKIES Apr 29, 2019 Sprenkle - CS335 27 13

Recommend


More recommend