Create a Hibernate Web login project using MyEclipse2017
Implement JavaBean/JDBC system manually
Hibernate automatically generates the required JavaBeans and replaces the functions of the original JDBC! It can be easily understood as: Hibernate=JavaBean+JDBC
1. Create a Java EE Web project and name the project HibernateDemo. Create two packages under the project src: org.easybooks.test.factory and org.easybooks.test.model.vo (the package names are arbitrary, as long as you remember the contents of each package clearly later)
After configuring the environment, click next
Check web.xml and click Finish.
2. Add Hibernate framework
The steps to add the Hibernate framework to the project are as follows.
① Right-click the project, select the menu [Configure Facets…] → [Install Hibernate Facet], start the [Install Hibernate Facet] wizard dialog box, and select the desired configuration from the drop-down list on the right side of the “Hibernate specification version” column on the “Project Configuration” page. Hibernate version added to the project. In order to maximize the use of the Hibernate tools integrated with MyEclipse 2017, select the latest version of Hibernate 5.1 and click the [Next] button.
② On the first “Hibernate Support for MyEclipse” page, create the Hibernate configuration file and SessionFactory class.
③ Click the [Next] button to enter the second “Hibernate Support for MyEclipse” page. Configure the details of the database connection used by Hibernate on this page. Because I have already created a connection named sqlsrv before, I only need to select “sqlsrv” in the “DB Driver” column, and the system will automatically load the contents of other columns. If you don’t know how to use myeclipse to connect to a remote database, you can read this article about remote database connection. You can also change the database you want to use. This article uses a SQL server database.
④ Click the [Next] button and select the Hibernate framework class library to be added to the project on the “Configure Project Libraries” page. For general applications, you do not need to use all Hibernate class libraries, so you only need to select the necessary ones. Just add the library. Here only check the most basic core library “Hibernate 5.1 Libraries” → “Core”, as shown in the figure.
Click the [Finish] button to open the perspective. The editor for the Hibernate configuration file “hibernate.cfg.xml” appears in the center of the main interface of the development environment. You can see the various configurations of Hibernate in this example on its “Configuration” option tab. information, as shown in the figure.
After completing the above steps, a Hibernate package directory, a hibernate.cfg.xml configuration file and a HibernateSessionFactory.java class were added to the project. In addition, the driver package of the database is also automatically loaded. At this time, the directory tree of the project appears as shown in the figure, indicating that the Hibernate framework has been successfully added to the project.
3. Generate persistent objects for the userTable table
The operation of generating persistent Java objects for tables in the database (in this case, the userTable table) is also called “reverse engineering”, and it is a necessary prerequisite for using the functionality of the Hibernate framework. The org.easybooks.test.model.vo package previously created under project src is used to store POJO classes and mapping files corresponding to the database userTable table.
The steps for reverse engineering are as follows.
① Select the main menu [Window] → [Perspective] → [Open Perspective] → [Database Explorer] to enter the DB Browser mode of MyEclipse. Open the sqlsrv connection, select the database table userTable, right-click, and select the menu [Hibernate Reverse Engineering…]. The [Hibernate Reverse Engineering] wizard dialog box will be launched to complete the generation of the corresponding POJO class and related mapping files from the existing database table. Configuration work.
② In the first “Hibernate Mapping and Application Generation” page of the wizard, select the location of the generated class and mapping files, as shown in the figure.
③ Click the [Next] button to enter the second “Hibernate Mapping and Application Generation” page and configure the details of the mapping file, as shown in the figure.
④ Click the [Next] button to enter the third “Hibernate Mapping and Application Generation” page. This page is mainly used to configure the details of reverse engineering. Just keep the default configuration here, as shown in the figure.
4. Create JSP file
This example has 4 JSP files, login.jsp (login page), main.jsp (welcome home page), error.jsp (error page), validate.jsp (validation page), which are similar to the previous example of the model2 mode Web login program Instead, use the Hibernate framework to access the database in an object-oriented manner.
login.jsp:
<%@ page language="java" pageEncoding="gb2312"%> <html> <head> <title>Simple Message Board</title> </head> <body bgcolor="#E3E3E3"> <form action="validate.jsp" method="post"> <table> <caption>User login</caption> <tr> <td>Username:</td> <td> <input type="text" name="username" size="20"/> </td> </tr> <tr> <td>Password:</td> <td> <input type="password" name="password" size="21"/> </td> </tr> </table> <input type="submit" value="Login"/> <input type="reset" value="Reset"/> </form> If you have not registered, click <a href="">here</a> to register! </body> </html>
main.jsp:
<%@ page language="java" pageEncoding="gb2312" import="org.easybooks.test.model.vo.UserTable"%> <html> <head> <title>Message board information</title> </head> <body> <% UserTable user=(UserTable)session.getAttribute("user"); String usr=user.getUsername(); %> Hello <%=usr%>! Welcome to the message board. </body> </html>
validate.jsp:
<%@ page language="java" pageEncoding="gb2312" import="org.easybooks.test.factory.*,org.hibernate.*,java.util.*,org.easybooks.test.model. vo.UserTable"%> <html> <head> <meta http-equiv="Content-Type" content="text/html;charset=gb2312"> </head> <body> <% request.setCharacterEncoding("gb2312"); //Set request encoding String usr=request.getParameter("username"); //Get the submitted username String pwd=request.getParameter("password"); //Get the submitted password boolean validated=false; //Verification success indicator UserTable user=null; //Get the UserTable object first. If it is the first time to access the page, the user object will definitely be empty. But if it is the second or even the third time, log in directly to the home page without re-verifying the user's information. user=(UserTable)session.getAttribute("user"); //If the user enters for the first time, the user persistent object has not been stored in the session, so it is null. if(user==null){<!-- --> //Query the records in the userTable table String hql="from UserTable u where u.username=? and u.password=?"; Query query=HibernateSessionFactory.getSession().createQuery(hql); query.setParameter(0, usr); query.setParameter(1, pwd); List users=query.list(); Iterator it=users.iterator(); while(it.hasNext()) {<!-- --> if(users.size()!=0){<!-- --> user=(UserTable)it.next(); //Create a persistent JavaBean object user session.setAttribute("user", user); //Store the user object in the session validated=true; //Marked as true, it means the verification is successful. } } } else{<!-- --> validated=true; //The user has logged in and been successfully verified before, so the mark is true, which means no further verification is required. } if(validated) {<!-- --> //Verify successfully jump to main.jsp %> <jsp:forward page="main.jsp"/> <% } else {<!-- --> //Verification failure jumps to error.jsp %> <jsp:forward page="error.jsp"/> <% } %> </body> </html>
error.jsp:
<%@ page language="java" pageEncoding="gb2312"%> <html> <head> <title>Error</title> </head> <body> \tLogin failed! Click <a href="login.jsp">here</a> to return </body> </html>