CS6320 Servlet Request and Response L Grewe 1 Recall Servlet Invocation Servlet operates on Request Response Web Browser HTTP response MyServlet service request response HTTP request 2 Client Request Data When a user submits a browser request to a web server it sends two categories of data HTTP Request Header Data Data that is automatically appended to the HTTP Request from the client For example cookies browser type etc Data Data sent by client Example the user explicitly typed into an HTML form i e login and password 3 An HTTP Header Request Example GET default asp HTTP 1 0 Accept image gif image x xbitmap image jpeg image png Accept Language en Connection Keep Alive Host magni grainger uiuc edu User Agent Mozilla 4 04 en WinNT I Nav Cookie SITESERVER ID 8dac8e0455f4890da220ada8b76f ASPSESSIONIDGGQGGGAF JLKHAEICGAHEPPMJKMLDEM Accept Charset iso 8859 1 utf 8 See also class website module for further examples information 4 Reading HTTP Request Headers 5 Sample HTTP Request Example of a HTTP Request to Yahoo com GET HTTP 1 1 Accept Accept Language en us Accept Encoding gzip deflate User Agent Mozilla 4 0 compatible MSIE 5 0 Windows NT DigExt Host www yahoo com Connection Keep Alive Tip Check out Cookie B 2td79o0sjlf5r b 2 http www web sniffer net 6 Accessing HTTP Headers To access any of these Headers the use the HTTPServletRequest getHeader method For example String connection req getHeader Connection To retrieve a list of all the Header Names use the getHeaderNames method getHeaderNames returns an Enumeration object For example Enumeration enum req getHeaderNames 7 Additional HTTP Information getMethod Indicates the request method e g GET or POST getRequestURI Returns the part of the URL that comes after the host and port For example for the URL http randomhost com servlet search the request URI would be servlet search getProtocol Returns the protocol version e g HTTP 1 0 or HTTP 1 1 Methods for specific request information getCookies getContentLength getContentType getMethod getProtocol etc See api and class website for more 8 Example 1 Our first example echoes all of the HTTP Request Information First it outputs Method RequestURI Protocol Version Then it calls getHeaderNames to retrieve a list of all HTTP Header Names For each header name it then calls getHeader 9 package coreservlets import import import import java io javax servlet javax servlet http java util public class ShowRequestHeaders extends HttpServlet public void doGet HttpServletRequest request HttpServletResponse response throws ServletException IOException response setContentType text html PrintWriter out response getWriter String title Servlet Example Showing Request Headers out println ServletUtilities headWithTitle title BODY BGCOLOR FDF5E6 n H1 ALIGN CENTER title H1 n B Request Method B request getMethod BR n B Request URI B request getRequestURI BR n B Request Protocol B request getProtocol BR BR n TABLE BORDER 1 ALIGN CENTER n TR BGCOLOR FFAD00 n TH Header Name TH Header Value Continued 10 Enumeration headerNames request getHeaderNames while headerNames hasMoreElements String headerName String headerNames nextElement out println TR TD headerName out println TD request getHeader headerName out println TABLE n BODY HTML Let the same servlet handle both GET and POST public void doPost HttpServletRequest request HttpServletResponse response throws ServletException IOException doGet request response 11 Reading Browser Types The User Agent HTTP header indicates the browser and operating system For example user agent Mozilla 4 0 compatible MSIE 6 0 Windows NT 5 1 You can use this header to differentiate browser types or simply log browser requests 12 Example User Agents Internet Explorer user agent Mozilla 4 0 compatible MSIE 6 0 Windows NT 5 1 Mozilla Mozilla 5 0 Windows U Windows NT 5 1 enUS rv 1 4 Gecko 20030624 For strange historical reasons IE identifies itself as Mozilla To differentiate between the two use MSIE not Mozilla 13 CGI Variables In addition to HTTP Request headers you can also determine additional information about both the client and the server IP Address of Client Host Name of Client Server Name Server Port Server Protocol Server Software 14 Getting Request Data 15 Data from HTML form Using HTML forms we can pass parameters to Web applications form action method form comprises a single form action the address of the application to which the form data is sent method the HTTP method to use when passing parameters to the application e g get or post 16 The input Tag Inside a form INPUT tags define fields for data entry Standard input types include buttons checkboxes password fields radio buttons text fields image buttons text areas hidden fields etc They all associate a single string value with a named parameter 17 GET Example form method get action http www google com search p input name q type text input type submit input type reset p form http www google com search q servlets 18 Getting the Parameter Values To get the value of a parameter named x req getParameter x where req is the service request argument If there can be multiple values for the parameter req getParameterValues x To get parameter names req getParameterNames 19 Example HTML Form that asks users for colors etc Uses these colors to control the response s background and foreground colors 20 html head title Sending Parameters title style type text css p display table row span display table cell padding 0 2em style head body h1 Please enter the parameters h1 form action SetColors method get p Background color span input type text name bgcolor span p p Font color span input type text name fgcolor span p p Font size span input type text name size span p h2 input type submit value Submit Parameters h2 form 21 parameters html An Example cont public class SetColors extends HttpServlet public void doGet HttpServletRequest request HttpServletResponse response throws ServletException IOException response setContentType text html PrintWriter out response getWriter String bg request getParameter bgcolor String fg request getParameter fgcolor String size request getParameter size SetColors java 22 An Example cont out println html head title Set Colors Example title head out println body style color fg background color bg font size size px out println h1 Set Colors Example h1 out println p You requested a background color bg p out println p You requested a font color fg p out println p You requested a font size size p out println body html
View Full Document
Unlocking...