–典型的请求头信息
–读取HTTP请求头
–使用表格显示所有请求头信息
–理解各种请求头的含义
–区分不同的浏览器类型
##############Michael分割线###################
• 典型的请求头信息
• 读取HTTP请求头
–使用HttpServletRequest中的方法
• 一般方法
–getHeader (header名称不区分大小写)
–getHeaders
–getHeaderNames
• 专门方法
–getCookies
–getAuthType
–getRemoteUser
–getContentLength
–getContentType
–getDateHeader
–getIntHeader
• 相关信息
–getMethod
–getRequestURI
–getQueryString
–getProtocol
• 使用表格显示有请求头信息
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    
        }

}

看下效果
• 理解各种请求头的含义
– Accept
• 标识浏览器能处理MIME类型
• 能发送不同的内容到不同的客户端. 例如,PNG文件有好的压缩特性,但是在浏览器中支持的不是很广泛。
• 一个Servlet可以检查是否支持PNG文件格式,如果支持
– <IMG SRC=“picture.png” ...> 否则
– <IMG SRC="picture.gif" ...>
– Accept-Encoding
• 标识浏览器能处理的编码类型
– Authorization
• 授权信息,通常出现在对服务器发送的WWW-Authenticate头的应答中
– Connection
• 表示是否需要持久连接。如果Servlet看到这里的值为“Keep-Alive”,或者看到请求使用的是HTTP 1.1(HTTP 1.1默认进行持久连接),它就可以利用持久连接的优点,当页面包含多个元素时(例如Applet,图片),显著地减少下载所需要的时间。要实现这一点,Servlet需要在应答中发送一个Content-Length头,最简单的实现方法是:先把内容写入ByteArrayOutputStream,然后在正式写出内容之前计算它的大小。
– Cookie
• 参考Java EE WEB工程师培训-JDBC+Servlet+JSP整合开发之16.Cookie
–Host
• 包含一个URL,用户从该URL代表的页面出发访问当前请求的页面
–If-Modified-Since
• 只有当所请求的内容,在指定的日期之后,又经过修改才返回它,否则返回304“Not Modified”应答
–Referer
• 包含一个URL,用户从该URL代表的页面出发访问当前请求的页面。
–User-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分割线###################

Java EE WEB工程师培训-JDBC+Servlet+JSP整合开发之14.Servlet请求头信息相关推荐

  1. Java EE WEB工程师培训-JDBC+Servlet+JSP整合开发之12.Servlet基础(2)

    –提交表单的方法 • get • post –Servlet 生命周期 –使用Servlet 输出HTML页面 –获得Servlet初始化参数 –页面导航 • 请求重定向 –response.send ...

  2. Java EE WEB工程师培训-JDBC+Servlet+JSP整合开发之13.Form表单处理(1)

    –Form 表单简介 –创建并提交表单 –使用Servlet处理表单 • 读取单个请求参数 • 读取多个表单 • 读取所有参数名称 –实例 • 注册会员 ###############Michael分 ...

  3. Java EE WEB工程师培训-JDBC+Servlet+JSP整合开发之10.Web_工程结构

    –简介 –Web应用程序的思想 –Web应用程序的目的 –Web工程结构 –web.xml 文件 –实例 • 创建一个简单的web应用程序 • 部署到tomcat中来运行 ############## ...

  4. Java EE WEB工程师培训-JDBC+Servlet+JSP整合开发之06.JDBC PreparedStatement

    –PreparedStatement –为占位符"?"赋值 –使用PreparedStatement动态执行SQL语句 ####################Michael分割线 ...

  5. JDBC+Servlet+JSP整合开发之25.JSP动作元素

    –jsp:useBean –jsp:setProperty –jsp:getProperty –jsp:forward –jsp:include –jsp:param –实例 ?计算器 ------- ...

  6. JDBC+Servlet+JSP整合开发之22.JSP简介

    –对JSP的需求 –JSP的结构 –JSP的好处 –JSP实例 ?创建一个简单的JSP页面 ########################################### ? JSP –JSP ...

  7. JDBC+Servlet+JSP整合开发之26.JSP内建对象

    –使用内建对象的目的  –内建对象  –out 内建对象  –request 内建对象  –response 对象  –session 内建对象  –pageContext 内建对象  –applic ...

  8. JDBC+Servlet+JSP整合开发之30-JDBC、Servlet、JSP的MVC

    –Servlet 的优势与弊端 –JSP 的优势与弊端 –MVC 设计模式 –实例 ?使用MVC实现学生信息的添加.显示 -----------------------------START----- ...

  9. JDBC+Servlet+JSP整合开发之29-JSP表达式语言(EL)

    –EL 简介  –EL的应用场合  –EL 的基本语法  –EL中的算术运算符  –EL中的关系运算符  –EL中的逻辑运算符 ------------------------------START- ...

最新文章

  1. 十进制转十六进制(蓝桥杯)
  2. YTU 2610: A改错题--体检情况分析
  3. Cause: java.sql.SQLSyntaxErrorException: Expression #1 of SELECT list is not in GROUP BY错误
  4. Reveal:分析iOS UI该武器
  5. Torchlight(火炬之光)人物骨骼动画
  6. 为什么Java能够持续的受到欢迎呢?原因有这些
  7. Docker 的日志相关整理
  8. 恢复出厂设置android手机号码,安卓手机怎么恢复出厂设置
  9. 内部类的小总结(语法和用法方面)
  10. python代码实现文件复制txt文件_工具类(1.1)
  11. 设计模式:高性能IO之Reactor模式
  12. fpga加载程序慢_可重构计算:基于FPGA可重构计算的理论与实践 1.器件架构 译文(一)...
  13. 全国第一家FPGA云主机(FAAS)正式启动售卖,被阿里云抢先了。
  14. MAC下ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
  15. 转载:互联网盈利模式
  16. 有了它,让我在bug面前一点也不慌!
  17. [翻译]在SQL Server中使用CLR调用.NET方法
  18. lisp用entmake生产圆柱体_蚌埠踏步板钢盖板沟盖板生产加工厂家材质齐全-老友网...
  19. 内网部署GPS定位系统方案
  20. mes系统服务器连接失败,mes系统如何连接其他系统设备?

热门文章

  1. 数学之路(2)-数据分析-R基础(4)
  2. java 微信证书文件_JAVA微信企业付款如何使用证书、证书调用实例
  3. 制造工业中的机器学习应用:I概览
  4. 工业用微型计算机(17)-指令系统(12)
  5. pypthon3精要(13)-变量命名规则之下划线
  6. mxnet基础到提高(24)-C++-符号,变量读写
  7. 【深度学习】短袖短裤识别算法冠军方案总结
  8. 【面试招聘】不要忽视实习面试失败对校招的影响
  9. C++自学者的困境!
  10. 【职场建议】开发转算法,我们应该如何准备(过来人的肺腑之言)