今天主要分享在Servlet容器在2个核心对象HttpServletRequest和HttpServletResponse中常用的方法
在web开发中经常是由多个Servlet来共同完成请求,RequestDispatcher接口就是为了多个Servlet之间的跳转而实现的,RequestDispatcher接口有2种方式实现跳转Servlet:include()和forward()
include()方法可以在当前的Servlet中显示另外一个Servlet中的内容
将HelloServlet2中的内容插入到HelloServlet中
package com.wen.servlet;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
/**
* @author hw
* @version 1.0
* @date 2021/1/8 15:27
*/
@WebServlet(
urlPatterns = {
"/servlet1"},
name = "helloServlet"
)
public class HelloServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html;charset=utf-8");
System.out.println("调用了doGet(HttpServletRequest req, HttpServletResponse resp)方法");
String servletPath = req.getServletPath();
RequestDispatcher requestDispatcher = req.getRequestDispatcher("/servlet2");
requestDispatcher.include(req,resp);
PrintWriter out = resp.getWriter();
out.println("收到了"+servletPath+"请求");
// 不应该调用父类的super.doGet(req, resp)方法,否则会出现HTTP 405错误
// super.doGet(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("doPost(HttpServletRequest req, HttpServletResponse resp)方法");
doGet(req,resp);
// 不应该调用父类的super.doPost(req, resp)方法,否则会出现HTTP 405错误
// super.doPost(req, resp);
}
@Override
public void destroy() {
System.out.println("调用了destroy方法");
super.destroy();
}
@Override
public void init() throws ServletException {
System.out.println("调用了init方法");
super.init();
}
}
package com.wen.servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
/**
* @author hw
* @version 1.0
* @date 2021/1/8 17:07
*/
@WebServlet(
urlPatterns = {
"/servlet2"},
name = "helloServlet2"
)
public class HelloServlet2 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html;charset=utf-8");
PrintWriter out = resp.getWriter();
out.println("这是HelloServlet2");
}
@Override
public void destroy() {
super.destroy();
}
@Override
public void init() throws ServletException {
super.init();
}
}
定义3个类HelloServlet3、HelloServlet4、HelloUser,
HelloServlet3将请求转发到HelloServlet4,HelloServlet4将结果返回给浏览器,HelloUser模拟dao层
package com.wen.servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* @author hw
* @version 1.0
* @date 2021/1/8 17:32
*/
@WebServlet(
urlPatterns = {
"/servlet3"},
name = "helloServlet3"
)
public class HelloServlet3 extends HttpServlet {
private HelloUser helloUser = new HelloUser();
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String username = req.getParameter("username");
String username1 = helloUser.getUsername(username);
req.setAttribute("msg",username1);
req.getRequestDispatcher("/servlet4").forward(req,resp);
}
}
package com.wen.servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
/**
* @author hw
* @version 1.0
* @date 2021/1/8 17:37
*/
@WebServlet(
urlPatterns = {
"/servlet4"},
name = "helloServlet4"
)
public class HelloServlet4 extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html;charset=utf-8");
String msg = (String)req.getAttribute("msg");
PrintWriter out = resp.getWriter();
out.println(msg);
}
}
package com.wen.servlet;
import java.util.HashMap;
import java.util.Map;
/**
* @author hw
* @version 1.0
* @date 2021/1/8 17:31
*/
public class HelloUser {
Map map=new HashMap<String,String>();
public HelloUser() {
map.put("11","join");
}
public String getUsername(String username){
return (String) map.get(username);
}
}
HttpServletResponse是用于对浏览器做出响应的操作对象,使用setContentType()设置JSP响应类型,使用getWriter()取得PrintWriter对象,使用sendRedirect()、sendError()对页面进行重定向或者是发送错误信息。
package com.wen.servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
/**
* @author hw
* @version 1.0
* @date 2021/1/8 19:39
*/
@WebServlet(
urlPatterns = {
"/servlet5"},
name = "helloServlet5"
)
public class HelloServlet5 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 必须在使用req.getParameter()之前设置才有效
req.setCharacterEncoding("utf-8");
// 必须在使用resp.getWriter()之前设置才有效
resp.setContentType("text/html;charset=utf-8");
String name = req.getParameter("name");
String city = req.getParameter("city");
PrintWriter out = resp.getWriter();
out.println(name);
out.println(city);
}
}
<%--设定页面传递参数的编码格式,所有页面的编码格式要统一--%>
<%request.setCharacterEncoding("utf-8");%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>form表单提交数据title>
head>
<br>
<form action="request_getParameter.jsp" method="post">
姓名:<input name="name" type="text" value="">br>
城市:<input name="city" value="" type="text">
<button type="submit">提交button>
form>
hr>
br>
<form action="<%=request.getContextPath()%>/servlet5" method="post">
姓名:<input name="name" type="text" value="">br>
城市:<input name="city" value="" type="text">
<button type="submit">提交button>
form>
body>
html>
sendRedirect()方法不同于forward()方法,重定向客户端会发送2次请求给服务端,在Servlet容器中需要2个线程来分别处理这2次请求,所有在第一次请求的HttpServletRequest对象中设置属性值,并不会传递到第二次请求中,而转发可以,因为转发是在服务器端进行了一次跳转,客户端只发送了一次请求。
package com.wen.servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
/**
* @author hw
* @version 1.0
* @date 2021/1/8 19:53
*/
@WebServlet(
urlPatterns = {
"/redirect1"},
name = "redirectServlet"
)
public class RedirectServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 必须在使用req.getParameter()之前设置才有效
req.setCharacterEncoding("utf-8");
// 必须在使用resp.getWriter()之前设置才有效
resp.setContentType("text/html;charset=utf-8");
req.setAttribute("name", "1111");
resp.sendRedirect(req.getContextPath() + "/redirect2");
PrintWriter out = resp.getWriter();
out.println("aaaaaaaaa");
out.flush();
out.close();
}
}
package com.wen.servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
/**
* @author hw
* @version 1.0
* @date 2021/1/8 19:54
*/
@WebServlet(
urlPatterns = {
"/redirect2"},
name = "redirectServlet2"
)
public class RedirectServlet2 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 必须在使用req.getParameter()之前设置才有效
req.setCharacterEncoding("utf-8");
// 必须在使用resp.getWriter()之前设置才有效
resp.setContentType("text/html;charset=utf-8");
String name = (String)req.getAttribute("name");
PrintWriter out = resp.getWriter();
out.println("重定向之后的内容");
out.println(name);
out.close();
}
}
在浏览器中输入
发现地址栏的URL发生了变化,而且第一个Servlet处理第一次请求中设置的属性值也没有在第二个Servlet中获取到。