220 likes | 338 Views
Forms - Java Server Pages. request is an implicit object that we use for retrieving form information request is an instance of class HttpServletRequest Information can be found at http://java.sun.com/webservices/docs/1.5/api/javax/servlet/http/HttpServletRequest.html.
E N D
Forms - Java Server Pages • request is an implicit object that we use for retrieving form information • request is an instance of class HttpServletRequest • Information can be found at http://java.sun.com/webservices/docs/1.5/api/javax/servlet/http/HttpServletRequest.html
Forms - Java Server Pages • The HttpServletRequest interface has some important methods • getMethod( ) Returns the name of the HTTP method with which this request was made, for example, GET, or POST. • Returns a String
Forms - Java Server Pages • To retrieve the method used: • The method is: • <%= request.getMethod( ) %>
HttpServletRequest methods • getQueryString( ) Returns the query string that is contained in the request URL after the path. • Returns a String
Forms - Java Server Pages • To retrieve the query string: • The query string is: • <%= request.getQueryString( ) %>
HttpServletRequest methods • getRemoteUser() Returns the login of the user making this request, if the user has been authenticated, or null if the user has not been authenticated. • Return a String
Forms - Java Server Pages • To retrieve the remote user: • The remote user is: • <%= request.getRemoteUser( ) %>
HttpServletRequest methods • getCookies() Returns an array containing all of the Cookie objects the client sent with this request. • Returns an array of Cookie objects • Cookie is a class
HttpServletRequest methods • getSession() Returns the current session associated with this request, or if the request does not have a session, creates one. • Returns an HttpSession object • HttpSession is a class
HttpServletRequest methods • HttpServletRequest extends the interface ServletRequest and inherits its methods • getRemoteAddr() Returns the Internet Protocol (IP) address of the client or last proxy that sent the request. Returns a String • getRemoteHost() Returns the fully qualified name of the client or the last proxy that sent the request. Returns a String
Forms - Java Server Pages • To retrieve the remote address and the remote host: • The remote address is: • <%= request.getRemoteAddr( ) %> • The remote host is: • <%= request.getRemoteHost( ) %>
HttpServletRequest methods • isSecure() Returns a boolean indicating whether this request was made using a secure channel, such as HTTPS. Returns a boolean. • getLocalPort() Returns the Internet Protocol (IP) port number of the interface on which the request was received. Returns an int.
Forms - Java Server Pages • To retrieve if the connection is secure and the local port: • Is the connection secure? • <%= request.isSecure( ) %> • The local port is: • <%= request.getLocalPort( ) %>
HttpServletRequest methods • getContentLength() Returns the length, in bytes, of the request body and made available by the input stream, or -1 if the length is not known. Returns an int.
Forms - Java Server Pages • To retrieve the number of bytes from user input • The number of bytes to read is: • <%= request.getContentLength( ) %>
HttpServletRequest methods • getContentLength() Returns the length, in bytes, of the request body and made available by the input stream, or -1 if the length is not known. Returns an int. • getInputStream() Retrieves the body of the request as binary data using a ServletInputStream. Returns a ServletInputStream.
HttpServletRequest methods • To retrieve user input • getParameter(String name) Returns the value of the named attribute as an Object, or null if no attribute of the given name exists. Returns an Object. • getAttributeNames() Returns an Enumeration containing the names of the attributes available to this request. Returns an Enumeration.
HttpServletRequest methods • getAttributeNames() Returns an Enumeration containing the names of the attributes available to this request. Returns an Enumeration. • Also getParameterNames( ), also return an Enumeration
Forms - Java Server Pages • To retrieve the data entered by the user for the object named “id” • The data entered is: • <%= request.getParameter( “id” ) %>
Forms - Java Server Pages • To retrieve all the parameters (names of the objects) of the form • <% • Enumeration enu = • request.getParameterNames( ); • // Process enu here (use a loop) • %>
Forms - Java Server Pages • // enu is the Enumeration from before • String attName = “”; • String data = “”; • while( enu.hasMoreElements( ) ) • { • attName = (String) enu.nextElement( ); • out.print( attName + ": " ); • data = request.getParameter( attName ); • out.println( data ); • }
Forms - Java Server Pages • For checkboxes, you can use the getParameterValues method • It returns an array of Strings • You can then process that array with a for loop • Be careful, the array could be null if no checkbox has been selected by the user