最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

Servlet中的HttpServletRequest

互联网 admin 0浏览 0评论

Servlet中的HttpServletRequest

1、获取请求行

@WebServlet(name = "requestServlet01", urlPatterns = "/requestServlet01")
public class RequestServlet01 extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//1、获取请求行//获取请求的资源路径System.out.println(req.getRequestURI());//资源路径(哪个项目):/day033/requestServlet01System.out.println(req.getRequestURL().toString());//完整路径:http://localhost:8080/day033/requestServlet01//获取请求方式System.out.println("请求方式"+req.getMethod());//获取get请求参数System.out.println("get的请求参数"+req.getQueryString());//获取客户机的ip和浏览器的端口System.out.println("远程地址:"+req.getRemoteAddr()+"远程端口:"+req.getRemotePort());}

 

2、获取请求头

常见的请求头

@WebServlet(name = "requestServlet01", urlPatterns = "/requestServlet01")
public class RequestServlet01 extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//2、获取请求头// 获取指定的请求头——getHeader里面写需要获取的请求头的key,根据key拿到请求头里的value——String value = req.getHeader("User-Agent");System.out.println(value);//批量获取请求头Enumeration<String> names = req.getHeaderNames();while (names.hasMoreElements()) {String name = names.nextElement();System.out.println(name + ":" + req.getHeader(name));}}

 

3、获取请求体

getParamter为了获取请求参数

@WebServlet(name = "requestServlet01", urlPatterns = "/requestServlet01")
public class RequestServlet01 extends HttpServlet {@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//获取请求体//单个获取请求值System.out.println(req.getParameter("username"));System.out.println(req.getParameter("password"));//获取指定名称的值,但是他的值有多个String[] names = req.getParameterValues("username");System.out.println(names);String[] passwords = req.getParameterValues("password");System.out.println(passwords);//批量获取name和值Map<String, String[]> map = req.getParameterMap();//map的迭代Set<Map.Entry<String, String[]>> entries = map.entrySet();System.out.println(entries);for (Map.Entry<String, String[]> entry : entries) {String key = entry.getKey();String[] value = entry.getValue();System.out.println("name:" + key + "-value:" + Arrays.toString(value));}}
}

 

4、重定向

