标签:tchar end mamicode val html head meta except tar
回顾一下 servlet运行流程
通过request获得请求行
获得客户端的请求方式:String getMethod()
获得请求的资源:
获取请求地址uri: String getRequestURI()
获取请求地址url:StringBuffer getRequestURL()
获取去web应用名称:String getContextPath()
获取get请求提交的参数:String getQueryString()
获取客户端的IP地址:getRemoteAddr()
代码展示
register.jsp
<%@ 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/LineServlet" method="get"> 用户名:<input type="text" name="uname"><br> 密码:<input type="password" name="pwd"><br> 爱好: 足球:<input type="checkbox" name="hobby" value="zq"> 篮球:<input type="checkbox" name="hobby" value="lq"> 乒乓球:<input type="checkbox" name="hobby" value="ppq"><br> <input type="submit" value="注册"> </form> </body> </html>
LineServlet
public class LineServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //获取哦请求方式 String method=request.getMethod(); System.out.println("请求方式"+method); //获取请求地址uri String uri=request.getRequestURI(); System.out.println("请求地址uri:"+uri); //获取请求地址 StringBuffer url=request.getRequestURL(); System.out.println("请求地址url是:"+url); //获取去web应用名称 String name=request.getContextPath(); System.out.println("web应用名称是:"+name); //获取get请求提交的参数 String query=request.getQueryString(); System.out.println("get请求提交的参数"+query); //获取客户端的IP地址 String ip=request.getRemoteAddr(); System.out.println("客户端IP地址是:"+ip); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
如果jsp文件的提交方式改成post那么这个get请求提交参数就是null
通过request获得请求头
String getHeader();
referer头的作用:获取该访问的的来源
代码展示
public class HeadServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //获取refer头 String refer=request.getHeader("referer"); System.out.println(refer); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
还是使用register.jsp的代码将action中的路径改成/WEB04/HeadServlet
referer头可以做一个防盗链的作用 就是可以判断这个访问来自哪里 如果不是我指定的访问路径 就不让他链接
代码展示
referer.jsp
<%@ 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> <a href="/WEB04/RefererServlet">14天交易日A股暴跌30%</a> </body> </html>
public class RefererServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //获取referer头 String refer=request.getHeader("referer"); //解决响应乱码 response.setContentType("text/html;charset=utf-8"); if(refer.startsWith("http://localhost")){ response.getWriter().write("这是一跳爆炸新闻"); }else{ response.getWriter().write("到连着 可耻"); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
通过request获得请求体
获取请求体:
String getParameter(String key);
String[] getParameterValues(String name)
Map<String, String[]> getParameterMap()
解决post请求乱码
setCharacterEncoding("UTF-8") 指定码表
解决get 请求乱码
当客户端提交中文,那么先将数据提交,请求行会按照ISO8859-1码表进行解析 存到 request对象中 然后再传到服务器进行解析,因为服务器是utf-8码表所以解析不出来 ,所以就会响应给客户端一个乱码,这个解决这个乱码,我们可以再按照这个编码给他编译回去。
运用到
getbytes(“码表”)可以指定码表
new String(bytes,"码表")也可以指定码表
例:new String(parameter.getbytes("iso8859-1"),"utf-8")
代码展示
register.jsp post请求
<%@ 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="post"> 用户名:<input type="text" name="uname"><br> 密码:<input type="password" name="pwd"><br> 爱好: 足球:<input type="checkbox" name="hobby" value="zq"> 篮球:<input type="checkbox" name="hobby" value="lq"> 乒乓球:<input type="checkbox" name="hobby" value="ppq"><br> <input type="submit" value="注册"> </form> </body> </html>
public class BodyServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //解决post请求乱码 request.setCharacterEncoding("UTF-8"); //获取请求体 String uname=request.getParameter("uname"); //解决get请求乱码 //uname=new String(uname.getBytes("ISO8859-1"),"UTF-8"); System.out.println("用户名"+uname); String pwd=request.getParameter("pwd"); System.out.println("密码"+pwd); String[] hobbys=request.getParameterValues("hobby"); for(String s:hobbys){ System.out.println(s); } //获取所有参数所在的map集合 Map<String, String[]> map=request.getParameterMap(); //获取所有key躲在的set集合 Set<String> keys=map.keySet(); for(String key:keys){ System.out.print(key+":"); String[] values=map.get(key); for(String value:values){ System.out.print(value+" "); } System.out.println(); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
register.jsp get请求 将register文件中的post改成get,将bodyservlet文件中的 将解决post乱码的代码行注释掉,将解决get乱码代码行放开
因为在上述代码中只解决了第一个name的乱码 并没有解决下边的那个map集合中的中文乱码。
标签:tchar end mamicode val html head meta except tar
原文地址:https://www.cnblogs.com/-gongxue/p/14510517.html