Presentation is loading. Please wait.

Presentation is loading. Please wait.

Michael Brockway Application Integration Servlets l Introduction & Overview l HTTP Servlets l HTTP get Requests l HTTP post Requests l Multi-tier Applications.

Similar presentations


Presentation on theme: "Michael Brockway Application Integration Servlets l Introduction & Overview l HTTP Servlets l HTTP get Requests l HTTP post Requests l Multi-tier Applications."— Presentation transcript:

1 Michael Brockway Application Integration Servlets l Introduction & Overview l HTTP Servlets l HTTP get Requests l HTTP post Requests l Multi-tier Applications Using JDBC from a Servlet l References

2 2 Introduction l References: Y D Liang, Intro to Java Programming, 7ed chapter 39 l Java’s support for distributed / internet- / web-based applications includes n Multithreading n Networking u java.net package u Socket-based communications, packet-based communications n Remote Method Calling u RMI (java.rmi package) u CORBA n Servlets u A client requests an action be performed; a server responds to client requests u An applet is a class that is downloaded to the client and performs functions there; u A servlet lives in the server and responds to requests from the client. u Basic packages are javax.servlet, javax.servlet.http t javax.servlet.jsp and javax.servlet.jsp.tagext comprise Java Server Pages (JSP), extending servlet capabilities to enable servers to deliver to the client dynamically created XHTML pages with embedded Java functionality.

3 3 Introduction n Servlets (ctd) u A distributed application often has a 3-tier architecture with a web server providing secure access to a “back end”, commonly a database system. u Servlets may be used to support thin clients with minimal client-side software. u The client sends one of a small number of requests to the server; servlets residing in the server respond u Clients connect to the server using standard protocols t HTTP forms t “get”, “post” requests  See http://www.w3.org/Addressing for information on URLs, and  http://www.w3.org/Protocols/HTTP l This chapter shows how servlets manage client/server communication using the HTTP protocol n Client sends an HTTP request n Servlet processes it and returns a response to client Web Server Back End Client eg database business logic

4 4 Servlet Architecture l A servlet is a class which implements the javax.servlet.Servlet interface void init(ServletConfig cfg) u parameter supplied by server containing the servletclass ServletConfig get ServletConfig() u returns reference to implementing object String get ServletInfo() n void service(ServletRequest rq, ServletResponse rp) u Containing server calls this in response to a client request to the servlet. n void destroy() l The packages provide two abstract classes implementing this n javax.servlet.GenericServlet n javax.servlet.http.HttpServlet n Most servlets extend one of these. We shall concentrate on the HttpServlet from now on. Class HttpServlet n Has empty methods, at leat one of which should be over-ridden:  doGet(HttpServletRequest rq, HttpServletResponse rp ) and  doPost( HttpServletRequest rq, HttpServletResponse rp ) n Overrides service() with a version which calls one of these, depending on request type; An HttpServlet needs to define specific overriding doGet() and/or doPost() methods

5 5 Servlet Architecture l Interface HttpServletRequest String getParameter(String param)  cf applet parameter Enumeration getParameterNames() u names of all parameters set as part of a post request String[] getParameterValues(String paramName) u a parameter can have multiple values Cookie[] getCookies() u cookies are stored on the client by the server (see later) HttpSession getSession (boolean create) u returns an HttpSession associated with current browsing session (see later) u if create = true, creates one if one does not already exist l Interface HttpServletResponse void addCookie(Cookie cookie) u added to header of response to client. Stored in client if client has enabled cookies. ServletOutputStream getOutputStream() n PrintWriter getWriter() u byte-based (binary) and character-based output to client, respecively u cookies are stored on the client by the server (see later) void setContentType(String type)  MIME type of output to client -- “text/html” for HTML content l Every call to doGet(...), doPost(...) gets from containing server objects implementing these.

6 6 Handling get Requests WelcomeServlet Example n WelcomeServlet.java + WelcomeServlet.html (See listings at back) n Note the tag in the XHTML form in WelcomeServlet.html: u directs a request from this form to the server, to the WelcomeServlet; u Establishes it as a get request n The tag displays a button labelled “Get HTML Document” u When the client loads this XHTML and clicks the button the get request is sent to the server n The WelcomeServlet, once correctly deployed in the server, responds by  Setting the response content type to “text/html”  Getting the output PrintWriter object for the response -- called out  Using out.println(...) to output lines of HTML to the client. n Practicalities  javax.servlet and javax.servlet.http are J2EE packages: J2EE needs to be installed in the development environment and an appropriate CLASSPATH set.

