问题描述
- 求 解 关于servlet 中用hibernate 出错
-
package cn.cui.database;import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;public class HibernateUtil {
private static SessionFactory factory;
static {//
Configuration cfg = new Configuration();
cfg.configure("/hibernate.cfg-mysql.xml");
factory = cfg.buildSessionFactory();
}public static Session openSession() { return factory.openSession(); }
}
package cn.cui.servlet;
import java.io.IOException;
import java.io.PrintWriter;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import cn.cui.database.HibernateUtil;
import com.google.gson.Gson;
/**
- Servlet implementation class PositionServlet
*/
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;private String result;
public LoginServlet() {
super();
// TODO Auto-generated constructor stub
}@Override
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}@Override
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
String name = request.getParameter("n");
String group = request.getParameter("g");
String pwd = request.getParameter("p");
System.out.println(name);result = selectUserInfo(name, group, pwd); PrintWriter out = response.getWriter(); // Gson gson = new Gson(); String jsonString = gson.toJson(result); // 用Gson方式 把object 保存为json字符 out.write(jsonString); out.flush(); out.close();
}
private String selectUserInfo(String name, String group, String pwd) {
// 查询
org.hibernate.Session ssn = HibernateUtil.openSession();
// String hql = "from GroupInfo where group=:a";
Query q = ssn.createQuery(hql);
q.setString("a", group);
List list = q.list();
for (GroupInfo s : list) {
String password = s.getPassword();
}
}
}
访问:http://localhost:8080/Server/login?n=name&g=group&p=pwd
错误:严重: Servlet.service() for servlet [LoginServlet] in context with path [/Server] threw exception [Servlet execution threw an exception] with root cause
java.lang.ClassNotFoundException: org.hibernate.HibernateException
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1720)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1571)
at cn.cui.servlet.LoginServlet.selectUserInfo(LoginServlet.java:64)
at cn.cui.servlet.LoginServlet.doPost(LoginServlet.java:46)
at cn.cui.servlet.LoginServlet.doGet(LoginServlet.java:31)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:313)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722) - Servlet implementation class PositionServlet