jsp向数据库传递中文参数(变量)乱码问题的解决:
来源:互联网 发布:纳粹十字勋章淘宝 编辑:程序博客网 时间:2024/06/06 01:45
jsp向数据库传递中文参数乱码问题的解决:
1,JSP页面中文显示乱码解决
估计大家都知道怎么解决,但是我还是想说明一下:
1)、JSP编码格式选择: <%@ page language="java" contentType="text/html; charset=gb2312" //pageEncoding="utf-8"
%>
pageEncoding=”utf-8”这个页面编码格式可加可不加
但是中文显示,一定要下面加上这段代码,就放在开头或者随便哪里位置都行:我在下面的示例程序里面是写在了一个.JSP页面里的,然后就不用每次添加代码,只要把这段处理中文字符的代码,添加进来就行:
<%@ include file="codeToString1.jsp" %>
codeToString1.jsp里的代码为:
<%!public String codeToString(String str){ //处理中文字符串的函数 String s=str; try{ byte tempByte[]=s.getBytes("ISO-8859-1"); s=new String(tempByte); return s; }catch(Exception e){ return s; }}%>
2,传参到数据库中文乱码解决
在JSP页面前端加上下面这行代码就行:(跳转前的页面,相当于服务器响应浏览器的编码风格)
<%request.setCharacterEncoding("gb2312"); //response.setContentType("text/html;charset=gb2312");pageEncoding="utf-8"%>
3、创建数据库(贴出我的数据库,新手可以直接在navicat里面创建一模一样的table)
!1创建数据库usertable
4、运行结果
!1注册程序
!2点击提交后结果显示,中文可以显示
!3处理之前的乱码图片
!4结果显示
5、完整源代码
注意:需要把上面的codeToString1.jsp包含进来,或者直接在开头写代码。
1、test1.jsp(注册程序块)
<%@ page language="java" contentType="text/html; charset=gb2312"%><%response.setContentType("text/html;charset=gb2312"); %><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><script type="text/javascript"> function on_submit() { debugger;//这是调试用的,可以注释掉 if(form1.username.value=="") { alert("用户名不能为空,请输入用户名!"); form1.username.focus(); return false; } if(form1.password.value=="") { alert("密码不能为空,请输入密码!"); form1.userpassword.focus(); return false; } if(form1.repassword.value=="") { alert("确认密码不能为空,请输入密码!"); form1.reuserpassword.focus(); return false; } if(form1.password.value!=form1.repassword.value) { alert("密码与确认密码不同."); form1.userpassword.focus(); return false; } }</script><html><head><title>用户注册程序</title></head><body><center> <table border=1 width=700> <tr> <td width=200 colspan=2 align=center>用户注册程序</td> </tr> <form name="form1" action="test2response.jsp" method="post" onsubmit="return on_submit()"> <tr> <td width=125>用户名:</td> <td width=75><input type=text name=username>(*)必填</td> </tr> <tr> <td>密码:</td> <td><input type=password name=password>(*)必填</td> </tr> <tr> <td>请再输入一次密码:</td> <td><input type=password name=repassword>(*)必填</td> </tr> <tr> <td>真实姓名:</td> <td><input type=text name=usertruename></td> </tr> <tr> <td>年龄:</td> <td><input type=text name=age></td> </tr> <tr> <td>性别:</td> <td><input type=radio name=sex value="男" checked>男 <input type=radio name=sex value="女" >女</td> </tr> <tr> <td>联系地址:</td> <td><input type=text name=address size="60"></td> </tr> <tr> <td>联系电话:</td> <td><input type=text name=telephone></td> </tr> <tr> <td colspan=2 align=center> <input type=submit value=提交> <input type=reset value=重输></td> </tr> </form> </table></center></body></html>
2、test2response.jsp (test1.jsp提交后跳转)
<%@ page language="java" contentType="text/html; charset=gb2312" %><%request.setCharacterEncoding("gb2312");//这行代码不加好像也不会乱码%><%@ page import="java.sql.*" %><%@ include file="codeToString1.jsp" %><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><% String username=codeToString(request.getParameter("username")); if(username==null)//无内容则设为空串 username=""; else out.print("username="+username+" "); String password=codeToString(request.getParameter("password")); if(password==null)//无内容则设为空串 password=""; out.print("password="+password); String usertruename=codeToString(request.getParameter("usertruename")); if(usertruename==null)//无内容则设为空串 usertruename=""; String age=codeToString(request.getParameter("age")); int ageint; try{ ageint=Integer.parseInt(age.trim()); }catch(Exception e){ ageint=0; } String sex=codeToString(request.getParameter("sex")); if(sex==null)//无内容则设为空串 sex=""; String address=codeToString(request.getParameter("address")); if(address==null)//无内容则设为空串 address=""; String telephone=codeToString(request.getParameter("telephone")); if(telephone==null)//无内容则设为空串 telephone="";%><% String sqlString=null; sqlString="insert into usertable1 (user_name,user_password,user_true_name," +"user_age,user_sex,user_address,user_telephone)"+ "values ('"+username+"','"+password+"','"+usertruename+"','"+ ageint+"','"+sex+"','"+address+"','"+telephone+"')";%><% //try{ Connection con; Statement stmt; //ResultSet rs; Class.forName("com.mysql.jdbc.Driver"); //我使用的是MYSQL数据库con=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/select_user_table","root","123456"); stmt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY); stmt.executeUpdate(sqlString); con.close(); stmt.close(); //}catch(SQLException e1){ //out.print("SQL Exception."); //}%><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>程序</title></head><body> <center> <table border=1 width=700> <tr> <td width=200 colspan=2 align=center>用户注册程序</td> </tr> <tr> <td width=200 colspan=2>追加用户成功!</td> </tr> </table> </center></body></html>
参考资料:Java Web轻量级开发全体验 邓子云/著
0 0
- jsp向数据库传递中文参数(变量)乱码问题的解决:
- JSP解决链接传递中文参数乱码的问题
- 解决jsp参数传递中文乱码的问题
- 解决javascript通过url向Jsp页面传递中文参数乱码问题
- jsp通过URL向页面传递中文参数乱码问题
- 解决JSP参数传递乱码的问题
- 解决JSP参数传递乱码的问题
- 解决jsp参数传递乱码的问题
- 解决jsp参数传递乱码的问题
- 解决jsp参数传递乱码的问题
- 解决jsp参数传递乱码的问题
- 解决jsp参数传递乱码的问题
- JS通过get、post向jsp传递中文出现乱码的问题的解决
- JSP传递参数至另一个JSP页面时中文乱码问题的解决
- 关于jsp页面传递中文参数乱码的解决
- 关于jsp页面传递中文参数乱码的解决
- jsp页面传递中文参数乱码的解决
- 关于jsp页面传递中文参数乱码的解决
- plsql客户端连接远程和本地数据库
- em与px的区别
- 在GitHub上创建博客主页
- BZOJ1009 单模板自动机 矩阵快速幂优化DP
- 记录nginx查看启用命令
- jsp向数据库传递中文参数(变量)乱码问题的解决:
- Android开发之EditText属性详解
- android 网络获取字符串
- CSS3文本溢出显示省略号
- rabbitmq
- oracle条件分支用法 oracle 之if..else用法
- bash功能 作业控制技巧
- 随笔
- ubuntu定时任务