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

举报
brucexiaogui 发表于 2021/12/30 00:20:28 2021/12/30
【摘要】 Servlet--获取Http协议请求头信息 一、创建一个webproject项目实现获取请求头信息 1、创建一个GetServlet类,继承HttpServlet通过doGet请求来获取协议请求头信息。 package tests06_2; import java.io.IOException;import java.i...

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


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

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


  
  1. package tests06_2;
  2. import java.io.IOException;
  3. import java.io.PrintWriter;
  4. import java.util.Enumeration;
  5. import javax.servlet.ServletException;
  6. import javax.servlet.http.HttpServlet;
  7. import javax.servlet.http.HttpServletRequest;
  8. import javax.servlet.http.HttpServletResponse;
  9. public class GetServlet extends HttpServlet {
  10. public void doGet(HttpServletRequest request, HttpServletResponse response)
  11. throws ServletException, IOException {
  12. System.out.println("请求首行信息");
  13. String type = request.getMethod();
  14. String url = request.getRequestURI();
  15. String protocol = request.getProtocol();
  16. System.out.println("首行信息:"+type+url+protocol);
  17. System.out.println("请求头信息");
  18. System.out.println("---下面是根据一个指定的头获取指定值--");
  19. String accept = request.getHeader("accept");
  20. System.out.println("accept信息:"+accept);
  21. System.out.println("-------下面是获取所有的头Head信息----------");
  22. Enumeration <String> enumer = request.getHeaderNames();
  23. while(enumer.hasMoreElements()){
  24. String key = enumer.nextElement();
  25. System.out.println(key+"=="+request.getHeader(key));
  26. }
  27. }
  28. public void doPost(HttpServletRequest request, HttpServletResponse response)
  29. throws ServletException, IOException {
  30. }
  31. }


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


  
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app version="3.0"
  3. xmlns="http://java.sun.com/xml/ns/javaee"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
  6. <servlet>
  7. <description>This is the description of my J2EE component</description>
  8. <display-name>This is the display name of my J2EE component</display-name>
  9. <servlet-name>GetServlet</servlet-name>
  10. <servlet-class>tests06_2.GetServlet</servlet-class>
  11. </servlet>
  12. <servlet-mapping>
  13. <servlet-name>GetServlet</servlet-name>
  14. <url-pattern>/getservlet</url-pattern>
  15. </servlet-mapping>
  16. </web-app>

3、在index.jsp文件写一个简单的超链接,发送doGet请求


  
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
  2. <%
  3. String path = request.getContextPath();
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  5. %>
  6. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  7. <html>
  8. <head>
  9. <base href="<%=basePath%>">
  10. <title>My JSP 'index.jsp' starting page</title>
  11. <meta http-equiv="pragma" content="no-cache">
  12. <meta http-equiv="cache-control" content="no-cache">
  13. <meta http-equiv="expires" content="0">
  14. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  15. <meta http-equiv="description" content="This is my page">
  16. <!--
  17. <link rel="stylesheet" type="text/css" href="styles.css">
  18. -->
  19. </head>
  20. <body>
  21. This is my JSP page. <br>
  22. <!-- 点击doget发送请求跳转到请求页面 -->
  23. <a href="http://localhost:8080/test06_2/getservlet">doGet请求</a>
  24. </body>
  25. </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

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。