使用JavaScript和JSP重定向到另一个页面

时间:2013-02-24 13:09:24

标签: javascript jsp redirect

我尝试使用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());
                   }

4 个答案:

答案 0 :(得分:1)

你可以试试这个

response.sendRedirect("signupSuccess.jsp");

答案 1 :(得分:1)

我认为你可以这样做:

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

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

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

答案 2 :(得分:0)

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

中写错了
 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

答案 3 :(得分:0)

我使用java编写了以下代码。

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