Java_Web三大框架之Hibernate+jsp+selvect+HQL登入验证

刚开始接触Hibernate有些举手无措,觉得配置信息太多。经过一个星期的适应,Hibernate比sql简单方便多了。下面做一下Hibernate+jsp+selvect+HQL登入验证。

第一步:编写用户实体类和配置用户Users2.hbm.xml映射。

/*
 * 用户实体类
 */
public class Users2 {
 private int id;//用户id
 private String name;//用户昵称
 private String password;//用户密码
 private String telephone;//用户号码
 private String username;//用户姓名
 private String isadmin;//是否是管理员

//省略get和set方法
}
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping
    package="entity">

    <class name="Users2" table="Users2">
        <id name="id">
            <generator class="increment"/>
        </id>
        <property name="name" />
        <property name="password" />
        <property name="telephone" />
        <property name="username" />
        <property name="isadmin"/>
    </class>

</hibernate-mapping>

第二步:配置hibernate.cfg.xml映射

<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory name="foo">
        <!-- 数据库方言 -->
        <property name="dialect">
            org.hibernate.dialect.OracleDialect
        </property>
        <!-- 连接数据库Url -->
        <property name="hibernate.connection.url">
            jdbc:oracle:thin:@localhost:1521:orcl
        </property>
        <!-- 连接驱动 -->
        <property name="hibernate.connection.driver_class">
            oracle.jdbc.driver.OracleDriver
        </property>
        <!-- 用户名 -->
        <property name="hibernate.connection.username">epet</property>
        <!-- 密码 -->
        <property name="hibernate.connection.password">123456</property>

                <!-- 在控制台打印sql信息 -->
        <property name="show_sql">true</property>
        <!-- 创建表结构 -->
        <property name="hibernate.hbm2ddl.auto">update</property>

        <!-- 配置映射信息 -->
        <mapping resource="entity/Users2.hbm.xml" />

    </session-factory>
</hibernate-configuration>

第三步:编写HibernateUtil帮助类,dao和daoImpl

package com.msit.hibernate.HibernateUtil;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {

    public HibernateUtil(){

    };

    public static SessionFactory SessionFactory = null;

    static{
        //hibernate
        Configuration cf = new Configuration();
        cf.configure();
        SessionFactory = cf.buildSessionFactory();//DriverManager.getconnection()
        //Session session = SessionFactory.openSession();/
    }

    public static Session getSession(){

        return SessionFactory.openSession();
    }

    public static void closeSession(Session session){
        if(session!=null){
            session.clear();
        }
    }

}
public interface UserDao {
    /*
     * 验证登入
     */
    public int isExist(String name,String password);
}
public class UserDaoImpl implements UserDao{

    public int isExist(String name, String password) {
             int i=0;
             //获取Session
         Session session = HibernateUtil.getSession();
         //开启事物
         Transaction Transaction=session.beginTransaction();
               //HQL语句
         Query query = session.createQuery("from Users2 u where u.username = ? and u.password = ?");
         query.setString(0, name);
         query.setString(1, password);
          List<Users2> list = query.list();
        if(list.size()>0){
            i=1;
        }else{
            i=0;
        }

            return i;
    }
}

第四步:编写UserSelvect和web_xml

package selvect;

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 Biz.Users2Biz;

import Biz.Impl.Users2BizImpl;

public class UserSelvect extends HttpServlet {

    /**
     * Constructor of the object.
     */
    public UserSelvect() {
        super();
    }

    /**
     * Destruction of the servlet. <br>
     */
    public void destroy() {
        super.destroy(); // Just puts "destroy" string in log
        // Put your code here
    }

    /**
     * The doGet method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to get.
     *
     * @param request the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException if an error occurred
     */
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }

    /**
     * The doPost method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to post.
     *
     * @param request the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException if an error occurred
     */
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        request.setCharacterEncoding("utf-8");
        String name=request.getParameter("name");
        String password=request.getParameter("password");
        Users2Biz biz=new Users2BizImpl();
        int i=biz.isExist(name, password);

        if(i>0){

            response.sendRedirect("index.jsp");
        }else{
            response.sendRedirect("login.jsp");

        }

    }
    /**
     * Initialization of the servlet. <br>
     *
     * @throws ServletException if an error occurs
     */
    public void init() throws ServletException {
        // Put your code here
    }

}
<?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>
    <servlet-name>UserSelvect</servlet-name>
    <servlet-class>selvect.UserSelvect</servlet-class>
    </servlet>

  <!-- 映射servlet -->
  <servlet-mapping>
      <servlet-name>UserSelvect</servlet-name>
      <url-pattern>/UserSelvect</url-pattern>
  </servlet-mapping>

  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

login.jsp页面

<H4>用户登录</H4>
<FORM  method="post" action="UserSelvect">
<DIV class=infos>
<TABLE class=field>
  <TBODY>
  <TR>
    <TD colSpan=2></TD></TR>
  <TR>
    <TD class=field>用 户 名:</TD>
    <TD><!-- <input type="text" class="text" name="name" /> --><INPUT
      id=user_name class=text type=text name=name> </TD></TR>
  <TR>
    <TD class=field>密  码:</TD>
    <TD><input type="password" class="text" name="password" />
