1.在MySQL里面新建一个数据库,里面新建一个表,加入相应的字段,新建一个Dynamic Web Project项目,整体的框架如图所示:
2.LoginRegisterInfo.java里面的代码:
package com.imooc.dao;
import java.util.List;
import javax.swing.JOptionPane;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.imooc.entity.User;
import com.imooc.util.HibernateSessionFactory;
public class LoginRegisterInfo {
private Session session;
private Transaction transaction;
private Query query;
private HibernateSessionFactory getSession;
public LoginRegisterInfo() {
}
public String saveInfo(User user) {
String mess = "error";
getSession = new HibernateSessionFactory();
session = HibernateSessionFactory.getSession();
try {
transaction = session.beginTransaction();
session.save(user);
transaction.commit();
mess = "success";
return mess;
} catch (Exception e) {
message("RegisterInfo error:" + e);
e.printStackTrace();
return null;
}
}
public List queryInfo(String type, Object value) {
getSession = new HibernateSessionFactory();
session = HibernateSessionFactory.getSession();
try {
String hqlsql = "from com.imooc.entity.User as u where u.username=?";
query = session.createQuery(hqlsql);
query.setParameter(0, value);
List list = query.list();
transaction = session.beginTransaction();
transaction.commit();
return list;
} catch (Exception e) {
message("LoginRegisterInfo error:" + e);
e.printStackTrace();
return null;
}
}
private void message(String mess) {
int type = JOptionPane.YES_NO_OPTION;
String title = "提示信息";
JOptionPane.showMessageDialog(null, mess, title, type);
}
}
package com.imooc.entity;
import java.io.Serializable;
public class User implements Serializable {
private Integer uid;
private String username;
private String password;
private String nickname;
private String email;
private Integer state;
private String code;
public User() {
}
public User(String username, String password, String nickname, String email, Integer state, String code) {
this.username = username;
this.password = password;
this.nickname = nickname;
this.email = email;
this.state = state;
this.code = code;
}
public Integer getUid() {
return this.uid;
}
public void setUid(Integer uid) {
this.uid = uid;
}
public String getUsername() {
return this.username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return this.password;
}
public void setPassword(String password) {
this.password = password;
}
public String getNickname() {
return this.nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
public String getEmail() {
return this.email;
}
public void setEmail(String email) {
this.email = email;
}
public Integer getState() {
return this.state;
}
public void setState(Integer state) {
this.state = state;
}
public String getCode() {
return this.code;
}
public void setCode(String code) {
this.code = code;
}
}
package com.imooc.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
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 com.imooc.dao.LoginRegisterInfo;
import com.imooc.entity.User;
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
String username = request.getParameter("username");
String password = request.getParameter("password");
User user = new User();
LoginRegisterInfo info = new LoginRegisterInfo();
if (username == null || username.length() == 0) {
out.print("");
} else {
List list = info.queryInfo("username", username);
for (int i = 0; i < list.size(); i++) {
user = list.get(i);
if (user.getUsername().equals(username)) {
if (user.getPassword().equals(password)) {
out.print("登录成功!");
return;
} else {
out.print("");
}
}
}
out.print("");
}
}
}
package com.imooc.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
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 com.imooc.dao.LoginRegisterInfo;
import com.imooc.entity.User;
@WebServlet("/RegistServlet")
public class RegistServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
String username = request.getParameter("username");
String password = request.getParameter("password");
String repassword = request.getParameter("repassword");
User user = new User();
LoginRegisterInfo info = new LoginRegisterInfo();
if (username == null || username.length() == 0) {
out.print("");
} else {
List list = info.queryInfo("username", username);
for (int i = 0; i < list.size(); i++) {
user = list.get(i);
if (user.getUsername().equals(username)) {
out.print("");
}
}
}
if (password == null || password.length() == 0) {
out.print("");
} else if (!password.equals(repassword)) {
out.print("");
}
user.setUsername(username);
user.setPassword(password);
info.saveInfo(user);
out.print("注册成功!");
}
}
package com.imooc.util;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
public class HibernateSessionFactory {
private static final ThreadLocal threadLocal = new ThreadLocal();
private static org.hibernate.SessionFactory sessionFactory;
private static Configuration configuration = new Configuration();
private static ServiceRegistry serviceRegistry;
static {
try {
configuration.configure();
serviceRegistry = new StandardServiceRegistryBuilder().configure().build();
try {
sessionFactory = new MetadataSources(serviceRegistry).buildMetadata().buildSessionFactory();
} catch (Exception e) {
StandardServiceRegistryBuilder.destroy(serviceRegistry);
e.printStackTrace();
}
} catch (Exception e) {
System.err.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
public HibernateSessionFactory() {
}
public static Session getSession() throws HibernateException {
Session session = (Session) threadLocal.get();
if (session == null || !session.isOpen()) {
if (sessionFactory == null) {
rebuildSessionFactory();
}
session = (sessionFactory != null) ? sessionFactory.openSession() : null;
threadLocal.set(session);
}
return session;
}
public static void rebuildSessionFactory() {
try {
configuration.configure();
serviceRegistry = new StandardServiceRegistryBuilder().configure().build();
try {
sessionFactory = new MetadataSources(serviceRegistry).buildMetadata().buildSessionFactory();
} catch (Exception e) {
StandardServiceRegistryBuilder.destroy(serviceRegistry);
e.printStackTrace();
}
} catch (Exception e) {
System.err.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
public static void closeSession() throws HibernateException {
Session session = (Session) threadLocal.get();
threadLocal.set(null);
if (session != null) {
session.close();
}
}
public static org.hibernate.SessionFactory getSessionFactory() {
return sessionFactory;
}
public static Configuration getConfiguration() {
return configuration;
}
}
update
MySQL
org.hibernate.dialect.MySQLDialect
root
root
jdbc:mysql://localhost:3306/hibernate
com.mysql.jdbc.Driver
true
true
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
My JSP 'index.jsp' starting page
登 录
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
My JSP 'index.jsp' starting page
注 册