Java : 在 servlet 中重新加载 webapp,然后重定向请求
Java : Reload webapp in the servlet and then redirect request
我的应用程序中有一个配置页面。当用户更改配置并提交时,我想用新的配置属性重新加载我的应用程序,然后重定向到某个页面。我能够使用我在此处找到的一些建议重新加载我的应用程序
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");
}
}
您可能无法执行此操作,因为您的 server-side 代码 刚刚 重新启动。
我推荐一种基于 AJAX 的 client-side 方法,在这种方法中,您不断地 ping 您的一些 servlet,直到应用程序 "back up" 并且然后通过 JavaScript.
移动到新页面
为此,您 首先 将重定向发送到 reload-checking 页面,然后 然后 (比如之后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();
我的应用程序中有一个配置页面。当用户更改配置并提交时,我想用新的配置属性重新加载我的应用程序,然后重定向到某个页面。我能够使用我在此处找到的一些建议重新加载我的应用程序 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");
}
}
您可能无法执行此操作,因为您的 server-side 代码 刚刚 重新启动。
我推荐一种基于 AJAX 的 client-side 方法,在这种方法中,您不断地 ping 您的一些 servlet,直到应用程序 "back up" 并且然后通过 JavaScript.
移动到新页面为此,您 首先 将重定向发送到 reload-checking 页面,然后 然后 (比如之后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();