core servlets chapter 6 generating server response: http status codes

14
Core servlets chapter 6 Generating server response: http status codes

Post on 19-Dec-2015

222 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Core servlets chapter 6 Generating server response: http status codes

Core servlets chapter 6

Generating server response: http status codes

Page 2: Core servlets chapter 6 Generating server response: http status codes

A request

GET /…/someName HTTP/1.1

Host:….

Header2:…

HeaderN:…

<blank line>

Page 3: Core servlets chapter 6 Generating server response: http status codes

A response

HTTP/1.1 200 OKContent-type: text/htmlHeader2:……HeaderN:…(blank line)<!DOCTYPE…><HTML>…

Page 4: Core servlets chapter 6 Generating server response: http status codes

(some) HTTP 1.1 status codes

• 100-199 – information to the client to take some further action

• 200-299 – request was successful• 300-399 – files have moved LOCATION

header may provide new address• 400-499 – client error• 500-599 – server error• In servlets, you send the code using

response.SC_SOME_MESSCODE

Page 5: Core servlets chapter 6 Generating server response: http status codes

Status codes and responses• In response to 100 (Expect request header with value Continue)

the server could send 100 SC_Continue – client should go ahead or 417 SC_EXPECTATION_FAILED – it won’t accept the document

• 200 OK – the default for servlets.• 202 Accepted, send SC_ACCEPTED to indicate request is still

being processed.• 204 No Content – send SC_NO_CONTENT to indicate browser

should continue to display previous document. Useful if the client is reloading but you can tell that the doc is up to date.

• 205 reset is used to clear form fields. Send SC_RESET_CONTENT, new in HTTP 1.1

• 301 Moved Permanently and 302 Found: SC_MOVED_PERMANENTLY typically sends a Location header with the new URL. 302 may indicate a temporary move. Servlet uses SC_MOVED_TEMPORARILY

Page 6: Core servlets chapter 6 Generating server response: http status codes

More codes

• 303 (See Other)

Page 7: Core servlets chapter 6 Generating server response: http status codes

Redirect vs refresh

• In 302 the browser automatically loads the URL from the location header.

• Track user behavior by routing them to a link on your own site and then recording what they select

• Side effects – you can return both a Set-Cookie response header via response.addCookie and a 302 status code by means of a response.sendRedirect(url). This latter is shorter and easier than using both response.setStatus(response.SC_MOVED_TEMPORARILY) and respnse.setHeader(“Location”,url)

Page 8: Core servlets chapter 6 Generating server response: http status codes

Wrongdestination servlet sends you to…

Page 9: Core servlets chapter 6 Generating server response: http status codes

The servlet that presents a form

Page 10: Core servlets chapter 6 Generating server response: http status codes

After submitting query

Page 11: Core servlets chapter 6 Generating server response: http status codes

I modified this to count hits to various engines using a file (see Hunter chapt 3

notes)• Yahoo 3

• MSN 1

• Google 1

• AllTheWeb 1

• AltaVista 0

• HotBot 0

• Lycos 1

Page 12: Core servlets chapter 6 Generating server response: http status codes

First create a file – this servlet could be run on startup… or file could be built manually

import java.io.*;import javax.servlet.*;import javax.servlet.http.*;public class WriteToFile extends GenericServlet { public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {res.setContentType("text/html"); PrintWriter out = res.getWriter(); out.println("starting to write"); FileWriter fileWriter = null; PrintWriter printWriter = null; try { fileWriter = new FileWriter("EngineCounter.initial"); printWriter = new PrintWriter(fileWriter); for(int j=0;j<7;j++) printWriter.println("0"); return; } catch (IOException e) { // problem during write // Log the exception. See Chapter 5. out.println("exception"+e.toString()); } finally { // Make sure to close the file if (printWriter != null) { printWriter.close();} out.println("writing complete..");//and don’t forget to close the file//fout.close();}} public String getServletInfo() { return "A servlet that writes to a file in work/ dir"; }}

Page 13: Core servlets chapter 6 Generating server response: http status codes

logic

• Next, count “hits” to engine by searching a string array for the name of the engine selected.

• Read file contents, increment one value• Re-write file contents• My array:String

names[]={"Yahoo","MSN","Google","AllTheWeb","AltaVista","HotBot", "Lycos"};

• I didn’t write it, but another servlet could be run at the end of the day to view engine-hit distribution

Page 14: Core servlets chapter 6 Generating server response: http status codes

My update file function in the redirect servlet

public void update_file(String name)throws IOException{ int pos=-1; for(int k=0;k<names.length;k++)if(name.equals(names[k])){pos=k; System.out.println("name is "+name+" pos is"+pos);}int ct[]=new int[names.length];int i=0;

FileInputStream fin=new FileInputStream("C:/apache-tomcat-6.0.10/apache-tomcat-6.0.10/bin/enginecounter.initial");

Scanner s=new Scanner(fin); while(s.hasNext()){ ct[i]=s.nextInt(); i++; }

fin.close();if(pos!=-1)ct[pos]++;FileWriter fileWriter = new FileWriter("EngineCounter.initial"); PrintWriter printWriter = new PrintWriter(fileWriter); for(int j=0;j<7;j++) printWriter.println(ct[j]); if (printWriter != null) { printWriter.close();

}//if}//update