HttpServletRequest
方法 |
说明 |
示例 |
setCharacterEncoding("编码类型") |
请求数据的字符编码设置为“编码类型” |
request.setCharacterEncoding("utf-8") |
getParameter("name") |
请求获取名称为“name”的数据 |
request.getParameter("name") |
getParameterValues("name") |
按照“name”请求获取名称为“name”的所有数据,并组成列表 |
request.getParameterValues("name") |
getRealPath("相对地址") |
将相对地址转换成绝对地址 |
request.getRealPath("./page/shopCar.jsp") |
getRequestDispatcher("地址").forward(request,response) |
转发(服务器行为) |
request.getRequestDispatcher("./newTest.jsp").forward(request, response) |
HttpServletResponse
方法 |
说明 |
示例 |
setCharacterEncoding("编码类型") |
响应数据的字符编码设置为“编码类型” |
response.setCharacterEncoding("utf-8") |
setContentType("内容类型") |
响应的数据类型设置为“内容类型” 六中内容类型: text/html :html代码 text/plain :字符串 application/json :json数据 image/jpeg :图像 application/javascript : JS代码 text/css :CSS样式 |
response.setContentType("text/html") |
sendRedirect("地址") |
重定向(客户端行为) |
response.sendRedirect("newTest.jsp") |
getWriter() |
设置响应输出 |
response.getWriter().println("输出") |
示例
public class ServletTest extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
response.getWriter().print("3秒后跳转到注册页面");
request.getRequestDispatcher("http://www.baidu.com").forward(request,response)
response.sendRedirect("http://www.baidu.com");
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}