Servlet--获取Http协议请求头信息
【摘要】
Servlet--获取Http协议请求头信息
一、创建一个webproject项目实现获取请求头信息
1、创建一个GetServlet类,继承HttpServlet通过doGet请求来获取协议请求头信息。
package tests06_2; import java.io.IOException;import java.i...
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 {
-
-
}
-
-
}
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控制台输出请求头信息。
文章来源: brucelong.blog.csdn.net,作者:Bruce小鬼,版权归原作者所有,如需转载,请联系作者。
原文链接:brucelong.blog.csdn.net/article/details/75317345
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)