第三步、创建servlet。在src下面创建相应包并在包里面创建LoginServlet.java来作为servlet。LoginServlet的代码如下:
package com.qingfeilee.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.hibernate.Session; import org.hibernate.Transaction; import com.qingfeilee.bean.MyUser; import com.qingfeilee.demo.DemoSessionFactory; public class LoginServlet extends HttpServlet { public LoginServlet() { super(); } public void destroy() { super.destroy(); } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Session session = DemoSessionFactory.currentSession(); Transaction t = session.beginTransaction(); t.begin(); MyUser user = new MyUser(); user.setUserId("1"); user.setPassword("11"); user.setUserName("22"); session.save(user); t.commit(); DemoSessionFactory.closeSession(); } public void init() throws ServletException { } }
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <servlet> <description>This is the description of my J2EE component</description> <display-name>This is the display name of my J2EE component</display-name> <servlet-name>LoginServlet</servlet-name> <servlet-class>com.qingfeilee.servlet.LoginServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>LoginServlet</servlet-name> <url-pattern>/servlet/LoginServlet</url-pattern> </servlet-mapping> </web-app>
<%@ page language="java" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>演示struts2</title> </head> <body> <form action="servlet/LoginServlet" method = "post"> <table> <tr> <td> 用户名: </td> <td> <input type="text" name="userName" /> </td> </tr> <tr> <td> 密码: </td> <td> <input type="password" name="password" /> </td> </tr> <tr> <td colspan="2"> <input type="submit" value="提交"> <input type="reset" value="重置"> </td> </tr> </table> </form> </body> </html>
<?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="current_session_context_class">thread</property> <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property> <property name="connection.url">jdbc:oracle:thin:@192.168.1.121:1521:jcs</property> <property name="connection.username">jcs_jbpm</property> <property name="connection.password">jcs_jbpm</property> <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property> <property name="myeclipse.connection.profile">oracle</property> <mapping resource="com/qingfeilee/bean/MyUser.hbm.xml"/> </session-factory> </hibernate-configuration>
package com.qingfeilee.bean; public class MyUser implements java.io.Serializable { private String userId; private String userName; private String password; public String getUserId() { return this.userId; } public void setUserId(String userId) { this.userId = userId; } 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; } }
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.qingfeilee.bean.MyUser" table="MY_USER" schema="JCS_JBPM"> <id name="userId" type="java.lang.String"> <column name="USER_ID" length="20" /> <generator class="assigned" /> </id> <property name="userName" type="java.lang.String"> <column name="USER_NAME" length="20" /> </property> <property name="password" type="java.lang.String"> <column name="PASSWORD" length="20" /> </property> </class> </hibernate-mapping>
package com.qingfeilee.demo; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class DemoSessionFactory { public static final SessionFactory sessionFactory; static{ sessionFactory = new Configuration().configure().buildSessionFactory(); } public static final ThreadLocal<Session> localThread = new ThreadLocal<Session>(); /** * author:qingfeilee * date:2012-03-05 * description:获取当前session * **/ public static Session currentSession(){ Session s = (Session)localThread.get(); if(s == null){ s = sessionFactory.getCurrentSession(); localThread.set(s); } return s; } /** * author:qingfeilee * date:2012-03-05 * description:关闭当前session * **/ public static void closeSession(){ Session s = localThread.get(); localThread.set(null); if(s != null && s.isOpen()){ s.close(); } } }