HttpServletRequest对象代表客户端的请求,当客户端通过HTTP协议访问服务器时,HTTP请求头中的所有信息都封装在这个对象中,开发人员通过这个对象的方法,可以获得客户这些信息。
request常用方法
获得客户机信息
getRequestURL方法返回客户端发出请求时的完整URL。
getRequestURI方法返回请求行中的资源名部分。
getQueryString 方法返回请求行中的参数部分。
getRemoteAddr方法返回发出请求的客户机的IP地址
getRemoteHost方法返回发出请求的客户机的完整主机名
getRemotePort方法返回客户机所使用的网络端口号
getLocalAddr方法返回WEB服务器的IP地址。
getLocalName方法返回WEB服务器的主机名
getMethod得到客户机请求方式
package com.hbsi.request;
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 Request1 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println(request.getRequestURI());
System.out.println(request.getRequestURL());
System.out.println(request.getQueryString());
System.out.println(request.getMethod());
System.out.println(request.getRemoteAddr());
System.out.println(request.getRemoteHost());
System.out.println(request.getRemotePort());
System.out.println(request.getLocalAddr());
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
获得客户机请求头
getHead(name)方法
getHeaders(String name)方法
getHeaderNames方法
获得客户机请求参数(客户端提交的数据)
getParameter(name):获取指定名称的参数值。这是最为常用的方法之一。
getParameterValues(String name):获取指定名称参数的所有值数组。它适用于一个参数名对应多个值的情况。如页面表单中的复选框,多选列表提交的值。
getParameterNames():返回一个包含请求消息中的所有参数名的Enumeration对象。通过遍历这个Enumeration对象,就可以获取请求消息中所有的参数名。
getParameterMap():返回一个保存了请求消息中的所有参数名和值的Map对象。Map对象的key是字符串类型的参数名,value是这个参数所对应的Object类型的值数组。
package com.hbsi.request;
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 Request2 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//String value = request.getHeader("Accept");
//System.out.println(value);
Enumeration e = request.getHeaderNames();
while(e.hasMoreElements()){
String name = (String) e.nextElement();
String value = request.getHeader(name);
System.out.println(name+":"+value);
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
request常见应用
各种表单输入项数据的获取
text、password、radio、checkbox、
file、select、textarea、 hidden、
image、button给js编程用
下面是request代码
package com.hbsi.request;
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 Reqeust3 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println(request.getParameter("name"));
}
private void test1form(HttpServletRequest request) {
String name = request.getParameter("username");
String password = request.getParameter("password");
String sex = request.getParameter("sex");
String city = request.getParameter("city");
String [] likes = request.getParameterValues("likes");
String likeString="";
for(int i=0;likes!=null && i<likes.length;i++){
String like = likes[i];
likeString +=like;
}
String memo = request.getParameter("memo");
System.out.println(name+","+password+","+sex+","+city+","+likeString+","+memo);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
请求参数的中文乱码问题
URL地址的编码
package com.hbsi.request;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Request4 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String value = request.getParameter("name");
System.out.println(new String(value.getBytes("iso8859-1"),"UTF-8"));
}
private void test2(HttpServletRequest request)
throws UnsupportedEncodingException {
//get方式
String name = request.getParameter("username");
name = new String(name.getBytes("iso8859-1"),"UTF-8");
System.out.println(name);
}
private void test1(HttpServletRequest request)
throws UnsupportedEncodingException {
request.setCharacterEncoding("UTF-8");//只对post方式起作用
System.out.println(request.getParameter("username"));
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
防盗链
package com.hbsi.request;
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 Request5 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String referer = request.getHeader("referer");
if(referer==null || !referer.startsWith("http://localhost")){
response.sendRedirect("/servletdemo/index.jsp");
return;
}
response.setContentType("text/html;charset=utf-8");
String data = "秘密!!!!";
response.getWriter().write(data);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
request对象实现请求转发:请求转发指一个web资源收到客户端请求后,通知服务器去调用另外一个web资源进行处理。
请求转发
Servlet API中定义了一个RequestDispatcher接口,俗称请求分派器。它定义了如下两个方法:
public void forward(ServletRequest request, ServletResponse response)
public void include(ServletRequest request, ServletResponse response)
获取RequestDispatcher实例的方式主要有两种:
调用ServletContext接口提供的getRequestDispatcher(String url)方法。
调用ServletRequest接口提供的getRequestDispatcher(String url)方法。
RequestDispatcher
include方法:
RequestDispatcher.include方法用于将RequestDispatcher对象封装的资源内容作为当前响应内容的一部分包含进来,从而实现可编程的服务器端包含功能。
被包含的Servlet程序不能改变响应消息的状态码和响应头,如果它里面存在这样的语句,这些语句的执行结果将被忽略。
package com.hbsi.request;
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 Request6 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.getRequestDispatcher("/header.html").include(request, response);
response.getOutputStream().write("aaaaaaaaaaaa".getBytes());
request.getRequestDispatcher("/footer.html").include(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}