关于hibernate和jsp,以及中文编码问题的传统解决方法

本文介绍如何使用MyEclipse集成开发环境结合Hibernate框架进行Java Web应用程序的开发,包括配置环境、创建实体类及实现数据库操作的具体步骤。
用MyEclipse来开发java网站还是很方便的 。
要实现一个应用hibernate来进行数据库操作的web程序,首先要在工程中添加hibernate的包,这个在MyEclipse中一个菜单命令就可以搞定了。
其次我们要建立相关数据库中某个表的hbm.xml,这个也是在MyEclipse中搞定。
再建立针对这个表的java类。
然后建立如下网页:
<%@ page language="java" pageEncoding="GB2312"%>
<%@page contentType="text/html;charset=gb2312" %>
<%@ page import="java.util.*" %>
<%@ page import="org.hibernate.cfg.Configuration" %>
<%@ page import="org.hibernate.SessionFactory" %>
<%@ page import="org.hibernate.Session" %>
<%@ page import="org.hibernate.Transaction" %>
<%
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> 
    
<%
        SessionFactory sf 
= new Configuration().configure().buildSessionFactory();
        Session sn 
= sf.openSession();
        Transaction tx 
= sn.beginTransaction();
        
        
if(request.getParameter("txtDetail"== null){
            out.println(
"Please input");
        }
else{
            hibernate.Detail detail 
= new hibernate.Detail();
            detail.setDate(
new Date());
            detail.setDetail(
new String(request.getParameter("txtDetail").getBytes("8859_1"),"GB2312"));
            detail.setSmallkind(
new String(request.getParameter("smallkind").getBytes("8859_1"),"GB2312"));
            
            try{
                sn.save(detail);
                tx.commit();
                
            }catch(Exception e){
                e.printStackTrace();
            }        
        }   
            sn.close();     
     
%>
     
<select name="combobox3">
<option>1</option>
</select>

     
<form method="get" action="index.jsp" name="Detail">
        
<input type="text" name="txtDetail"/> 
        
<select name="smallkind">
        
<% 
            sn
=sf.openSession();
            
            
String hql = "from SmallKind";
            List list 
= sn.createQuery(hql).list();
            Iterator it 
= list.iterator();
            
            
while(it.hasNext())
            {
                hibernate.SmallKind obj 
= (hibernate.SmallKind)it.next();
                out.println(
"<option>"+obj.getNameSmallKind()+"</option>");
            }
            
        
%>
        
</select>
         
<input type="Submit" name="detailSub" value="确定"/>    
         
    
</form>
    
<table>
        
<% 
            hql 
= "from Detail";
            list 
= sn.createQuery(hql).list();
            it 
= list.iterator();
            
            
while(it.hasNext())
            {
                hibernate.Detail obj1 
= (hibernate.Detail)it.next();
                out.println(
"<tr><td>"+obj1.getDetail()+"</td><td>"+obj1.getSmallkind()+"</td><td>"+obj1.getDate()+"</td></tr>");
            }
            sn.close();
        
%>
        
</table>
  
</body>
</html>
请注意detail.setDetail(new String(request.getParameter("txtDetail").getBytes("8859_1"),"GB2312"));这段代码。它的作用是保证传出的数据能够保持为GB2312编码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值