2013-02-24 93 views
1

我試圖在註冊完成後立即使用JSP或JavaScript來將用戶重定向到我的日誌頁面。使用JavaScript和JSP重定向到另一個頁面

我應該提及註冊表格正在調用相同的頁面,當我說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()); 
        } 
+0

有沒有可能「的signin.jsp」沒有足夠的信息來解決路徑登錄頁?嘗試代碼將頁面的完整路徑。 – deau 2013-02-24 13:14:46

回答

1

我認爲你可以這樣來做:

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

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

這將在服務器端評估result然後,取決於result ,將轉發到signupSuccess.jsp頁面 - 即URL將保持不變,但響應將包括成功頁面的評估內容。

+0

你能告訴我如何在servelet中編寫上面的代碼,而不是將它寫在scriptlet中嗎? – 2013-02-27 09:15:04

+0

你已經有了一個servlet嗎?你能提供更多的資源和應用程序配置嗎? – NSPKUWCExi2pr8wVoGNk 2013-02-28 13:45:30

+0

謝謝你回覆tomas但我解決了這個問題.. – 2013-03-03 10:14:21

1

你可以試試這個

response.sendRedirect("signupSuccess.jsp"); 
+0

沒有這種方法,只有[sendRedirect](http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletResponse.html#sendRedirect(java.lang.String))。 – NSPKUWCExi2pr8wVoGNk 2013-02-25 09:27:07

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 
相關問題