Java EE WEB¹¤³ÌʦÅàѵ-JDBC+Servlet+JSPÕûºÏ¿ª·¢Ö®14.ServletÇëÇóÍ·ÐÅÏ¢
2009-06-21 21:37:09
°æÈ¨ÉùÃ÷£ºÔ´´×÷Æ·£¬Ð»¾ø×ªÔØ£¡·ñÔò½«×·¾¿·¨ÂÉÔðÈΡ£ |
¨CµäÐ͵ÄÇëÇóÍ·ÐÅÏ¢
¨C¶ÁÈ¡HTTPÇëÇóÍ· ¨CʹÓñí¸ñÏÔʾËùÓÐÇëÇóÍ·ÐÅÏ¢ ¨CÀí½â¸÷ÖÖÇëÇóÍ·µÄº¬Òå ¨CÇø·Ö²»Í¬µÄä¯ÀÀÆ÷ÀàÐÍ ##############Michael·Ö¸îÏß###################
• µäÐ͵ÄÇëÇóÍ·ÐÅÏ¢
• ¶ÁÈ¡HTTPÇëÇóÍ·
¨CʹÓÃHttpServletRequestÖеķ½·¨ • Ò»°ã·½·¨ ¨CgetHeader (headerÃû³Æ²»Çø·Ö´óСд) ¨CgetHeaders ¨CgetHeaderNames • רÃÅ·½·¨ ¨CgetCookies ¨CgetAuthType ¨CgetRemoteUser ¨CgetContentLength ¨CgetContentType ¨CgetDateHeader ¨CgetIntHeader • Ïà¹ØÐÅÏ¢ ¨CgetMethod ¨CgetRequestURI ¨CgetQueryString ¨CgetProtocol • ʹÓñí¸ñÏÔʾÓÐÇëÇóÍ·ÐÅÏ¢
login.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>login.html</title> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="this is my page"> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <!--<link rel="stylesheet" type="text/css" href="./styles.css">--> </head> <body> <form name="f1" id="f1" action="/Servlet_RequestHeader/servlet/RequestHeaderServlet" method="post"> <table border="0"> <tr> <td>Login:</td> <td><input type="text" name="login" id="login"></td> </tr> <tr> <td>Password:</td> <td><input type="password" name="password" id="password"></td> </tr> <tr> <td colspan="2" align="center"><input type="submit" value="login"></td> </tr> </table> </form> </body> </html> RequestHeaderServlet.java
package com.michael.servlet; import java.io.IOException; import java.io.PrintWriter; import java.util.Enumeration; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class RequestHeaderServlet extends HttpServlet { /** * Constructor of the object. */ public RequestHeaderServlet() { super(); } /** * Destruction of the servlet. <br> */ public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } /** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); } /** * The doPost method of the servlet. <br> * * This method is called when a form has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Enumeration names = request.getHeaderNames(); response.setContentType("text/html"); PrintWriter out = response.getWriter(); out .println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">"); out.println("<HTML>"); out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>"); out.println(" <BODY>"); out.println("<table>"); out.println("<tr>"); out.println("<th>"); out.println("RequestHeader Name"); out.println("</th>"); out.println("<th>"); out.println("RequestHeader Value"); out.println("</th>"); out.println("</tr>"); while(names.hasMoreElements()){ String name = (String) names.nextElement(); String value = request.getHeader(name); out.println("<tr>"); out.println("<td>"); out.println(name); out.println("</td>"); out.println("<td>"); out.println(value); out.println("</td>"); out.println("</tr>"); } out.println("</table>"); out.println(" </BODY>"); out.println("</HTML>"); out.flush(); out.close(); } /** * Initialization of the servlet. <br> * * @throws ServletException if an error occure */ public void init() throws ServletException { // Put your code here } } ¿´ÏÂЧ¹û
• Àí½â¸÷ÖÖÇëÇóÍ·µÄº¬Òå
¨C Accept • ±êʶä¯ÀÀÆ÷ÄÜ´¦ÀíMIMEÀàÐÍ • ÄÜ·¢ËͲ»Í¬µÄÄÚÈݵ½²»Í¬µÄ¿Í»§¶Ë. ÀýÈ磬PNGÎļþÓкõÄѹËõÌØÐÔ£¬µ«ÊÇÔÚä¯ÀÀÆ÷ÖÐÖ§³ÖµÄ²»ÊǺܹ㷺¡£ • Ò»¸öServlet¿ÉÒÔ¼ì²éÊÇ·ñÖ§³ÖPNGÎļþ¸ñʽ£¬Èç¹ûÖ§³Ö ¨C <IMG SRC=¡°picture.png¡± ...> ·ñÔò ¨C <IMG SRC="picture.gif" ...> ¨C Accept-Encoding • ±êʶä¯ÀÀÆ÷ÄÜ´¦ÀíµÄ±àÂëÀàÐÍ ¨C Authorization • ÊÚȨÐÅÏ¢£¬Í¨³£³öÏÖÔÚ¶Ô·þÎñÆ÷·¢Ë͵ÄWWW-AuthenticateÍ·µÄÓ¦´ðÖÐ ¨C Connection • ±íʾÊÇ·ñÐèÒª³Ö¾ÃÁ¬½Ó¡£Èç¹ûServlet¿´µ½ÕâÀïµÄֵΪ¡°Keep-Alive¡±£¬»òÕß¿´µ½ÇëÇóʹÓõÄÊÇHTTP 1.1£¨HTTP 1.1ĬÈϽøÐг־ÃÁ¬½Ó£©£¬Ëü¾Í¿ÉÒÔÀûÓó־ÃÁ¬½ÓµÄÓŵ㣬µ±Ò³Ãæ°üº¬¶à¸öÔªËØÊ±£¨ÀýÈçApplet£¬Í¼Æ¬£©£¬ÏÔÖøµØ¼õÉÙÏÂÔØËùÐèÒªµÄʱ¼ä¡£ÒªÊµÏÖÕâÒ»µã£¬ServletÐèÒªÔÚÓ¦´ðÖз¢ËÍÒ»¸öContent-LengthÍ·£¬×î¼òµ¥µÄʵÏÖ·½·¨ÊÇ£ºÏȰÑÄÚÈÝдÈëByteArrayOutputStream£¬È»ºóÔÚÕýʽд³öÄÚÈÝ֮ǰ¼ÆËãËüµÄ´óС¡£ ¨C Cookie • ²Î¿¼Java EE WEB¹¤³ÌʦÅàѵ-JDBC+Servlet+JSPÕûºÏ¿ª·¢Ö®16.Cookie ¨CHost
• °üº¬Ò»¸öURL£¬Óû§´Ó¸ÃURL´ú±íµÄÒ³Ãæ³ö·¢·ÃÎʵ±Ç°ÇëÇóµÄÒ³Ãæ ¨CIf-Modified-Since • Ö»Óе±ËùÇëÇóµÄÄÚÈÝ£¬ÔÚÖ¸¶¨µÄÈÕÆÚÖ®ºó£¬ÓÖ¾¹ýÐ޸IJŷµ»ØËü£¬·ñÔò·µ»Ø304¡°Not Modified¡±Ó¦´ð ¨CReferer • °üº¬Ò»¸öURL£¬Óû§´Ó¸ÃURL´ú±íµÄÒ³Ãæ³ö·¢·ÃÎʵ±Ç°ÇëÇóµÄÒ³Ãæ¡£ ¨CUser-Agent • ä¯ÀÀÆ÷ÀàÐÍ£¬Èç¹ûServlet·µ»ØµÄÄÚÈÝÓëä¯ÀÀÆ÷ÀàÐÍÓйØÔò¸ÃÖµ·Ç³£ÓÐÓᣠBrowserTypeServlet.java
package com.michael.servlet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class BrowserTypeServlet extends HttpServlet { /** * Constructor of the object. */ public BrowserTypeServlet() { super(); } /** * Destruction of the servlet. <br> */ public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } /** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); } /** * The doPost method of the servlet. <br> * * This method is called when a form has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String browserName = request.getHeader("user-agent"); String result=""; if(browserName.indexOf("MSIE")!=-1){ result = "Äúµ±Ç°Ê¹ÓõÄä¯ÀÀÆ÷ÊÇIE!"; }else{ result = "Äúµ±Ç°Ê¹ÓõÄä¯ÀÀÆ÷ÊÇFireFox!"; } response.setContentType("text/html;charset=gbk"); PrintWriter out = response.getWriter(); out .println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">"); out.println("<HTML>"); out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>"); out.println(" <BODY>"); out.println(result); out.println(" </BODY>"); out.println("</HTML>"); out.flush(); out.close(); } /** * Initialization of the servlet. <br> * * @throws ServletException if an error occure */ public void init() throws ServletException { // Put your code here } } ²âÊÔ
OK!
##############Michael·Ö¸îÏß################### ±¾Îijö×Ô ¡°ÍõǬDe¼¼ÊõBlog[°®Éú»î£¬°®Ñ§Ï°]¡± ²©¿Í£¬Ð»¾ø×ªÔØ£¡ ±¾Îijö×Ô 51CTO.COM¼¼Êõ²©¿Í |
¸½¼þÏÂÔØ£º
¡¡¡¡
Servlet_RequestHeader.rar
¡¡¡¡
Servlet_RequestHeader.rar












redking 
²©¿Íͳ¼ÆÐÅÏ¢
ÈÈÃÅÎÄÕÂ
×îÐÂÆÀÂÛ
ÓÑÇéÁ´½Ó