</TD></TR>
</TBODY>
</TABLE>
<DIV class=buttons> <INPUT onclick=‘document.location="UserSelvect"‘ value=登陆 type="submit"> <INPUT onclick=‘document.location="regs.jsp"‘ value=注册 type=button>
</DIV></DIV></FORM>

成功页面:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">

    <title>My JSP ‘index.jsp‘ starting page</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
  </head>

  <body>
   登入成功。
  </body>
</html>
时间: 2024-08-24 23:24:16

Java_Web三大框架之Hibernate+jsp+selvect+HQL登入验证的相关文章

Java_Web三大框架之Hibernate+jsp+selvect+HQL注册用户

Hibernate比SQL语句简单多了,代码冗余少,切方便简洁明了.下面用Hibernate+jsp+selvect+HQL来实现注册用户. 第一步:编写用户实体类和Users2.hbm.xml映射. /* * 用户实体类 */ public class Users2 { private int id;//用户id private String name;//用户昵称 private String password;//用户密码 private String telephone;//用户号码 p

Java_Web三大框架之Hibernate+jsp+selvect+HQL查询数据

俗话说:"好记性不如烂笔头".本人学习Hibernate也有一个星期了,对Hibernate也有一个初步的了解.下面对Hibernate显示数据做个笔记,使用租房系统的Hibernate+jsp+selvect. 第一步:编写房屋实体类 /* * 房屋实体类 */ public class House { private int id;//房屋id private String title;//标题 private String description;//描述 private Str

Java_Web三大框架之Hibernate+jsp+HQL分页查询

分页查询无处不在.使用Hibernate+jsp+HQL进行分页查询. 第一步:编写房屋实体类和House.hbm.xml映射. /* * 房屋实体类 */ public class House { private int id;//房屋id private HouseType type;//房屋类型 private Users2 user;//用户 private Street street;//街道 private String title;//标题 private String descr

Java_Web三大框架之Hibernate+HQL语言基础

12.1 HQL语言基础Hibernate查询语言为HQL(Hibernate Query Language),可以直接使用实体类名及属性.HQL语法类似于SQL,有SQL的关键词如select.from.order by.count().where等等.不同的是HQL是一种完全面向对象的语言,能够直接查询实体类及属性.12.1.1 HQL语法HQL语法类似于SQL,是一种select...from...的结构.其中,from后跟的是实体类名,而不是表名.select后面跟的可以是实体对象,也可

Java_Web三大框架之Hibernate 入门(一)

一.Hibernate简介: Hibernate作者——Gavin King Hibernate创始人 < Hibernate in action >作者 EJB 3.0的Entity bean specification的实际领导人(sun任命的领导人是Linda DeMichiel) 参加了XDoclet和Middlegen的开发 2003年9月加入JBoss,全职进行Hibernate开发 二.Hibernate 1.一个开发源代码的对象关系映射框架 2.对JDBC进行了非常轻量级的对象

Java_Web三大框架之Hibernate配置文件(二)

下面介绍一下编写Hibernate的配置文件,使用Hibernate操作数据库. 开始部署:下载需要的jar包 下载Hibernate Hibernate 的官方主页是www.hibernate.org 推荐下载hibernate-distribution-3.3.2.GA-dist.zip Hibernate包目录结构 部署jar包 hibernate3.jar required 目录下的jar 包 Oracle 数据库驱动jar包 第一步:创建实体类和实体映射文件 public class

Java_Web三大框架之Hibernate操作数据库(三)

使用Hibernate操作数据库需要七个步骤: (1)读取并解析配置文件 Configuration conf = newConfiguration().configure(); (2)读取并解析映射信息,创建SessionFactory SessionFactory sf = conf.buildSessionFactory(); (3)打开Session Session session = sf.openSession(); (4)开始一个事务(增删改操作必须,查询操作可选) Transac

Java_Web三大框架之Hibernate增删改查

下面介绍一下Hibernate的增删改查. 第一步:编写用户实体类以及User.hbm.xml映射 package com.msl.entity; public class User { private int id; private String name; private String password; private String telephone; private String username; private String isadmin; public User() { } p

Java_Web三大框架之Struts2

今天正式接触Java_Web三大框架之Struts2框架.对于初学者来说,先来了解什么是框架技术: 一.“框架技术”帮我们更快更好地构建程序: 1.是一个应用程序的半成品 2.提供可重用的公共结构 3.按一定规则组织的一组组件 二.优势: 1.不用再考虑公共问题 2.专心在业务实现上 3.结构统一,易于学习.维护 4.新手也可写出好程序 介绍一下主流框架Struts 目前Struts框架有俩个版本,分别是Struts和Struts2,对于初学者来说,可能会认为Struts2是Struts的升级版