當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Java EE WEB工程师培训-JDBC+Servlet+JSP整合开发之14.Servlet请求头信息
生活随笔
收集整理的這篇文章主要介紹了
Java EE WEB工程师培训-JDBC+Servlet+JSP整合开发之14.Servlet请求头信息
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
–典型的請求頭信息
–讀取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分割線###################
–讀取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请求头信息的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C# 之不安全代码(2)
- 下一篇: Indy10中IdMappedPortT