2016-09-20 116 views
0

我已創建登錄網頁登錄網站。 每當我嘗試登錄它時,它都不會迴應任何內容,但是如果我點擊了signin按鈕,那麼它首先會給我一個電子郵件或密碼不正確的消息(我已將此代碼插入到我的servlet中)爲字段是空的,但在這個過程後,如果我再次輸入憑據,它允許我登錄。無法以登錄形式登錄

我的意思是首先,我必須點擊signin按鈕比servlet會給電子郵件或密碼不正確,比輸入憑據後,它將允許我登錄。登錄

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
 
    pageEncoding="ISO-8859-1"%> 
 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
 
<html> 
 
<head> 
 
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
 
<title>iCliniX</title> 
 
<%@ include file="controls/css.jsp" %> 
 
<%@ include file="controls/js.jsp" %> 
 
</head> 
 
<body> 
 

 

 

 
<div class="account-container login"> 
 
\t 
 
\t <div class="content clearfix"> 
 
\t \t 
 
\t \t <form action="../AdminLoginServlet" method="post"> 
 
\t \t 
 
\t \t \t <h1>Sign In</h1> \t \t 
 
\t \t \t 
 
\t \t \t <div class="login-fields"> 
 
\t \t \t \t 
 
\t \t \t \t <p>Sign in using your registered account:</p> 
 
\t \t \t \t 
 
\t \t \t \t <div class="field"> 
 
\t \t \t \t \t <label for="username">Username:</label> 
 
\t \t \t \t \t <input type="text" id="username" name="username" value="" placeholder="Username" class="login username-field" /> 
 
\t \t \t \t </div> <!-- /field --> 
 
\t \t \t \t 
 
\t \t \t \t <div class="field"> 
 
\t \t \t \t \t <label for="password">Password:</label> 
 
\t \t \t \t \t <input type="password" id="password" name="password" value="" placeholder="Password" class="login password-field"/> 
 
\t \t \t \t </div> <!-- /password --> 
 
\t \t \t \t 
 
\t \t \t </div> <!-- /login-fields --> 
 
\t \t \t 
 
\t \t \t <div class="login-actions"> 
 
\t \t \t \t 
 
\t \t \t \t <span class="login-checkbox"> 
 
\t \t \t \t \t <% 
 
\t \t \t \t \t \t String value = (String)request.getSession().getAttribute("registration"); 
 
\t \t \t \t \t \t if(value!=null){ 
 
\t \t \t \t \t \t \t out.print(value); 
 
\t \t \t \t \t \t } 
 
\t \t \t \t \t %> 
 
\t \t \t \t </span> 
 
\t \t \t \t \t \t \t \t \t 
 
\t \t \t \t <input type="submit" class="button btn btn-secondary btn-large" value="Sign In" /> 
 
\t \t \t \t 
 
\t \t \t </div> <!-- .actions --> 
 
\t \t \t 
 
\t \t \t 
 
\t \t </form> 
 
\t \t 
 
\t </div> <!-- /content --> 
 
\t 
 
</div> <!-- /account-container --> 
 

 
<!-- Text Under Box --> 
 
<div class="login-extra"> 
 
\t <a href="forgotpassword.jsp">Forgot Password</a> 
 
</div> <!-- /login-extra --> 
 

 

 

 
</body> 
 

 
</html>

的servelt腳本JSP文件的

JSP代碼。 AdminLoginServelt.java

package com.iclinix.controller; 
 

 
import java.io.IOException; 
 
import java.sql.Connection; 
 
import java.sql.DriverManager; 
 
import java.sql.ResultSet; 
 
import java.sql.Statement; 
 

 
import javax.servlet.ServletException; 
 
import javax.servlet.annotation.WebServlet; 
 
import javax.servlet.http.HttpServlet; 
 
import javax.servlet.http.HttpServletRequest; 
 
import javax.servlet.http.HttpServletResponse; 
 
import javax.servlet.http.HttpSession; 
 

 

 
@WebServlet("/AdminLoginServlet") 
 
public class AdminLoginServlet extends HttpServlet { 
 
\t private static final long serialVersionUID = 1L; 
 
     
 
    
 
\t protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
 
\t \t 
 
\t } 
 

 
\t 
 
\t protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
 
\t \t String email = request.getParameter("username"); 
 
\t \t String pass = request.getParameter("password"); 
 
\t \t HttpSession s = request.getSession(); 
 
\t \t try { 
 
\t \t \t Class.forName("com.mysql.jdbc.Driver"); 
 
\t \t \t Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/iclinix","root","root"); 
 
\t \t \t Statement st = con.createStatement(); 
 
\t \t \t String mail = null, password = null; 
 
\t \t \t ResultSet rs = st.executeQuery("select * from tbl_admin_details where email='"+email+"'"); 
 
\t \t \t while(rs.next()){ 
 
\t \t \t \t mail = rs.getString("email"); 
 
\t \t \t \t password = rs.getString("password"); 
 
\t \t \t } 
 
\t \t \t if(mail!=null && password!=null &&email.equals(mail)&&pass.equals(password)){ 
 
\t \t \t \t response.sendRedirect("cpaneliclinix/index.jsp"); 
 
\t \t \t } 
 
\t \t \t else{ 
 
\t \t \t \t s.setAttribute("registration", "Email Or Password are Wrong!!!"); 
 
\t \t \t \t response.sendRedirect("cpaneliclinix/login.jsp"); 
 
\t \t \t } 
 
\t \t } catch (Exception e) { 
 

 
\t \t } 
 
\t } 
 

 
}

我希望你有我的問題。

回答

0

變更表單動作如下,由於../AdminLoginServlet是無效網址

<form action="AdminLoginServlet" method="post"> 

而且,在servlet做的sendRedirect相關網址下面的變化。

response.sendRedirect("index.jsp");//cpaneliclinix/index.jsp replaced 
response.sendRedirect("login.jsp");//cpaneliclinix/login.jsp replaced 

確保你有你的根上下文位置的index.jsp &的login.jsp文件。如果您使用的是Eclipse,那麼在WebContent文件夾中。