@WebServlet(name = "requestServlet03", urlPatterns = "/requestServlet03")
public class RequestServlet03 extends HttpServlet {//重定向@Overrideprotected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//设置相应行resp.setStatus(302);//设置响应头--告诉浏览器重定向地址resp.setHeader("location","");//简写req.setAttribute("name", "zs");resp.sendRedirect("http://localhost:8080/day033/requestServlet02");}

5、转发

@WebServlet(name = "requestServlet04", urlPatterns = "/requestServlet04")
public class RequestServlet04 extends HttpServlet {//转发@Overrideprotected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {System.out.println("我要转发了");req.setAttribute("name","准备转发");//转发到另一个资源,服务器内部行为req.getRequestDispatcher("/requestServlet02").forward(req,resp);}
}

 

6、防盗链

通过超链接点击网站会携带这个头,请求的来源

而直接访问网址,是没有这个东西的

 

请求头中的referer这个头可以用来限制请求方式,例如,只能通过超链接进行访问,则比较请求头里携带的referer是否是所规定的,限制用户不可以通过其他方式或者直接访问网址的方式进行访问。

@WebServlet(name = "requestServlet05", urlPatterns = "/requestServlet05")
public class RequestServlet05 extends HttpServlet {//重定向@Overrideprotected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {resp.setContentType("text/html;charser=utf-8");//获取请求头里的referer的值String referer = req.getHeader("referer");//判断referer是否为null,是否有上级页面if (referer == null || referer.trim().equals("")){//动态的获取网站的前缀:req.getContextPath()resp.sendRedirect(req.getContextPath()+"/demo1.html");return;}//判断referer的地址是否是我们指定的网页链接if(!referer.startsWith("")){System.out.println("非法访问");resp.sendRedirect(req.getContextPath()+"/demo1.html");return;}resp.getWriter().write("访问成功");}
}

 

Servlet中的HttpServletRequest

1、获取请求行

@WebServlet(name = "requestServlet01", urlPatterns = "/requestServlet01")
public class RequestServlet01 extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//1、获取请求行//获取请求的资源路径System.out.println(req.getRequestURI());//资源路径(哪个项目):/day033/requestServlet01System.out.println(req.getRequestURL().toString());//完整路径:http://localhost:8080/day033/requestServlet01//获取请求方式System.out.println("请求方式"+req.getMethod());//获取get请求参数System.out.println("get的请求参数"+req.getQueryString());//获取客户机的ip和浏览器的端口System.out.println("远程地址:"+req.getRemoteAddr()+"远程端口:"+req.getRemotePort());}

 

2、获取请求头

常见的请求头

@WebServlet(name = "requestServlet01", urlPatterns = "/requestServlet01")
public class RequestServlet01 extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//2、获取请求头// 获取指定的请求头——getHeader里面写需要获取的请求头的key,根据key拿到请求头里的value——String value = req.getHeader("User-Agent");System.out.println(value);//批量获取请求头Enumeration<String> names = req.getHeaderNames();while (names.hasMoreElements()) {String name = names.nextElement();System.out.println(name + ":" + req.getHeader(name));}}

 

3、获取请求体

getParamter为了获取请求参数

@WebServlet(name = "requestServlet01", urlPatterns = "/requestServlet01")
public class RequestServlet01 extends HttpServlet {@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//获取请求体//单个获取请求值System.out.println(req.getParameter("username"));System.out.println(req.getParameter("password"));//获取指定名称的值,但是他的值有多个String[] names = req.getParameterValues("username");System.out.println(names);String[] passwords = req.getParameterValues("password");System.out.println(passwords);//批量获取name和值Map<String, String[]> map = req.getParameterMap();//map的迭代Set<Map.Entry<String, String[]>> entries = map.entrySet();System.out.println(entries);for (Map.Entry<String, String[]> entry : entries) {String key = entry.getKey();String[] value = entry.getValue();System.out.println("name:" + key + "-value:" + Arrays.toString(value));}}
}

 

4、重定向

@WebServlet(name = "requestServlet03", urlPatterns = "/requestServlet03")
public class RequestServlet03 extends HttpServlet {//重定向@Overrideprotected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//设置相应行resp.setStatus(302);//设置响应头--告诉浏览器重定向地址resp.setHeader("location","");//简写req.setAttribute("name", "zs");resp.sendRedirect("http://localhost:8080/day033/requestServlet02");}

5、转发

@WebServlet(name = "requestServlet04", urlPatterns = "/requestServlet04")
public class RequestServlet04 extends HttpServlet {//转发@Overrideprotected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {System.out.println("我要转发了");req.setAttribute("name","准备转发");//转发到另一个资源,服务器内部行为req.getRequestDispatcher("/requestServlet02").forward(req,resp);}
}

 

6、防盗链

通过超链接点击网站会携带这个头,请求的来源

而直接访问网址,是没有这个东西的

 

请求头中的referer这个头可以用来限制请求方式,例如,只能通过超链接进行访问,则比较请求头里携带的referer是否是所规定的,限制用户不可以通过其他方式或者直接访问网址的方式进行访问。

@WebServlet(name = "requestServlet05", urlPatterns = "/requestServlet05")
public class RequestServlet05 extends HttpServlet {//重定向@Overrideprotected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {resp.setContentType("text/html;charser=utf-8");//获取请求头里的referer的值String referer = req.getHeader("referer");//判断referer是否为null,是否有上级页面if (referer == null || referer.trim().equals("")){//动态的获取网站的前缀:req.getContextPath()resp.sendRedirect(req.getContextPath()+"/demo1.html");return;}//判断referer的地址是否是我们指定的网页链接if(!referer.startsWith("")){System.out.println("非法访问");resp.sendRedirect(req.getContextPath()+"/demo1.html");return;}resp.getWriter().write("访问成功");}
}

 

与本文相关的文章

发布评论

评论列表 (0)

  1. 暂无评论