标签:path throws width Servle 网站跳转 zhang gif pass 技术分享
HttpservletRequest
简介和运行环境
我们在创建Servlet时会覆盖service()方法,或doGet()/doPost(),这些方法都有两个参数,一个为代表请求的request和代表响应response。service方法中的request的类型是ServletRequest,而doGet/doPost方法的request的类型是HttpServletRequest,HttpServletRequest是ServletRequest的子接口,功能和方法更加强大
request的运行流程
request请求航的内容
获得请求的资源:
String
getContextPath()
---web
应用的名称
String
getQueryString()
---- get
提交
url
地址后的参数字符串
username=zhangsan&password=123
package com.ithiema.line; 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 { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //1、获得请求方式 String method = request.getMethod(); System.out.println("method:"+method); //2、获得请求的资源相关的内容 String requestURI = request.getRequestURI(); StringBuffer requestURL = request.getRequestURL(); System.out.println("uri:"+requestURI); System.out.println("url:"+requestURL); //获得web应用的名称 String contextPath = request.getContextPath(); System.out.println("web应用:"+contextPath); //地址后的参数的字符串 String queryString = request.getQueryString(); System.out.println(queryString); //3、获得客户机的信息---获得访问者IP地址 String remoteAddr = request.getRemoteAddr(); System.out.println("IP:"+remoteAddr); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
获得客户机的一些信息 getRemoteAddr()获得访问的客户端ip地址
request获得请求头
ong
getDateHeader(
String name)
Enumeration
getHeaders(
String name)
int
getIntHeader(
String name)
referer头的作用:执行该此访问的的来源 做防盗链
package com.ithiema.header; import java.io.IOException; import java.util.Enumeration; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class HeaderServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //1、获得指定的头 String header = request.getHeader("User-Agent"); System.out.println(header); //2、获得所有的头的名称 Enumeration<String> headerNames = request.getHeaderNames(); while(headerNames.hasMoreElements()){ String headerName = headerNames.nextElement(); String headerValue = request.getHeader(headerName); System.out.println(headerName+":"+headerValue); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
package com.ithiema.header; 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 RefererServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //对该新闻的来源的进行判断 String header = request.getHeader("referer"); if(header!=null&&header.startsWith("http://localhost")){ //是从我自己的网站跳转过来的 可以看新闻 response.setContentType("text/html;charset=UTF-8"); response.getWriter().write("中国确实已经拿到100块金牌...."); }else{ response.getWriter().write("你是盗链者,可耻!!"); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
请求体中的内容是通过post提交的请求参数,格式是:
username=zhangsan&password=123&hobby=football&hobby=basketball
key ---------------------- value
username [zhangsan]
password [123]
hobby [football,basketball]
以上面参数为例,通过一下方法获得请求参数:
String
getParameter(
String name)
String[]
getParameterValues(
String name)
Enumeration
getParameterNames()
Map<String,String[]>
getParameterMap()
注意:get请求方式的请求参数 上述的方法一样可以获得
解决post提交方式的乱码:request.setCharacterEncoding("UTF-8");
解决get提交的方式的乱码:
parameter = new String(parameter.getbytes("iso8859-1"),"utf-8");
标签:path throws width Servle 网站跳转 zhang gif pass 技术分享
原文地址:https://www.cnblogs.com/hellowq/p/9415361.html