Java:在servlet中重新加载webapp,然后重定向请求

时间:2016-01-06 10:33:00

标签: java http tomcat servlets httprequest

我的应用中有配置页面。当用户更改配置并提交配置时,我想使用新的配置属性重新加载我的应用程序,然后重定向到某个页面。我能够使用我在这里找到的一些建议重新加载我的应用程序 Start / stop a web application from itself?

但在重新加载应用程序后,我无法重定向到新页面。

更多细节。

public class ConfigurationServlet extends HttpServlet implements ContainerServlet{

    private static final long serialVersionUID = 1L;
    private SalsaEnvironment salsaEnv;
    protected Host host = null;
    protected Wrapper wrapper = null;
    protected Context context = null;

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        // update configuration properties
        .
        .
        . 

        // reload app once configuration is changed
        restartWebApp(resp.getWriter(), "/salsadb");

        // simple redirect/forward - does not work - gives following exception
        // java.lang.NullPointerException org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:610)
        //resp.sendRedirect("/salsadb/account");
        //req.getRequestDispatcher("/account");

        // invalid old session, create a new one and try to redirect - does not work
        req.getSession(false).invalidate();
        req.getSession(true);
        req.getRequestDispatcher("/account");
    }

    @Override
    public Wrapper getWrapper() {
        // TODO Auto-generated method stub
        return this.wrapper;
    }

    @Override
    public void setWrapper(Wrapper wrapper) {
        this.wrapper = wrapper;
        if (wrapper == null) {
            context = null;
            host = null;
        } else {
            context = (Context) wrapper.getParent();
            host = (Host) context.getParent();
            Engine engine = (Engine) host.getParent();
            try {
            } catch (Exception e) {
                // ?
            }
        }
    }

    private void restartWebApp(PrintWriter writer, String path){
        if ((path == null) || (!path.startsWith("/") && path.equals(""))) {
            System.out.println("invalid path");
            return;
        }
        String displayPath = path;
        if( path.equals("/") )
            path = "";

        try {
            Context context = (Context) host.findChild(path);
            if (context == null) {
                /*writer.println(sm.getString
                               ("managerServlet.noContext",
                                   RequestUtil.filter(displayPath)));*/
                System.out.println("no context");
                return;
            }

            context.reload();
            //writer.println(sm.getString("managerServlet.reloaded", displayPath));
            System.out.println("reloaded webapp");
        } catch (Throwable t) {
            log("ManagerServlet.reload[" + displayPath + "]", t);
            /*writer.println(sm.getString("managerServlet.exception",
                                        t.toString()));*/
            System.out.println("excpetion during reloading the webapp");
        }
    }

1 个答案:

答案 0 :(得分:1)

您可能无法执行此操作,因为服务器端代码刚刚重新启动

我推荐一种基于 AJAX 的客户端方法,在这种方法中,你经常ping你的一些servlet,直到app为#34;备份"然后通过JavaScript移动到新页面。

为了实现这一目标,您首先首先将重定向发送到重新加载检查页面,然后然后(比如说1s后允许浏览器获取重定向页面)启动重启:

    req.getSession(false).invalidate();
    req.getSession(true);
    resp.sendRedirect("check_restart_status.jsp");
    //Use a timer or whatever
    new Thread(() -> {
        try{
          Thread.sleep(5000);
        } catch(InterruptedException ie) {
          return;
        }
        restartWebApp("/salsadb");
    }).start();