Request_获取请求数据_代码案例和Request_Request获取请求头数据
作者:互联网
URL:统一资源定位符:Http://localhost/day14/demo1 中华人民共和国
URL:统一资源标示符:/day14/demo1 共和国
java代码
@WebServlet("/requestDemo1") public class ServletDome5 extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //获取请求方式GET String method = req.getMethod(); System.out.println(method); //获取虚拟路径 String contextPath = req.getContextPath(); System.out.println(contextPath); //获取Servlet路径 String servletPath = req.getServletPath(); System.out.println(servletPath); //获取get方式请求参数 String queryString = req.getQueryString(); System.out.println(queryString); // String requestURI = req.getRequestURI(); StringBuffer requestURL = req.getRequestURL(); System.out.println(requestURI); System.out.println(requestURL); //获取协议及版本:http:/1.1 String protocol = req.getProtocol(); System.out.println(protocol); //获取客户机的IP地址 String remoteAddr = req.getRemoteAddr(); System.out.println(remoteAddr); }
Request_Request获取请求头数据
方法:
String getHeader(String name):通过请求头的名称获取请求头的值
Enumeration<String> getHeaderNames():获取所有的请求名称
Enumeration<String> headerNames = req.getHeaderNames(); while (headerNames.hasMoreElements()){ String s = headerNames.nextElement(); String header = req.getHeader(s); System.out.println(s+"---"+header); }
String referer = req.getHeader("referer"); System.out.println(referer); if (referer!=null){ if (referer.contains("/day14")){ // System.out.println("播放电影...."); resp.setContentType("text/html;charset=utf-8"); resp.getWriter().write("播放电影"); }else { // System.out.println("想看电影吗"); resp.setContentType("text/html;charset=utf-8"); resp.getWriter().write("播放电影"); } }
搜索
复制
Enumeration<String> headerNames = req.getHeaderNames(); while (headerNames.hasMoreElements()){ String s = headerNames.nextElement(); String header = req.getHeader(s); System.out.println(s+"---"+header); }
标签:请求,req,Request,System,获取,println,out,String 来源: https://www.cnblogs.com/12-12-12/p/16562331.html