我想用Servlet,JSP和Hibernate创建登录系统。但我收到一个错误。使用的Java类如下。你能帮帮我吗?
项目 - >右键单击 - >运行方式
执行结果:
HTTP状态404 - / BookStore /
登录Servlet:
Entity
@Table(name="users")
public class Users implements Serializable {
@Id @GeneratedValue
private Long id;
private String firstName;
private String middleName;
private String lastName;
private String email;
private String userId;
private String password;
//Constructor and Getter Setter methods
}
用户模型:
public class LoginService {
public boolean authenticate(String userId, String password) {
Users user = getUserByUserId(userId);
if(user!=null && user.getUserId().equals(userId) && user.getPassword().equals(password)){
return true;
}else{
return false;
}
}
public Users getUserByUserId(String userId) {
Session session = HibernateUtil.openSession();
Transaction tx = null;
Users user = null;
try {
tx = session.getTransaction();
tx.begin();
Query query = session.createQuery("from Users where userId='"+userId+"'");
user = (Users)query.uniqueResult();
tx.commit();
} catch (Exception e) {
if (tx != null) {
tx.rollback();
}
e.printStackTrace();
} finally {
session.close();
}
return user;
}
public List<Users> getListOfUsers(){
List<Users> list = new ArrayList<Users>();
Session session = HibernateUtil.openSession();
Transaction tx = null;
try {
tx = session.getTransaction();
tx.begin();
list = session.createQuery("from Users").list();
tx.commit();
} catch (Exception e) {
if (tx != null) {
tx.rollback();
}
e.printStackTrace();
} finally {
session.close();
}
return list;
}
}
登录服务:
public class HibernateUtil {
private static final SessionFactory sessionFactory;
static {
try {
sessionFactory = new AnnotationConfiguration().configure("hibernate.cfg.xml").buildSessionFactory();
} catch (Throwable ex) {
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static Session openSession() {
return sessionFactory.openSession();
}
}
Hibernate Util:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/test?zeroDateTimeBehavior=convertToNull</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password"></property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<mapping class="com.bookstore.model.Users" />
</session-factory>
</hibernate-configuration>
hibernate.cfg.xml中:
<!-- Account Login-->
<div align="right">
<form role="form" action="login" method="post" >
<label class="control-label">User Id:</label>
<input type="text" name="userId" class="span3">
<label class="control-label" >Password:</label>
<input type="password" name="password">
<input type="submit" value="Login" >
</form>
</div>
的login.jsp
<display-name>BookStore</display-name>
<servlet>
<servlet-name>Login</servlet-name>
<servlet-class>com.bookstore.controller.Login</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Login</servlet-name>
<url-pattern>/Login</url-pattern>
</servlet-mapping>
</web-app>
的web.xml
{{1}}