7 7 Handling get Requests l Deploying the WelcomeServlet in Tomcat Server n to serve the get request from this XHTML form Look at Tomcat’s webapps directory In here make a directory servletexs u the context root of this Web application n In this directory install the following subdirectories and files (files in italics): servletexs WelcomeServlet.html WEB-INF web.xml classes WelcomeServlet.class web.xml is the deployment descriptor also listed at the back of these slides n Ties the servlet to incoming get requests from client forms: see Servlet Mappings section n Client runs the servlet by first loading the form generating the get request: http://localhost:8080/servletexs/WelcomeServlet.html

8 8 Handling get Requests Containing Data l WelcomeServlet2 n WelcomeServlet2.java + WelcomeServlet2.html ( See listings at back) n Note the tag in a XHTML form in WelcomeServlet2.html as before: n The tags within the form display a text box and a “Submit” button. u When the client clicks it the get request is sent to the server u with parameter named “firstname” with value = contents of text box WelcomeServlet2 can use request.getParameter( “firstname”) to obtain the data entered by the client in the form. l Deploying: n Install WelcomeServlet2.java, WelcomeServlet2.html in Tomcat in advjhtp1/... n Edit web.xml to include and elements for this servlet n Restart Tomcat l Client http://localhost:8080/servletexs/WelcomeServlet2.html

9 9 Handling post Requests l WelcomeServlet3 n WelcomeServlet3.java + WelcomeServlet3.html ( See listings at back) n Note the tag in a XHTML form in WelcomeServlet3.html: n The tags within the form display a text box and a “Submit” button. u When the client clicks it the post request is sent to the server u with parameter named “firstname” with value = contents of text box WelcomeServlet3 uses request.getParameter( “firstname”) to obtain the data entered by the client in the form. l Deploying: n Install WelcomeServlet3.java, WelcomeServlet3.html in Tomcat in advjhtp1/... n Edit web.xml to include and elements for this servlet n Restart Tomcat l Client http://localhost:8080/servletexs/WelcomeServlet3.html

10 10 Multi-Tier Applications -- Using JDBC from a Servlet l SurveyServlet.java, Survey.html n Functionality u Collects a vote from each client “What is your favourite pet?” u Stores votes in database u Returns to client a report of survey statistics n Thin client: just a web browser n Middle tier: the SurveyServlet n Back end: a Cloudscape database connected to middle tier by JDBC l ServeyServlet n init(...) u Initially sets up a JDBC Connection to the database, and SQL PreparedStatement objects to update the database and extract statistics from it Web Server Back End Client db access middle tier JDBC XHTML

11 11 Multi-Tier Applications -- Using JDBC from a Servlet l ServeyServlet n doPost(...) u Gets vote from client  request.getParameter(“animal”) u Uses a Statement to update the database u Uses Statement objects to get get statistics u Sends XHTML response to client l Survey.html n Form with post method, containing n Radio buttons with name = “animal”for the client to vote n A Submit button l Deploy in the usual way Install class, html file, and add animalsurvey elements to web.xml; restart Tomcat. u servlet-name = animalsurvey, url-pattern = /animalsurvey, etc Also copy ojdbc14.jar (containing the Oracle JDBC driver) to Tomcat’s...\servletexs\WEB-INF\lib subdirectory.

12 12 Session Tracking -- Cookies l A way for a server to keep persistent information about a particular client n preferences n contents of an e-shopping cart n etc l Text based data l If cookies enabled in client’s browser, n Server can send cookies in header of an XHTML page n Client browser stores them in client machine n Sent back to server with next get or post from client n Cookies expire after a finite amount of time l See Liang n CookieServlet.java, CookieSelectLanguage.html

13 13 Session Tracking -- HttpSession Interface l Another way of supporting a server to maintain information about client preferences n See, eg Liang n SessionServlet.java, SessionSelectLanguage.html n The Request object provides an object implementing HttpSession n Same functionality as Cookie example l Unlike Cookies, HttpSession objects are not normally saved between browser sessions but just “live” for the duraiton of a session.

14 14 Further Reading l WWW Resources n The Sun java enterprise edition web site has plenty on servlets. Go to http://java.sun.com/ ffollow the links to API documentation on the Classes and interfaces in the javax.servlet and javax.servlet.http packages, and tutorial and background reading on servlets. http://java.sun.com/ Explore http://jakata.apache.org/tomcat/ For reading about servlets deployed in Tomcat http://jakata.apache.org/tomcat/ l The Liang textbook references cited above. l The O’Reilly book Java Servlet Programming


Download ppt "Michael Brockway Application Integration Servlets l Introduction & Overview l HTTP Servlets l HTTP get Requests l HTTP post Requests l Multi-tier Applications."

Similar presentations


Ads by Google