无法登录登录表单

时间:2016-09-20 14:01:22

标签: jsp servlets login

我创建了一个登录网页登录网站。 每当我尝试登录它时,它都没有响应任何内容,但是如果我点击按钮上的符号,那么首先它会给我一条消息,即电子邮件或密码不正确(我已将此代码插入到我的servlet中)如果字段为空,但在此过程之后,如果我再次输入凭据,则允许我登录。

我的意思是首先我必须点击按钮而不是servlet会给出电子邮件或密码错误,而不是在输入凭据后它将允许我登录。

登录的Jsp代码



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

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



</body>

</html>
&#13;
&#13;
&#13;

jsp文件的Servelt脚本。 AdminLoginServelt.java

&#13;
&#13;
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 {
	private static final long serialVersionUID = 1L;
       
    
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
	}

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

		}
	}

}
&#13;
&#13;
&#13;

我希望你能得到我的问题。

1 个答案:

答案 0 :(得分:0)

更改表单操作,如下所示,因为../AdminLoginServlet是无效的URL

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

此外,在servlet中,以下更改与sendRedirect中的URL相关。

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

确保你有index.jsp&amp; root根文件位置中的login.jsp文件。如果您正在使用eclipse,请在WebContent文件夹中。