–典型的请求头信息
–读取HTTP请求头
–使用表格显示所有请求头信息
–理解各种请求头的含义
–区分不同的浏览器类型
–读取HTTP请求头
–使用表格显示所有请求头信息
–理解各种请求头的含义
–区分不同的浏览器类型
##############Michael分割线###################
• 典型的请求头信息
• 读取HTTP请求头
–使用HttpServletRequest中的方法
• 一般方法
–getHeader (header名称不区分大小写)
–getHeaders
–getHeaderNames
• 专门方法
–getCookies
–getAuthType
–getRemoteUser
–getContentLength
–getContentType
–getDateHeader
–getIntHeader
• 相关信息
–getMethod
–getRequestURI
–getQueryString
–getProtocol
–使用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>
<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
}
}
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
– 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返回的内容与浏览器类型有关则该值非常有用。
• 包含一个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
}
}
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分割线###################
附件:http://down.51cto.com/data/2353111
本文转自redking51CTO博客,原文链接:http://blog.51cto.com/redking/168678,如需转载请自行联系原作者