关于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编码。
要实现一个应用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编码。
相关文章推荐
- 关于JSP中文问题的解决方法
- MySQL5.0中文问题及JSP汉字编码问题解决方法
- 关于上个Flex-Jsp-DB例子中Flex和Jsp传递中文参数问题的解决方法!(Tomcat服务器)
- Unity 中关于中文乱码以及压缩解压遇到的问题解决方法
- 关于JSP中文问题的解决方法
- 中文乱码--MySQL5.0中文问题及JDBC数据库连接和JSP汉字编码问题解决方法总
- 关于std::fstream以及std::ifstream打开中文路径名失败的问题和解决方法
- 关于jsp页面 title中文乱码问题的解决方法
- MySQL5.0中文问题及JDBC数据库连接和JSP汉字编码问题解决方法总结
- java和jsp中文编码问题的解决方法(ISO8859_1、GBK、UTF-8)
- 关于jsp页面中中文乱码问题的解决方法
- MySQL5.0中文问题及JDBC数据库连接和JSP汉字编码问题解决方法总结
- MySQL5.0中文问题及JDBC数据库连接和JSP汉字编码问题解决方法总结
- MySQL5.0中文问题及JDBC数据库连接和JSP汉字编码问题解决方法总结
- jsp+mysql文件上传下载方法比较(解决中文编码问题)
- JSP页面编码问题研究三(中文字符乱码问题的解决方法)
- (转)MySQL5.0中文问题及JDBC数据库连接和JSP汉字编码问题解决方法总结
- 关于JSP中文问题的解决方法
- 关于std::fstream以及std::ifstream打开中文路径名失败的问题和解决方法