java - Why do I have to delete the calls to "super.doGet(req, resp)" and "super.doPost(req, resp)"?
问题描述
I was trying to pass values from a servlet to my jsp page using the code below:
package lecture_mvc.mvc.simple;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class SimpleController extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
super.doGet(req, resp);
processRequest(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
super.doPost(req, resp);
processRequest(req,resp);
}
private void processRequest(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
String type = request.getParameter("type");
Object resultObject = null;
if (type == null || type.equals("greeting")) {
resultObject = "Hello";
} else if (type.equals("date")) {
resultObject = new java.util.Date();
} else {
resultObject = "Invalid Type";
}
request.setAttribute("result", resultObject);
RequestDispatcher dispatcher = request.getRequestDispatcher("/simpleView.jsp");
dispatcher.forward(request, response);
}
}
but it throws a java.lang.IllegalStateException
.
So I deleted the super.doGet(req, resp);
and super.doPost(req, resp);
code and then it works well.
What is the problem with using those lines of code?
解决方案
要创建一个 servlet 来处理请求,您需要扩展javax.servlet.http.HttpServlet
和覆盖您需要的任何方法(如在您的示例中,它通常只是doGet
and doPost
。您正在覆盖方法,但您还调用javax.servlet.http.HttpServlet
.
javax.servlet.http.HttpServlet
不知道你想做什么,所以它的实现只是向客户端返回一个错误。代码是这样的:
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
String protocol = req.getProtocol();
String msg = lStrings.getString("http.method_get_not_supported");
if (protocol.endsWith("1.1")) {
resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, msg);
} else {
resp.sendError(HttpServletResponse.SC_BAD_REQUEST, msg);
}
}
使用 生成响应resp.sendError
。如果您阅读此方法的 JavaDoc,它会说(强调我的):
如果响应已提交,则此方法将引发 IllegalStateException。使用此方法后,应将响应视为已提交,不应写入.
然后,当您返回代码时,您会执行 a ,但or调用dispatcher.forward(request, response);
已经提交了响应。您无法再写入已提交的响应,因为响应内容已写入输出流并发送到客户端。当你这样做时,你会得到你在帖子中描述的内容,一个非法的状态异常:super.doGet
super.doPost
java.lang.IllegalStateException:提交响应后无法转发
所以,简而言之:
- 当你调用
super.doGet
orsuper.doPost
然后你的代码时,你基本上会尝试写两次响应,所以你得到了非法状态异常。 - 删除对
super.doGet
或super.doPost
仅允许您的代码处理请求并生成单个响应的调用,这就是它起作用的原因。
推荐阅读
- excel - Oracle BI Publisher Excel 模板创建 - 字段的值全部出现在一个单元格中
- powershell - Move-ADObject 找不到具有标识的对象
- dialogflow-es - Actions On Google 导入现有项目
- python - 使用 Pythonic 矢量化代码在 2D numpy 数组中索引不同大小的范围
- javascript - Socket IO 监听所有 Sessions id
- angular - 使用 CdkVirtualFor 和 templateCacheSize 进行排序
- javascript - 比较两个对象并替换通用对象属性
- asp.net-core - InvalidCastException:无法将“System.Double”类型的对象转换为“System.Single”类型
- javascript - API Binance 获取免费余额
- postgresql - Postgres - 确定更新的列名