2016-09-20 114 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文件夹中。