Request
请求对象的常用方法-
获取各种路径:
返回值方法名说明
StringgetContextPath()获取虚拟目录名称StringgetServletPath()获取Servlet映射路径StringgetRemoteAddr()获取访问者ip地址StringgetQueryString()获取请求的消息数据StringgetRequestURI()获取统一资源标识符 比URL大StringBuffergetRequestURL()获取统一资源定位符
@WebServlet("/ServletDemo01")
public class ServletDemo01 extends HttpServlet {
@Override
protected void service(HttpServletRequest req
, HttpServletResponse resp
) throws ServletException
, IOException
{
String contextPath
= req
.getContextPath();
System
.out
.println(contextPath
);
String servletPath
= req
.getServletPath();
System
.out
.println(servletPath
);
String remoteAddr
= req
.getRemoteAddr();
System
.out
.println(remoteAddr
);
String queryString
= req
.getQueryString();
System
.out
.println(queryString
);
String requestURI
= req
.getRequestURI();
System
.out
.println(requestURI
);
StringBuffer requestURL
= req
.getRequestURL();
System
.out
.println(requestURL
);
}
}
获取请求头信息:
返回值方法名说明
StringgetHeader(String name)根据请求头获取一个值,键不区分大小写EnumerationgetHeaders(String name)根据请求头名称获取多个值EnumerationgetHeaderNames()获取所有请求头名称
String connection
= req
.getHeader("connection");
System
.out
.println(connection
);
获取求情参数信息:
返回值方法名说明
Map<String,String[]>getParameterMap()返回所有参数的键值对EnumerationgetParameterNamer()获取所有名称StringgetParameter(String name)根据名称获取数据 一个键对应一个值String[]getParameterValues(String name)获取所有数据 一个键对应多个值
req
.方法名
@WebServlet("/ServletDemo3")
public class ServletDemo3 extends HttpServlet {
@Override
protected void service(HttpServletRequest req
, HttpServletResponse resp
) throws ServletException
, IOException
{
String contextPath
= req
.getContextPath();
System
.out
.println(contextPath
);
String requestURI
= req
.getRequestURI();
StringBuffer requestURL
= req
.getRequestURL();
String header
= req
.getHeader("user-agent");
System
.out
.println(header
);
String username
= req
.getParameter("username");
System
.out
.println(username
);
String
[] hobbies
= req
.getParameterValues("hobby");
System
.out
.println(Arrays
.toString(hobbies
));
Map
<String
, String
[]> map
= req
.getParameterMap();
Set
<String> keys
= map
.keySet();
for (String key
: keys
) {
String
[] values
= map
.get(key
);
System
.out
.println(key
+":"+Arrays
.toString(values
));
}
}
}
封装数据相关操作:
Beanutils:和新方法populate(要封装的对象,map集合数据)
设置编码(乱码问题):
req
.setCharacterEncoding("UTF-8");
作为域对象共享数据:作用范围小 一次请求
getAttribute
setAttribute
removeAttribute
请求转发:
转发是一次请求,才可以使用request域共享数据
forward()
req
.setAttribute("aaa","123");
req
.getRequestDispatcher("ServletDemo6").forward(req
,resp
);
Object aaa
= req
.getAttribute("aaa");
System
.out
.println(aaa
);
请求包含:
include()
Response:
操作响应行:
resp.setStatus(int 状态码)
字节流响应消息:
getOutPutStream():响应字节输出流对象
resp
.setContentType("text/html;charset=UTF-8");
ServletOutPutStream os
=resp
.getOutPutStream();
String str
= "你好";
os
.write(str
.getBytes("UTF-8"));
字符流响应消息:
getWriter():获取响应字符输出流对象
resp
.setContentType("text/html;charset=UTF-8");
PrintWriter pw
=resp
.getWriter();
String str
= "你好";
pw
.writer(str
);
字符乱码问题:
//设置浏览器的编码格式 resp.setContentType(“text/html;charset=UTF-8”);