概述
request相关的API
代码一
@WebServlet("/servlet/RequestDemo1")
public class RequestDemo1 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
/*
* getRequestURL方法 -- 返回客户端发出请求完整URL
getRequestURI方法 -- 返回请求行中的资源名部分
getQueryString方法 -- 返回请求行中的参数部分
getRemoteAddr方法 -- 返回发出请求的客户机的IP地址
getMethod -- 得到客户机请求方式
!!getContextPath -- 获得当前web应用虚拟目录名称 --
* 在写路径时不要将web应用的虚拟路径的名称写死,
* 应该在需要写web应用的名称的地方通过getContextPath方法动态获取
* */
StringBuffer url = request.getRequestURL();
System.out.println("url:"+url);
String uri = request.getRequestURI();
System.out.println("uri:"+uri);
String paramters = request.getQueryString();
System.out.println("paramters:"+paramters);
String ip = request.getRemoteAddr();
System.out.println("ip:"+ip);
String method = request.getMethod();
System.out.println("method:"+method);
String contextPath = request.getContextPath();
System.out.println("contextPath:"+contextPath);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
代码二
@WebServlet("/servlet/RequestDemo2")
public class RequestDemo2 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
/*
* getHeader(name)方法 --- String
getHeaders(String name)方法 --- Enumeration<String>
getHeaderNames方法 --- Enumeration<String>
* */
//根据指定的请求头名称来获取对应的值
String host = request.getHeader("host");
System.out.println("host1:"+host);
//根据请求头的名称来获取一组值放在迭代器中
Enumeration<String> hosts = request.getHeaders("host");
//遍历迭代器
System.out.println("打印host列表");
while (hosts.hasMoreElements()){
String value = hosts.nextElement();
System.out.println("host:"+value);
}
//获取全部请求头的名称
System.out.println("获取全部请求头的名称");
Enumeration<String> heads = request.getHeaderNames();
while (heads.hasMoreElements()){
String headname = heads.nextElement();
System.out.println(headname);
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
代码三
@WebServlet("/servlet/RequestDemo3")
public class RequestDemo3 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
/*
*
* getParameter(String name) --- String 通过name获得值
getParameterValues(String name) --- String[ ] 通过name获得多值 checkbox
getParameterMap() --- Map<String,String[ ]> key :name value: 多值 将查询的参数保存在一个Map中
getParameterNames() --- Enumeration<String> 获得所有name
*
* */
//指定服务器解码的码表
request.setCharacterEncoding("utf8");
//根据参数名来获取参数值
System.out.println("根据参数名来获取参数值:");
String username = request.getParameter("username");
System.out.println("username:"+username);
//根据参数名来获取多值
String[] likes = request.getParameterValues("like");
for(String l:likes){
System.out.println("like:"+l);
}
//把参数名当作键,参数多值数组当做值存放在一个map中
System.out.println("把参数名当作键,参数多值数组当做值存放在一个map中");
Map<String, String[]> map = request.getParameterMap();
for(Map.Entry<String,String[]> m:map.entrySet()){
System.out.println(m.getKey()+":"+ Arrays.toString(m.getValue()));
}
//
System.out.println("获得所有name:");
Enumeration<String> names = request.getParameterNames();
while (names.hasMoreElements()){
String name = names.nextElement();
System.out.println(name);
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
代码四【请求转发forward】
@WebServlet("/servlet/RequestDemo4")
public class RequestDemo4 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//创建调度器(指定资源跳转的位置)
RequestDispatcher rd = request.getRequestDispatcher("/servlet/RequestDemo5");
/* //
response.getWriter().write("demo4");
response.flushBuffer();//强制冲刷响应缓冲区*/
System.out.println("begin...");
//请求转发
rd.forward(request,response);
System.out.println("end...");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
@WebServlet("/servlet/RequestDemo5")
public class RequestDemo5 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("start...");
//请求转发
request.getRequestDispatcher("/servlet/RequestDemo6").forward(request,response);
System.out.println("stop...");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
@WebServlet("/servlet/RequestDemo6")
public class RequestDemo6 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.getWriter().write("demo6");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
代码五【域属性】
@WebServlet("/servlet/RequestDemo7")
public class RequestDemo7 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置域属性
request.setAttribute("name", "小朴朴");
//请求转发
request.getRequestDispatcher("/servlet/RequestDemo8").forward(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
@WebServlet("/servlet/RequestDemo7")
public class RequestDemo8 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取域属性值
Object name = request.getAttribute("name");
//
System.out.println(name);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
代码六【servlet和jsp之间共享数据】
@WebServlet("/servlet/RequestDemo9")
public class RequestDemo9 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置域属性
request.setAttribute("name","小朴朴");
request.setAttribute("age","35");
request.setAttribute("addr","北京");
request.setAttribute("gender","男");
//
request.getRequestDispatcher("/index.jsp").forward(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<h3>获取RequestDemo9的域属性值</h3>
姓名:<%=request.getAttribute("name")%><br/>
年龄:<%=request.getAttribute("age")%><br/>
地址:<%=request.getAttribute("addr")%><br/>
性别:<%=request.getAttribute("gender")%><br/>
</body>
</html>
代码七【请求包含include】
@WebServlet("/servlet/RequestDemo10")
public class RequestDemo10 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//创建调度器
RequestDispatcher rd = request.getRequestDispatcher("/servlet/RequestDemo11");
response.getWriter().write("demo10");
//调用方法做请求包含
rd.include(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
@WebServlet("/servlet/RequestDemo11")
public class RequestDemo11 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.getWriter().write("demo11");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}