【问题标题】:Redirecting to another page with JavaScript and JSP使用 JavaScript 和 JSP 重定向到另一个页面
【发布时间】:2013-02-24 13:09:24
【问题描述】:

我正在尝试在注册完成后立即使用 JSP 或 JavaScript 将用户重定向到我的登录页面。

我应该提到注册表调用同一页面,当我说window.location 时,glassfish 会抛出错误,因为找不到资源。

                                   {
    if(request.getParameter("submit")!=null)
                   {
        String uname=request.getParameter("txtusername");
        String password=request.getParameter("txtpassword");
        String mail=request.getParameter("txtemail");
        String dob=request.getParameter("dob");
        String city=request.getParameter("txtcity");
        String state=request.getParameter("txtstate");
        String country=request.getParameter("txtcountry");
        String address=request.getParameter("txtstreetaddress");
        String scity=request.getParameter("txtscity");
        String sstate=request.getParameter("txtsstate");
        String scountry=request.getParameter("txtscountry");
        String saddress=request.getParameter("txtsaddress");
        String mobile=request.getParameter("txtno");
        String gender=request.getParameter("rdbgender");
             adminClasses.user u= new adminClasses.user(0, uname, password, mail, dob, city, state, country, address, scity, sstate, scountry, saddress, mobile, gender);
              int result=adminClasses.userfunctions.addNewUser(u);
                    %>
                    <script>
                        var res=<%=result%>;
                        if(res==0)
                            {
                               window.alert("Registration Unsuccessful");
                            }
                            else
                                {
                                    window.alert("Resgistration Successful!Click Ok to Sign in");
                                    window.location="signin.jsp";
                                }
                    </script>
                    <%                  
                                           }
                              }
       catch (Exception e)
                                     {
                    out.println(e.getMessage());
                   }

【问题讨论】:

  • “signin.jsp”是否有可能没有足够的信息来解析登录页面的路径?尝试代码将页面的完整路径。

标签: javascript jsp redirect


【解决方案1】:

我认为你可以这样做:

...
int result = adminClasses.userfunctions.addNewUser(u);

if (result == 0) {
    request.getRequestDispatcher("signupSuccess.jsp").forward(request, response);
}
...

它将在服务器端评估result,然后根据result 转发到signupSuccess.jsp 页面——即URL 将保持不变,但响应将包括成功页面的评估内容.

【讨论】:

  • 你能告诉我如何将上面的代码写在一个servlet中而不是写在scriptlet中吗?
  • 你已经有一个servlet了吗?能否提供更多来源和应用配置?
【解决方案2】:

你可以试试这个

response.sendRedirect("signupSuccess.jsp");

【讨论】:

【解决方案3】:

检查当前页面的路径,如下面的代码;因为我认为你可能在

中写错了路径
 window.location="signin.jsp"

//    Example

//    Return the entire URL (of the current page):

    <script>

    document.write(location.href);

    </script>
//    The output of the code above is:

//    http://www.w3schools.com/js/js_window_location.asp

【讨论】:

    【解决方案4】:

    我已经使用 java 完成了以下代码。

    response.getWriter().write("<script> window.location="redirect_url"</script>");
    

    【讨论】:

      猜你喜欢
      • 2018-03-15
      • 1970-01-01
      • 2014-06-24
      • 1970-01-01
      • 2013-09-05
      • 2016-07-16
      • 2014-05-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多