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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15052037/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 23:18:44  来源:igfitidea点击:

Redirecting to another page with JavaScript and JSP

javascriptjspredirect

提问by Abdullah Malikyar

I am trying to redirect the user to my log in page as soon as the registration is done, using either JSP or JavaScript.

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

I should mention that the registration form is calling the same page and when I say window.location, glassfish throws error as resource not found.

我应该提到注册表正在调用相同的页面,当我说时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());
                   }

采纳答案by NSPKUWCExi2pr8wVoGNk

I think you could do it this way:

我认为你可以这样做:

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

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

It will evaluate the resulton the server side and then, depending on the result, will forward to a signupSuccess.jsppage – i.e. the URL will stay the same, but the response will include success page's evaluated content.

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

回答by JRR

You can try this

你可以试试这个

response.sendRedirect("signupSuccess.jsp");

回答by shareef

check your path for current page like code below ; because i think you may be writing the path wrong in

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

 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

回答by Thusitha Indunil

I have done like following code using java.

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

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