首页 > 解决方案 > 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?

标签: javajspexceptionservlets

解决方案


要创建一个 servlet 来处理请求,您需要扩展javax.servlet.http.HttpServlet和覆盖您需要的任何方法(如在您的示例中,它通常只是doGetand 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.doGetsuper.doPost

java.lang.IllegalStateException:提交响应后无法转发

所以,简而言之:

  • 当你调用super.doGetorsuper.doPost然后你的代码时,你基本上会尝试写两次响应,所以你得到了非法状态异常。
  • 删除对super.doGetsuper.doPost仅允许您的代码处理请求并生成单个响应的调用,这就是它起作用的原因。

推荐阅读