您的位置:首页 > 理论基础 > 计算机网络

Servlet--获取Http协议请求头信息

2017-07-18 18:51 295 查看
Servlet--获取Http协议请求头信息

一、创建一个webproject项目实现获取请求头信息

1、创建一个GetServlet类,继承HttpServlet通过doGet请求来获取协议请求头信息。

package tests06_2;

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 GetServlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

System.out.println("请求首行信息");
String type = request.getMethod();
String url = request.getRequestURI();
String protocol = request.getProtocol();
System.out.println("首行信息:"+type+url+protocol);

System.out.println("请求头信息");
System.out.println("---下面是根据一个指定的头获取指定值--");
String accept = request.getHeader("accept");
System.out.println("accept信息:"+accept);

System.out.println("-------下面是获取所有的头Head信息----------");
Enumeration <String> enumer = request.getHeaderNames();
while(enumer.hasMoreElements()){
String key = enumer.nextElement();
System.out.println(key+"=="+request.getHeader(key));
}

}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

}

}
4000


2、web.xml配置信息如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>GetServlet</servlet-name>
<servlet-class>tests06_2.GetServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>GetServlet</servlet-name>
<url-pattern>/getservlet</url-pattern>
</servlet-mapping>

</web-app>
3、在index.jsp文件写一个简单的超链接,发送doGet请求

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>

<body>
This is my JSP page. <br>
<!-- 点击doget发送请求跳转到请求页面 -->
<a href="http://localhost:8080/test06_2/getservlet">doGet请求</a>
</body>
</html>

4、测试结果

在浏览器中输入地址 (http://localhost:8080/test06_2/)访问项目首页,并在首页点击goGet请求按钮。查看myeclipse控制台输出请求头信息。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: