我創建登錄頁面來檢索MySQL數據庫登錄信息,然後檢查用戶輸入的數據。然後,如果輸入的數據在數據庫中,那麼我希望重定向到主頁,否則會在登錄頁面(或警報消息)中創建並顯示錯誤消息。我怎樣才能實現這個?從JSP重定向
我爲此程序編碼了這麼多;但它不完整;
<form method="post" name="Login_Form" class="form-signin">
<h3 class="form-signin-heading">City Pharmacy</h3>
<hr class="colorgraph"><br>
<p> Login As Supplier </p>
<input type="text" class="form-control" name="Username" placeholder="Username" required="" autofocus="" />
<input type="password" class="form-control" name="Password" placeholder="Password" required=""/>
<button class="btn btn-lg btn-primary btn-block" name="Submit" value="Login" type="Submit">Login</button>
<br>
<a href ="index2.jsp"> Login As Admin </a>
<%
String user = request.getParameter("Username");
session.putValue("Username",user);
String pwd = request.getParameter("Password");
Class.forName("com.mysql.jdbc.Driver");
java.sql.Connection con = DriverManager.getConnection("jdbc:mysql://localhost/pmsdb","dbuser","dbpassword");
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("select * from webappligin where username='"+user+"' and type = Supplier");
if(rs.next()){
if(rs.getString("password").equals(pwd)){
//wantto go to home page
}else{
// create an error alert
}
}
%>
</form>
我沒有JSP的經驗,但我認爲你可以在你的'if-else'中使用javascript,並重定向用戶或顯示警報彈出窗口。 – wawek
http://stackoverflow.com/questions/30821635/validation-on-redirect/30840414#30840414 – Naman