带有和不带参数的JSP页面重定向

时间:2015-02-13 05:57:35

标签: jsp servlets

我有一种情况,我有两个jsp页面(page1.jsp,page2.jsp)加上一个index.jsp页面...用户将调用index.jsp页面..现在,如果他/她点击了网址使用参数(http://localhost:8080/Test/Index.jsp?type=1)然后他应该被重定向到page1,如果他没有参数(http://localhost:8080/Test/Index.jsp),那么它应该带他到第2页。

如何实现这一目标?我正在使用response.sendRedirect来做这个......

感谢您的帮助。谢谢

2 个答案:

答案 0 :(得分:1)

检查servlet中是否存在type参数。

if("1".equals(request.getParameter("type"))
{
     response.sendRedirect("Page1");
}
else
{
     response.sendRedirect("Page2");
}

答案 1 :(得分:0)

你应该从请求对象获取type属性然后如果这个索引等于1重定向到page1.jsp,否则page2.jsp

String typeVal = (String)request.getParameter("type");
if ("1".equals(typeVal)) {
     response.sendRedirect("Page1.jsp");
} else {
     response.sendRedirect("Page2.jsp");
}