【问题标题】:JSP Page Redirection with and without parameters带参数和不带参数的 JSP 页面重定向
【发布时间】:2015-02-13 05:57:34
【问题描述】:

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

如何做到这一点?我正在使用 response.sendRedirect 来执行此操作...

感谢您的帮助..谢谢

【问题讨论】:

    标签: jsp servlets


    【解决方案1】:

    检查 servlet 中是否存在类型参数。

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

    【讨论】:

    • 你明白 if/else 吗?
    • 你了解NPE吗?试试这个,让我知道你观察到了什么。如果是 request.getParameter("type").equals("1"),就会导致 NPE。
    • 哦,对不起,我理解错了," ".equals() 也检查 null 。取消投票:)
    【解决方案2】:

    您应该从请求对象中获取类型属性,然后如果此索引等于 1,则重定向到 page1.jsp,否则为 page2.jsp

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-09-12
      • 1970-01-01
      • 2016-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-23
      相关资源
      最近更新 更多