×¢²á | µÇ¼ Íü¼ÇÃÜÂ룿 51ctoÊ×Ò³ | ²©¿Í | ÂÛ̳ | ÕÐÆ¸
ÈȵãÎÄÕ ¡¶ÕÆ¿ØWindows SErver 2..
¡¡°ïÖú

Java EE WEB¹¤³ÌʦÅàѵ-JDBC+Servlet+JSPÕûºÏ¿ª·¢Ö®14.ServletÇëÇóÍ·ÐÅÏ¢


2009-06-21 21:37:09
°æÈ¨ÉùÃ÷£ºÔ­´´×÷Æ·£¬Ð»¾ø×ªÔØ£¡·ñÔò½«×·¾¿·¨ÂÉÔðÈΡ£
¨CµäÐ͵ÄÇëÇóÍ·ÐÅÏ¢
¨C¶ÁÈ¡HTTPÇëÇóÍ·
¨CʹÓñí¸ñÏÔʾËùÓÐÇëÇóÍ·ÐÅÏ¢
¨CÀí½â¸÷ÖÖÇëÇóÍ·µÄº¬Òå
¨CÇø·Ö²»Í¬µÄä¯ÀÀÆ÷ÀàÐÍ
##############Michael·Ö¸îÏß###################
• µäÐ͵ÄÇëÇóÍ·ÐÅÏ¢
image
• ¶ÁÈ¡HTTPÇëÇóÍ·
¨CʹÓÃHttpServletRequestÖеķ½·¨
• Ò»°ã·½·¨
¨CgetHeader (headerÃû³Æ²»Çø·Ö´óСд)
¨CgetHeaders
¨CgetHeaderNames

• רÃÅ·½·¨
¨CgetCookies
¨CgetAuthType
¨CgetRemoteUser
¨CgetContentLength
¨CgetContentType
¨CgetDateHeader
¨CgetIntHeader

• Ïà¹ØÐÅÏ¢
¨CgetMethod
¨CgetRequestURI
¨CgetQueryString
¨CgetProtocol
• ʹÓñí¸ñÏÔʾÓÐÇëÇóÍ·ÐÅÏ¢
image
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>
image
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    
        }    

}
image
¿´ÏÂЧ¹û
image
image
• Àí½â¸÷ÖÖÇëÇóÍ·µÄº¬Òå
¨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·µ»ØµÄÄÚÈÝÓëä¯ÀÀÆ÷ÀàÐÍÓйØÔò¸ÃÖµ·Ç³£ÓÐÓá£
• Çø·Ö²»Í¬µÄä¯ÀÀÆ÷ÀàÐÍimage
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    
        }    

}
image
²âÊÔ
image
image
OK!
##############Michael·Ö¸îÏß###################

±¾Îijö×Ô ¡°ÍõǬDe¼¼ÊõBlog[°®Éú»î£¬°®Ñ§Ï°]¡± ²©¿Í£¬Ð»¾ø×ªÔØ£¡


¸½¼þÏÂÔØ£º
¡¡¡¡Servlet_RequestHeader.rar


Àà±ð£ºC/C++/Java ©ª ¼¼ÊõȦ() ©ª ÔĶÁ() ©ª ÆÀÂÛ() ©ªÍÆË͵½¼¼ÊõȦ ©ª·µ»ØÊ×Ò³  


    ÎÄÕÂÆÀÂÛ
 
2009-06-23 11:47:17
×øÏÂÀ´ÂýÂýÑо¿Ï¡£

2009-06-23 11:47:26
×øÏÂÀ´ÂýÂýÑо¿Ï¡£

 

·¢±íÆÀÂÛ

êÇ   ³Æ£º
ÑéÖ¤Â룺 ¡¡µã»÷ͼƬ¿ÉË¢ÐÂÑéÖ¤Âë¡¡¡¡²©¿Í¹ý2¼¶£¬ÎÞÐèÌîдÑéÖ¤Âë
ÄÚ   ÈÝ£º