我目前在动态 Web 项目中使用 Hibernate,但在登录页面时遇到问题:我总是被重定向到不正确的登录页面。我是否拥有有效凭证并不重要。我认为这是因为数据库没有被读取。
这是我的 servlet:
/**
* @see HttpServlet#HttpServlet()
*/
public Loginaction() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession(true);
String username = request.getParameter("nom");
String password = request.getParameter("pass");
UserDAO user = new UserDAO();
boolean result = user.authentification(username, password);
BaseUser user1 = user.getusername(username);
if(result == true){
session.setAttribute("user", user1);
request.getRequestDispatcher("Liste_Projets.jsp").forward(request, response);
}else{
request.getRequestDispatcher("wrong_list.jsp").forward(request, response);
}
}
这是我的 UserDAO:
public class UserDAO extends BaseUser{
public boolean authentification(String login, String mdp){
BaseUser user = getusername(login);
if(user!=null){
if(user.getLogin().equals(login) && user.getPassword().equals(mdp))
return true;
else
return false;
}
}
public BaseUser getusername(String login) {
Session session = HibernateUtil.currentSession();
Transaction tx = null;
BaseUser user = null;
try {
tx = session.getTransaction();
tx.begin();
Query query = session.createQuery("from User where LOGIN='"+login+"'");
user = (BaseUser)query.uniqueResult();
tx.commit();
} catch (Exception e) {
if (tx != null) {
tx.rollback();
}
e.printStackTrace();
} finally {
HibernateUtil.closeSession();
}
return user;
}
这是我的 setter 和 getter:
package suivi.base;
import java.io.Serializable;
/*
* @hibernate.class
* table="User"
*/
public abstract class BaseUser implements Serializable {
private int hashCode = Integer.MIN_VALUE;
// primary key
private java.lang.String _login;
// fields
private java.lang.String _nomPrenom;
private java.lang.String _password;
private java.lang.String _login;
// constructors
public BaseUser () {
initialize();
}
/**
* Constructor for primary key
*/
public BaseUser (java.lang.String _login) {
this.setLogin(_login);
initialize();
}
/**
protected void initialize () {}
/**
* Return the unique identifier of this class
* @hibernate.id
* generator-class="vm"
* column="LOGIN"
*/
public java.lang.String getLogin () {
return _login;
}
/**
* Set the unique identifier of this class
* @param _login the new ID
*/
public void setLogin (java.lang.String _login) {
this._login = _login;
this.hashCode = Integer.MIN_VALUE;
}
/**
* Return the value associated with the column: NOM_PRENOM
*/
public java.lang.String getNomPrenom () {
return _nomPrenom;
}
/**
* Set the value related to the column: NOM_PRENOM
* @param _nomPrenom the NOM_PRENOM value
*/
public void setNomPrenom (java.lang.String _nomPrenom) {
this._nomPrenom = _nomPrenom;
}
/**
* Return the value associated with the column: PASSWORD
*/
public java.lang.String getPassword () {
return _password;
}
/**
* Set the value related to the column: PASSWORD
* @param _password the PASSWORD value
*/
public void setPassword (java.lang.String _password) {
this._password = _password;
}
public String toString () {
return super.toString();
}
}
JSP页面:
<script type="text/javascript">
function Annuler(){
document.login.reset();
}
function valider()
{
var ok = true;
if (document.getElementById("nom").value.length<1)
{
ok = false;
document.getElementById("erreurLogin").innerHTML="*Obligatoire";
}
var x = document.getElementById("pass").value;
if(x == "")
{
ok = false;
document.getElementById("erreurLogin1").innerHTML="*Obligatoire";
}
if(ok)
{
document.login.submit()
}
}
</script>
...//code
<body>
<form name="login" method="Post" action="Loginaction">
<tr height="40px" valign="bottom">
<td width="150px"> </td>
<td class="StyleLabel">Identifiant: </td>
<td width="1px"></td>
<td colspan="2" ><input class="input" type="text" size="24" maxlength="20" name="nom" id="nom"></td>
<td><label class="asterix" id="erreurLogin"></label></td>
</tr>
<tr height="40px" valign="bottom">
<td width="150px"></td>
<td width="100px" class="StyleLabel" >Mot de passe: </td>
<td width="1px"></td>
<td colspan="2" > <input class="input" type="password" size="24" autocomplete="off" maxlength="20" name="pass" id="pass"> </td>
<td ><label class="asterix" id="erreurLogin1"></label></td>
</tr>
<tr height="40px" >
<td> </td>
<td colspan="3" align="center"> <img src="images/btnValid.gif" name="valid" value="valid" type="submit" onclick="valider();"/></td>
<td colspan="3" align="center"> <img src="images/btnAnnul.gif" onclick="Annuler();"/></td>
</tr>
</table>
最佳答案
在 UserDAO 类中进行以下更改,
public boolean authentification(String login, String mdp){
BaseUser user = getusername(login);
if(user!=null )
{
if(user.getLogin().equals(login) && user.getPassword().equals(mdp))
return true;
else
return false;
}
}
关于mysql - 使用 hibernate 登录失败 servlet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23620825/