如何从JSP内部重定向 [英] How to redirect from inside a JSP

查看:46
本文介绍了如何从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.

这篇关于如何从JSP内部重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