javaweb loginSystem 总结

包的结构:dao,dao.impl,service,service.impl,util,test,web.controller,web.formbean,web.UI 

用到的东西: 

hibernate3.6,jstl,beanutils(其中jstl和beanutils貌似eclipse自带) 

流程: 

图像

几个要点: 

    1. web.UI包存在的原因是为了保护jsp,将jsp放到WEB-INF目录下,不能直接访问,这时用servlet进行跳转 

    2. 登录成功后进行重定向到首页,因此将用户放到session域中,而在注销的时候getSession(false),且不要忘了删除 

    3. 等待几秒刷新使用http头进行模拟 <meta http-equiv="refresh" content="3; url=xxx"> 

    4. 在主页使用jstl的c:if标签进行判断,如果有用户,显示hello,否则显示登录或注册提示,判断为空用empty 

    5. 验证写在formbean里,错误放到formbean的一个hashmap里,并且出错后放到request里的是整个formbean 

 

一些代码类似于模版代码: 

  1. HibernateUtil
    public class HibernateUtil { 
    
    private static final SessionFactory sessionFactory=buildSessionFactory(); 
    
    private static SessionFactory buildSessionFactory(){ 
    
    try{ 
    
    return new Configuration().configure().buildSessionFactory(); 
    
    }catch(Throwable ex){ 
    
    System.err.println("Initial SessionFactory creation failed." + ex); 
    
                throw new ExceptionInInitializerError(ex); 
    
    } 
    
    } 
    
    public static SessionFactory getSessionFactory(){ 
    
    return sessionFactory; 
    
    } 
    
    } 
  2. md5加密
        public static String md5(String src){
            try {
                MessageDigest md=MessageDigest.getInstance("md5");
                byte[] md5=md.digest(src.getBytes());
                BASE64Encoder encoder=new BASE64Encoder();
                return encoder.encode(md5);
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            }
        }
  3. request2bean,copybean
    public static <T> T request2bean(HttpServletRequest request,Class<T> type){
            Enumeration names=request.getParameterNames();
            T bean;
            try {
                bean = type.newInstance();
            } catch (Exception e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            } 
            HashMap map=new HashMap();
            while(names.hasMoreElements()){
                String name=(String) names.nextElement();
                map.put(name, request.getParameter(name));
            }
            try {
                BeanUtils.populate(bean, map);
                return bean;
            } catch (Exception e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            }
                
        }
        
        public static void copyBean(Object dest,Object src){
            //注册自定义对象转换类
            ConvertUtils.register(new Converter(){
    
                @Override
                public Object convert(Class type, Object value) {
                    SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");
                    String dateStr=(String) value;
                    if(!StringUtils.isNullOrEmpty(dateStr)){
                        try {
                            return df.parse(dateStr);
                        } catch (ParseException e) {
                            e.printStackTrace();
                        }
                    }
                    return null;
                }
                
            }, Date.class);
            
            try {
                BeanUtils.copyProperties(dest, src);
            } catch (Exception e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            } 
            
        }
  4. hibernate的save
        public void addUser(User user) {
            Session session=null;
            Transaction ts=null;
            try {
                session = sf.openSession();
                ts = session.beginTransaction();
                ts.begin();
                session.save(user);
                Query query=session.createQuery("from User where username=? and password=?");
        query.setParameter(0, username);
        query.setParameter(1, password);
        List<User> result=query.list();

    ts.commit(); }
    catch (Exception e) { if(ts!=null){ ts.rollback(); } e.printStackTrace(); throw new RuntimeException("failed to add user."); }finally{ if(session!=null && session.isOpen()){ session.close(); } } }
  5. hibernate配置文件和映射文件
    <?xml version='1.0' encoding='utf-8'?>
    <!DOCTYPE hibernate-configuration PUBLIC
            "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
            "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
            
    <hibernate-configuration>
    
        <session-factory>
        
            <!-- Database connection settings -->
            <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
            <property name="connection.url">jdbc:mysql://localhost:3306/javaweb</property>
            <property name="connection.username">root</property>
            <property name="connection.password">123456</property>
            
            <!-- JDBC connection pool (use the built-in) -->
            <property name="connection.pool_size">1</property>
    
            <!-- SQL dialect -->
            <property name="dialect">org.hibernate.dialect.HSQLDialect</property>
    
            <!-- Enable Hibernate's automatic session context management -->
            <property name="current_session_context_class">thread</property>
    
            <!-- Disable the second-level cache  -->
            <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
    
            <!-- Echo all executed SQL to stdout -->
            <property name="show_sql">true</property>
    
            <!-- Drop and re-create the database schema on startup -->
            <property name="hbm2ddl.auto">create</property>
            
            <mapping resource="com/libin/domain/User.hbm.xml"/>
        </session-factory>
        
    </hibernate-configuration>
    <?xml version='1.0' encoding='utf-8'?>
    <!DOCTYPE hibernate-mapping PUBLIC
            "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
            "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
    
    <hibernate-mapping package="com.libin.domain">
    
        <class name="User" table="user">
        
            <id name="id" column="user_id">
                <generator class="uuid" />
            </id>
            
            <property name="username">
                <column name="username" not-null="true" />
            </property>
            
            <property name="password">
                <column name="password" not-null="true" />
            </property>
            
            <property name="email">
                <column name="email" length="30" not-null="true" />
            </property>
            
            <property name="birthday" type="date" />
            
        </class>
    
    </hibernate-mapping>

     

posted @ 2013-03-01 16:46  cubika  阅读(302)  评论(0编辑  收藏  举报