javaweb基础——httpServletRequest请求对象
Posted 名字真的很急用
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javaweb基础——httpServletRequest请求对象相关的知识,希望对你有一定的参考价值。
httpServletRequest请求对象
httpServletRequest它是一个接口,它的父接口是ServletRequest,在开发中,常用的是带协议的请求对象。
请求消息:请求行,请求头,请求参数
1.获取请求行的常用方法。
1.获取请求提交的方式:getMethod()
2.获取请求的协议:getProtocal()
3.获取项目名称:getContextPath()
4.获取servlet路径:getServletPath()
5.获取请求路径:getServletURI(),getServletURL()
@WebServlet("/ques")
public class HttpServletRequest extends HttpServlet {
@Override
protected void doGet(javax.servlet.http.HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
/*1.获取请求提交的方式:getMethod()
2.获取请求的协议:getProtocal()
3.获取项目名称:getContextPath()
4.获取servlet路径:getServletPath()
5.获取请求路径:getServletURI(),getServletURL()
*/
String method = req.getMethod();
System.out.println(method);
String protocol = req.getProtocol();
System.out.println(protocol);
String contextPath = req.getContextPath();
System.out.println(contextPath);
String servletPath = req.getServletPath();
System.out.println(servletPath);
String requestURI = req.getRequestURI();
System.out.println(requestURI);
StringBuffer requestURL = req.getRequestURL();
String s =requestURL.toString();
System.out.println(s);
}
}
URI:对应的是项目名加访问路径名
URL:全部路径名
/ques
http://localhost:8080/ques
2.获取请求消息头相关的方法
1.根据消息头,获取值
String value = getHeader(消息头名称);
2.获取所有的消息头
Enumeration er =getHeaderNames();
@WebServlet("/head")
public class requestHead extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取所有的请求消息头名称
resp.setContentType("text/html;charset=utf-8");
Enumeration<String> headerNames = req.getHeaderNames();
while ((headerNames.hasMoreElements())){
String s = headerNames.nextElement();
String header = req.getHeader(s);
resp.getWriter().write("消息头"+s+"消息头对应的值"+header+"<br/>");
}
}
}
消息头host消息头对应的值localhost:8080
消息头connection消息头对应的值keep-alive
消息头upgrade-insecure-requests消息头对应的值1
消息头user-agent消息头对应的值Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3947.100 Safari/537.36
消息头accept消息头对应的值text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
消息头accept-encoding消息头对应的值gzip, deflate, br
消息头accept-language消息头对应的值zh-CN,zh;q=0.9
消息头cookie消息头对应的值Idea-569630ff=c506c55b-a36d-4346-9ce3-b90e8c2862cf; JSESSIONID=46A0D3E6E18B23F807869CE83467EF50
案例:防盗链
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<a href="/down">点击,从本页面下载才可以。。。。。。</a>
</body>
</html>
@WebServlet("/down")
public class 防盗链 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取referer请求头, referer=http://localhost:8080/....
String referer = req.getHeader("Referer");
String servletPath = req.getServletPath();
if(referer!=null && referer.contains(servletPath)){
resp.getWriter().print("download.............");
}else {
resp.sendRedirect("/download.html");
}
}
}
以上是关于javaweb基础——httpServletRequest请求对象的主要内容,如果未能解决你的问题,请参考以下文章
JavaWeb1.3.3基础加强:注解案例(注解+反射重构"JavaWeb-1.2.3基础加强:案例(反射+配置文件))