HeaderValue
host 127.0.0.1
connection keep-alive
cache-control max-age=0
accept text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
user-agent Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.76 Safari/537.36
accept-encoding gzip,deflate,sdch
accept-language en-US,en;q=0.8

PrintRequestHeaders.java

 

01.import java.io.PrintWriter;
02.import java.io.IOException;
03.import java.util.Enumeration;
04.import javax.servlet.annotation.WebServlet;
05. 
06.import javax.servlet.ServletException;
07.import javax.servlet.http.HttpServlet;
08.import javax.servlet.http.HttpServletRequest;
09.import javax.servlet.http.HttpServletResponse;
10. 
11.@WebServlet(name = "printHeaders", urlPatterns = {"/printHeaders"}, loadOnStartup = 1)
12.public class PrintRequestHeaders extends HttpServlet {
13. 
14.@Override
15.public void doGet(HttpServletRequest request, HttpServletResponse response)
16.throws IOException, ServletException {
17. 
18.printHeader(request, response);
19.}
20. 
21.@Override
22.public void doPost(HttpServletRequest request, HttpServletResponse response)
23.throws IOException, ServletException {
24. 
25.printHeader(request, response);
26.}
27. 
28. 
29.public void printHeader(HttpServletRequest request, HttpServletResponse response)
30.throws IOException, ServletException {
31. 
32.response.setContentType("text/html");
33. 
34.PrintWriter out = response.getWriter();
35.out.println("<!doctype html>");
36.out.println("<html><head><title>Request Headers</title></head>");
37.out.println("<body>");
38.out.println("<table align=center border=1>");
39.out.println("<tr><th>Header</th><th>Value</th></tr>");
40. 
41.String header = null;
42.Enumeration<String> e = request.getHeaderNames();
43.while(e.hasMoreElements()) {
44.header = e.nextElement();
45.if(header != null){
46.out.println("<tr><td align=center>" + header + "</td>");
47.out.println("<td align=center>" + request.getHeader(header) + "</td></tr>");
48.}
49.}
50. 
51.out.println("</table>");
52.out.println("</body></html>");
53.}
54.}
創作者介紹

資訊園

shadow 發表在 痞客邦 PIXNET 留言(0) 人氣()