您的位置:首页 > Web前端 > JavaScript

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

2008-01-23 21:03 891 查看
用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编码。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: