CS3220 Web and Internet Programming Generating HTTP Responses Chengyu Sun California State University, Los Angeles
HTTP Response As Output Browser Application Server HTTP request HTTP response input output program
HTTP Response Example HTTP/1.1 200 OK Content-Type: text/html;charset=ISO-8859-1 Content-Length: 168 Date: Sun, 03 Oct 2004 18:26:57 GMT Server: Apache-Coyote/1.1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html><head><title>Servlet Life Cycle</title></head> <body> n is 299 and m is 440 </body> </html>
HTTP Response Status line Protocol Status code Header [Message body]
Common Status Codes 200 (OK) 401 (Unauthorized) 403 (Forbidden) 404 (Not Found) 500 (Internal Server Error)
Status Codes 100 – 199: Informational. Client should respond with further action 200 – 299: Request is successful 300 – 399: Files have moved 400 – 499: Error by the client 500 – 599: Error by the server
Redirect Codes 301 (Moved Permanently) 302 (Found) 303 (See Other) GET 302 (Found) a.k.a. (Moved Temporarily) 303 (See Other) GET and POST 307 (Temporary Redirect) GET but not POST
<error-page> in web.xml Showing customized error pages for common errors, e.g. 404 Page Not Found Example: <error-page> <error-code>404</error-code> <location>/WEB-INF/404.html</location> </error-page> <error-code>403</error-code> <location>/WEB-INF/403.html</location>
Header Fields Request Response Accept Accept-Charset Accept-Encoding Accept-Language Connection Content-Length Cookies Response Content-Type Content-Encoding Content-Language Connection Content-Length Set-Cookie
More Response Header Fields Location for redirect Refresh “Push” Incremental display Content-Disposition (RFC2183) RFC2616, Section 14 http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
HttpServletResponse http://download.oracle.com/javaee/7/api/javax/servlet/http/HttpServletResponse.html
Header Methods in HttpServletResponse addHeader(), setHeader() addIntHeader(), setIntHeader() addDateHeader(), setDateHeader() containsHeader()
More HttpServletResponse Methods setContentType( String type ) sendRedirect( String location ) Set status code to 302 getWriter() For text responses, e.g. HTML pages getOutputStream() For binary responses, e.g. images
Example: Add Revisited Redirect the user back to the input form if the parameters are missing or invalid sendRedirect() does not terminate the execution of doGet() or doPost()
Example: Countdown Automatically refreshes the display every second The Refresh header
File Upload and Download in Web Applications Pictures on online albums and social networking sites Attachments in web forum posts and web emails Resumes on job search sites Student homework submissions in learning management systems … …
File Upload – The Form <form action="FileUploadHandler" method="post" enctype="multipart/form-data"> First file: <input type="file" name="file1" /> <br /> Second file: <input type="file" name="file2" /> <br /> <input type="submit" name="upload" value="Upload" /> </form>
File Upload – The Request POST / HTTP/1.1 Host: cs.calstatela.edu:4040 […] Cookie: SITESERVER=ID=289f7e73912343a2d7d1e6e44f931195 Content-Type: multipart/form-data; boundary=---------------------------146043902153 Content-Length: 509 -----------------------------146043902153 Content-Disposition: form-data; name="file1"; filename="test.txt" Content-Type: text/plain this is a test file. Content-Disposition: form-data; name="file2"; filename="test2.txt.gz" Content-Type: application/x-gzip ?????????UC
Apache commons-fileupload http://commons.apache.org/proper/commons-fileupload/index.html Parse an HTTP request and retrieve the uploaded files Requires Apache commons-io
Paths $PROJECT/WEB-INF/lib for additional jar files in a webapp $WORKSPACE/.metadata/.plugins/org.eclipse.wst.server.core is where webapp is deployed locally getServletContext().getRealPath(“/path_within_webapp") to get the absolute file system path to the file or folder
Important Classes FileItem in commons-fileupload - https://commons.apache.org/proper/commons-fileupload/apidocs/org/apache/commons/fileupload/FileItem.html File in java.io - https://docs.oracle.com/javase/7/docs/api/java/io/File.html
Example: Download Download an image file from the server File I/O Content type Binary response Content-Length and Content-Disposition header
Summary HTTP response format Status code ranges and some common status codes Generate non-HTML responses Use of headers Writer vs. OutputStream