Java的HttpServletRequest
Posted 孙崇武
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java的HttpServletRequest相关的知识,希望对你有一定的参考价值。
1、HttpServletRequest
(1)获得请求行:
method:getMethod();POST/GET;
URI:getRequestURI();可以是网址可以是相对地址也可以是绝对地址;
URL:getRequestURL();只能是网址;
重要:WEB应用名称:getContextPath();应用于重定向;
url后面的字符串:只适用于get方式;post方式没有;getQueryString();
IP:可以获取IPv4或者是IPv6;getRemoteAddr();
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>login.jsp</title> </head> <body> <form action="/WEB04/LineServlet" method="post"> 用户名:<input type="text" name = "username"><br> 密码:<input type="password" name = "password"><br> <input type="submit" value="提交"> </form> <a href = "/WEB04/ReferServlet">鼓励企业家</a> </body> </html>
package com.oracle.web; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class LineServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); //获取请求方式 String method = request.getMethod(); //获取请求资源 String URI = request.getRequestURI(); StringBuffer URL = request.getRequestURL(); //获取WEB应用名称 String webname = request.getContextPath(); //获取url后面的字符串(get方式) String str = request.getQueryString(); //获取客户端的请求ip String IP = request.getRemoteAddr(); System.out.println("请求方式"+method); System.out.println("URI"+URI); System.out.println("URL"+URL); System.out.println("WEB名称"+webname); System.out.println("url后面的字符串"+str); System.out.println("请求IP"+IP); response.getWriter().write("hello world!"); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
(2)获得请求头:
重要:getHeader("referer");用于做防盗链
package com.oracle.web; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ReferServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); //获取请求头中的referer String referer = request.getHeader("referer"); if(referer !=null&&referer.startsWith("http://localhost")){ response.getWriter().write("hello world!"); }else{ response.getWriter().write("你好世界"); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
但是:
(3)获得请求体:
获得单个参数:getParameter("name");
获得多个参数:Map<String,String[]> getParameterMap();键值对
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <form action="/WEB04/BodyServlet" method="get"> 用户名:<input type="text" name="username"><br> 密码:<input type="password" name="passworwd"><br> 爱好: <input type="checkbox" name="hobby" value="zq">足球 <input type="checkbox" name="hobby" value="ppq">乒乓球 <input type="checkbox" name="hobby" value="lq">篮球 <input type="checkbox" name="hobby" value="ymq">羽毛球 <br> <input type="submit" value="提交"> </form> </body> </html>
package com.oracle.web; import java.io.IOException; import java.util.Map; import java.util.Map.Entry; import java.util.Set; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class BodyServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //get方式解决中文乱码 String username = request.getParameter("username"); String password = request.getParameter("password"); System.out.println(username+"..."+password); //获取表单所有元素的值 //hobby,{zq,ppq,lq,ymq} Map<String,String[]> map = request.getParameterMap(); response.setContentType("text/html;charset=UTF-8"); response.getWriter().write("hello world!"); /*Set<String> set = map.keySet();//hobby只有一个hobby //增强for(变量类型:容器标识) for (String s : set) { String key = s; String[] value = map.get(key); for(String s1:value){ System.out.println(s+".."+s1); } }*/ Set<Entry<String, String[]>> set = map.entrySet(); for(Entry<String, String[]> s:set){ for(String str:s.getValue()){ System.out.println(s.getKey()+"..."+str); } } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
(4)中文乱码:
解决post提交方式的乱码:request.setCharacterEncoding("UTF-8");
解决get,post提交的方式的乱码:只能作用一个,用一个写一个;
parameter = new String(parameter.getbytes("iso8859-1"),"utf-8");
(5)request是一个域对象,作用范围:
一次请求中;访问时创建,响应结束时request销毁;
(6)请求转发:
getRequestDispatcher("/Servlet01")不用写项目名字;
requestDispathcer.forward(ServletRequest request, ServletResponse response);
package com.oracle.web01; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class Servlet01 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); request.setAttribute("name", "西红猫"); //请求转发 request.getRequestDispatcher("/Servlet02").forward(request, response); response.getWriter().write("你好世界!"); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
package com.oracle.web01; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class Servlet02 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String name = (String)request.getAttribute("name"); response.setContentType("text/html;charset=UTF-8"); response.getWriter().write("hello world!"+name); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
当改变时:
package com.oracle.web01; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class Servlet01 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); request.setAttribute("name", "西红猫"); //请求转发 /*request.getRequestDispatcher("/Servlet02").forward(request, response);*/ response.getWriter().write("你好世界!"); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
(7)重定向和请求转发区别:
重定向两次请求,转发一次请求;
重定向地址栏地址变化,转发地址不变;
重定向可以访问外部网站,转发只能访问内部资源
转发的性能要优于重定向;
以上是关于Java的HttpServletRequest的主要内容,如果未能解决你的问题,请参考以下文章
如何向我的 Java Servlet 的现有 HttpServletRequest 添加参数?
JAVA 普通类中怎么调用HTTPSERVLETRequest