📄 ch12.htm
字号:
case 0: return"Jan";<BR> case 1: return"Feb";<BR> case 2: return"Mar";<BR> case 3: return"Apr";<BR> case 4: return"May";<BR> case 5: return"Jun";<BR> case 6: return"Jul";<BR> case 7: return"Aug";<BR> case 8: return"Sep";<BR> case 9: return"Oct";<BR> case 10: return"Nov";<BR> case 11: return"Dec";<BR> }<BR> return "-";<BR> }<BR>}</TT></BLOCKQUOTE><P>Integers will print in their current precision. Common log formatcalls for two-digit numbers. The method <TT>formatFor2()</TT>will add a zero at the beginning if the passed integer is lessthan 10.<P>Notice the polarity switch of <TT>getTimezoneOffset()</TT>.Common log format as well as most other time-zone representationsuse hours away from GMT (locale - GMT).<BR><P><CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%><TR VALIGN=TOP><TD><B>Note</B></TD></TR><TR VALIGN=TOP><TD><BLOCKQUOTE>The <TT>getTimezoneOffset()</TT> method returns the number of minutes GMT is away from your locale (GMT - locale). Normal convention is to use the number of minutes your locale is away from GMT (locale - GMT). Be careful when displaying time-zone information.</BLOCKQUOTE></TD></TR></TABLE></CENTER><P><P>Now the HTTPrequest contains the request and the initial partsof the log string. The remaining two pieces of log information,<TT>status</TT> and <TT>length</TT>,are filled in by the send routines.<H3><A NAME="AlteringtheSendRoutines">Altering the Send Routines</A></H3><P>A nice feature of many servers is that they send a standard HTMLfile for error states. For instance, file error404.html is returnedwhen a request is not found, which enables you to configure whatis sent for various error conditions. Previously, the status ofa transmission was always <TT>200 OK</TT>.Now, negative responses also need to use the services of <TT>sendFile()</TT>.Status and the description string need to be passed in by thecaller. This has another advantage because log information iswritten in the <TT>sendFile()</TT>routine, which means that negative responses are also recorded.<BLOCKQUOTE><TT> /**<BR> * Send a negative (404 NOT FOUND) response<BR> * @param request the HTTP request torespond to.<BR> */<BR> private void sendNegativeResponse(HTTPrequestrequest)<BR> {<BR> try<BR> {<BR> StringfileToGet = "error404.html";<BR> FileInputStreaminFile = new FileInputStream(fileToGet);<BR> if(debug & level < 4)<BR> {<BR> System.out.print("DEBUG:Sending -rsp ");<BR> System.out.print(fileToGet+ " " + inFile.available());<BR> System.out.println("Bytes");<BR> }<BR> sendFile(request,inFile, 404, "Not Found", fileToGet);<BR> inFile.close();<BR> }<BR> catch (FileNotFoundExceptionfnf)<BR> {<BR> System.out.println("Error:No error404.html file for -rsp");<BR> }<BR> catch (ProtocolExceptionpe)<BR> {<BR> }<BR> catch (IOExceptionioe)<BR> {<BR> System.out.println("Unknownfile length: " + ioe);<BR> }<BR> }<BR><BR> /**<BR> * Send the passed file<BR> * @param request the HTTP request instance<BR> * @param inFile the opened input filestream to send<BR> */<BR> private void sendFile(HTTPrequest request,<BR> FileInputStreaminFile,<BR> intstatus, String describe,<BR> StringfileToGet)<BR> {<BR> DataOutputStreamoutbound = null;<BR> String type =null;<BR><BR> try<BR> {<BR> //Acquire an output stream<BR> outbound= new DataOutputStream(<BR> request.clientSocket.getOutputStream());<BR><BR> //Send the response header<BR> intperiod = fileToGet.lastIndexOf('.');<BR> if( period != -1 && period != fileToGet.length() )<BR> type= (String)suffix.get(fileToGet.substring(period + 1));<BR> if(type == null)<BR> type= "text/plain";<BR><BR> if(debug & level < 4)<BR> {<BR> System.out.println("DEBUG:Type " + type);<BR> }<BR> outbound.writeBytes("HTTP/1.0" + status + " " + describe + "\r\n");<BR> outbound.writeBytes("Content-type:" + type + "\r\n");<BR> outbound.writeBytes("Content-Length:" + inFile.available() + "\r\n");<BR> outbound.writeBytes("\r\n");<BR><BR> request.log.append(status+ " " + inFile.available());<BR> System.out.println(request.log.toString());<BR> if( logging )<BR> logFile.print(request.log.toString()+ "\r\n");<BR><BR> //Added to allow Netscape to process header properly<BR> //This is needed because the close is not recognized<BR> sleep(5000);<BR><BR> //If not a HEAD request, send the file body.<BR> //HEAD requests only solicit a header response.<BR> if(!request.method.equals("HEAD"))<BR> {<BR> bytedataBody[] = new byte[1024];<BR> intcnt;<BR> while((cnt = inFile.read(dataBody)) != -1)<BR> outbound.write(dataBody,0, cnt);<BR> }<BR><BR> //Cleanup<BR> outbound.flush();<BR> outbound.close();<BR> request.inbound.close();<BR> }<BR> catch (IOExceptionioe)<BR> {<BR> System.out.println("IOExceptionwhile sending file: " + ioe);<BR> }<BR> }</TT></BLOCKQUOTE><P>Notice how the MIME content type is derived from the file's extension.Using a hash table for this information allows a rapid lookup.If the extension is not found, or if the file has no extension,the file is sent as type <TT>text/plain</TT>.<H2><A NAME="CreatingNewContentTypes"><FONT SIZE=5 COLOR=#FF0000>CreatingNew Content Types</FONT></A></H2><P>Now that the server can send various MIME content types, it'stime to investigate how to use HotJava content handlers. Thisexercise is small in scope, but large in know-how.<P>HotJava comes equipped with four content handlers, in additionto its standard HTML handler:<UL><LI><TT>text/plain</TT><LI>image/gif<LI>image/x_xbitmap<LI><TT>image/x_xpixmap</TT></UL><P>This exercise adds two new types, <TT>text/fee</TT>and <TT>text/foo</TT>. Since thesetypes aren't within the local system, HotJava will try to loadthem from the server:<BLOCKQUOTE><TT>GET /classes/net/www/content/text/fee.classHTTP/1.0<BR>GET /classes/net/www/content/text/foo.class HTTP/1.0</TT></BLOCKQUOTE><H3><A NAME="WritingContentHandlers">Writing Content Handlers</A></H3><P>A HotJava content handler consists of a single class that extendsthe ContentHandler class. Its class name must be the same as theMIME sub-type that it handles. In addition, it must have the samepath it would have if it were local to HotJava.<P>The code below implements the <TT>text/fee</TT>content handler:<BLOCKQUOTE><TT>package net.www.content.text;<BR><BR>import net.www.html.ContentHandler;<BR>import net.www.html.URL;<BR>import java.io.InputStream;<BR><BR>/**<BR> * A content handler for text/fee objects<BR> */<BR>public class fee extends ContentHandler<BR>{<BR> /**<BR> * Read in an ASCII text file and appendan<BR> * ID string to the end.<BR> * @param is holds the input stream forthe content<BR> * @param u holds the URL for the object<BR> */<BR> public Object getContent(InputStream is,URL u)<BR> {<BR> StringBuffer sb= new StringBuffer();<BR> int c;<BR><BR> while ((c = is.read())>= 0)<BR> {<BR> s
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -