request对象常用API
1)取得请求头相关的信息
*a)request.getHeader(String):String
b)request.getHeaders(String):Enumeration
c)getHeaderNames():Enumeration
java代码实现:
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 Rdome2 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setCharacterEncoding("utf-8"); //客户端接收的语言类型 String acceptLanguage = response.getHeader("Accept-Language"); //客户端版本号的名字 String userAgent = request.getHeader("User-Agent"); //连接的目标主机和端口号 String host = request.getHeader("Host"); response.getWriter().write("客户端接收的语言类型 Accept-Language=" + acceptLanguage + "<br/>"); response.getWriter().write("客户端版本号的名字 User-Agent=" + userAgent + "<br/>"); response.getWriter().write("连接的目标主机和端口号 Host=" + host + "<br/>"); response.getWriter().write("========================================= "+ "<br/>"); //一次性取得所有的请求头信息 Enumeration<String> enums = request.getHeaderNames(); while(enums.hasMoreElements()) { //请求头 String keys = enums.nextElement(); Enumeration<String> enums2 = request.getHeaders(keys); while(enums2.hasMoreElements()) { String value = enums2.nextElement(); response.getWriter().write(keys+"<->"+value+"<br/>"); } } }}
点击查看更多内容
为 TA 点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