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控制台输出请求头信息。
一、创建一个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控制台输出请求头信息。
相关文章推荐
- HttpServletRequest获取请求得URL信息
- Servlet程序获取http协议提交的信息
- HttpServletRequest请求中获取操作系统,浏览器及浏览器版本信息
- Web HttpServletRequest的getRequestURL方法获取不到https协议请求问题
- (10A)HttpServletRequest:获取客户机信息、获取消息头信息、获取客户机请求参数、请求转发forward
- 获取HttpServletRequest请求消息头、消息行信息
- 通过HttpServletRequest获取请求header信息及request.getHeader("referer")的作用
- 在 Android 上通过模拟 HTTP multipart/form-data 请求协议信息实现图片上传
- 获取HttpServletRequest请求参数的工具类
- Get请求/Post请求与如何在Servlet中获取请求信息——day_07
- Android模拟 HTTP multipart/form-data 请求协议信息实现图片上传
- Android模拟 HTTP multipart/form-data 请求协议信息实现图片上传
- HttpServletRequest getXXX 获取参数、请求uri url、全路径
- HTTP协议分析系列(二)------HTTP请求信息和相应信息的格式
- nginx+tomcat 架构 HttpServletRequest.getScheme()获取正确的协议
- node.js学习笔记之HTTP获取客户端请求信息
- HttpServletRequest获取请求
- 第三天(Servlet中获取请求信息)
- nginx+tomcat 架构 HttpServletRequest.getScheme()获取正确的协议
- HTTP协议的请求和响应信息