core servlets chapter 6 generating server response: http status codes
Post on 19-Dec-2015
222 views
TRANSCRIPT
Core servlets chapter 6
Generating server response: http status codes
A request
GET /…/someName HTTP/1.1
Host:….
Header2:…
…
HeaderN:…
<blank line>
A response
HTTP/1.1 200 OKContent-type: text/htmlHeader2:……HeaderN:…(blank line)<!DOCTYPE…><HTML>…
(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
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
More codes
• 303 (See Other)
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)
Wrongdestination servlet sends you to…
The servlet that presents a form
After submitting query
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
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"; }}
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
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