简体   繁体   English

.forward()然后resp.sendRedirect()与servlet

[英].forward() then resp.sendRedirect() with servlet

I'm using servlet in order to make a java based website. 我正在使用servlet来创建一个基于java的网站。 I'm sending calcul data to a server, this server is then doing it's calculation and storing the result in the database. 我将计算数据发送到服务器,然后该服务器进行计算并将结果存储在数据库中。

what I want is display a loading page, so i should get 1/form submission 2/loading page 3/result page 我想要的是显示一个加载页面,所以我应该得到1 /表单提交2 /加载第3页/结果页面

to do that my submission form send me on a servlet wich display the loading page then check in the database if the data is present, if it is it then redirects me over the result page. 为此,我的提交表单发送给我一个显示加载页面的servlet,然后检查数据库中是否存在数据,如果存在,则将结果页面重定向到我。

this is my loading servle 这是我的装载服务

public class AffichageChargementServlet  extends HttpServlet{

    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //recuperation de la clé de l'entité qui contiadra les resultats
        String key = req.getParameter("key");
        //affichage de la page de chargement
        try {
            getServletContext().getRequestDispatcher("/loading.html").forward(req, resp);
        } catch (ServletException e) {
            System.out.println("problemme dans la servlet AffichageChargementServlet lors du chargement de /loading.html");
            e.printStackTrace();
        }
        //verification de la disponibilité de l'entité
        Key resultatKey = KeyFactory.createKey("Resultat", key);
        DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
        Entity result = null;
        while(true){
            try {
                result = datastore.get(resultatKey);
            } catch (EntityNotFoundException e) {
                System.out.println("pas encore prete");
                //ne rien faire, la data n'est pas encore disponible
            }
            if(result != null){
                System.out.println("redirection vers /affichageResults?key="+key);
                resp.sendRedirect("/affichageResults?key="+key);
            }
        }
    }

}

As I just learned, it is not possible to do multiple redirection in a servlet, so i'm getting a java.lang.IllegalStateException: Response already committed 正如我刚刚了解到的,不可能在servlet中进行多次重定向,所以我得到了一个java.lang.IllegalStateException:响应已经提交

Is there a workaround? 有解决方法吗?

thanks 谢谢

Response already committed means you have already write something on response and you are trying to forward or redirect. Response already committed意味着您已经在响应中写了一些内容,并且您正在尝试转发或重定向。

You should use either of Forward or Redirect on the response not both, your code does both. 您应该在响应中使用Forward或Redirect,而不是两者都使用,您的代码同时执行这两项操作。 You need to decide first whether you want to forward or redirect and then commit the response based on your decision 您需要先决定是要转发还是重定向,然后根据您的决定提交响应

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM