如何从JSP内部重定向

问题描述:

我有一个JSP页面,需要引入有条件的重定向逻辑.例如,如果变量高于某个阈值,则继续像正常一样呈现JSP.否则,我需要将浏览器重定向到例如http://myapp.example.com/fizz.

I have a JSP page and need to introduce conditional redirect logic. For instance, if a variable is above a certain threshold, then continue rendering the JSP like normal. Otherwise, I need to redirect the browser to, say, http://myapp.example.com/fizz.

if(var > threshold) {
    // Render page like normal
} else {
    // Redirect to http://myapp.example.com/fizz.
}

关于如何完成此重定向部分的任何想法(从JSP页面内部)?

Any ideas as to how I could accomplish the redirect portion of this (from inside a JSP page)?

尝试一下-

<%
// New location to be redirected
String site = new String("http://myapp.example.com/fizz");
response.setStatus(response.SC_MOVED_TEMPORARILY);
response.setHeader("Location", site); 
%>

或者您也可以使用-

<%    
  response.sendRedirect("http://myapp.example.com/fizz");
 %>

您可以使用这两种方法中的任何一种.

You can use any approach from both of them.