中文乱码问题
百度了很多方法,还是没解决,求大神帮忙。<%@page contentType="text/html;charset=GBK"%>
<%@page import="java.sql.*"%>
<jsp:useBean id="java" scope="page" class="vote.DBConnect"/>
<%
String question=request.getParameter("question");
if(question==null){
question ="";
}
String type=request.getParameter("type");
int num=0,optType=0;
try{
num=Integer.parseInt(request.getParameter("num"));
}
catch(Exception e){}
%>
<html>
<head>
<title>增加主题</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<link href="vote.css" rel="stylesheet" type="text/css">
</head>
<body>
<center>
<h3>增加主题</h3>
<hr>
<a href="add.jsp">增加主题</a>||<a href="del.jsp">删除主题</a>|| <a href="display.jsp">查看主题</a>
|| <a href="modify.jsp">修改主题</a> ||<a href="index.jsp">返回首页</a>
<hr>
<form action="add.jsp" method="post">
请输入主题名:<br>
<input type="text" name="question" size="50" value="<%=question%>"><p>
<input type="checkbox" value="T" name="type"
<%
if(type!=null){
out.print("checked");
optType=1;
}
else{
optType=0;
}
%>
>多选 请输入选项的数目:
<input typ="text" name="num" size="5" value="<%=num%>">
<input type="submit" name="sumbit" value="创建选项">
<p>
<%
String submit=request.getParameter("sumbit");
if(submit!=null){
for(int i=1;i<=num;i++){
%>
选项<%=i%>:
<input type="text" name="<%=i%>" value="" size="50">
<br>
<%}%>
<input type="submit" name="submit1" value="增加主题">
<%}%>
</form>
<%
String submit1=request.getParameter("submit1"),optContent,sql;
String str="";
int id;
if(submit1!=null){
sql="insert into vote(flag,optType,question) values(0,'"+optType+"','"+question+"')";
java.executeUpdate(sql);
sql="select * from vote order by id desc";
ResultSet rs=java.executeQuery(sql);
rs.next();
id=rs.getInt(1);
for(int i=1;i<=num;i++){
str=Integer.toString(i);
optContent=request.getParameter("i");
sql="insert into vote(flag,optContent,number) values('"+id+"','"+optContent+"',0)";
java.executeUpdate(sql);
}
out.print("成功插入主题!");
}
%>
</center>
</body>
</html> --------------------编程问答-------------------- 楼主这样在jsp页面里面写代码的方式
不太正统
像这样的话,你就用这种方式解决乱码问题吧,比如
String s2 = new String(s1.getBytes(“ISO-8859-1”),”GBK”);(在你这把s1换成question)
另外你得保证你得数据库中出来的是中文哦 --------------------编程问答-------------------- 乱码问题不是盲目的乱试一通,首先要确定是哪一步开始乱码的。 --------------------编程问答-------------------- 后台传过来的时候打印一下,看看是不是乱码 --------------------编程问答-------------------- 然后插入数据库那里把question改成s2吗 --------------------编程问答-------------------- 应该是在获取页面输入的中文的时候乱码的String question=request.getParameter("question");和optContent=request.getParameter("i");应该是这两个地方,可是直接在这两个地方转码会错误打不开页面 --------------------编程问答--------------------
在创建选项的时候就乱码了 --------------------编程问答--------------------
是的,转码就用这段代码就行了 --------------------编程问答--------------------
楼主这样在jsp页面里面写代码的方式
不太正统
像这样的话,你就用这种方式解决乱码问题吧,比如
String s2 = new String(s1.getBytes(“ISO-8859-1”),”GBK”);(在你这把s1换成question)
这样改网页打不开了
错误提示:HTTP Status 500 -
--------------------------------------------------------------------------------
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: An exception occurred processing JSP page /add.jsp at line 6
3: <jsp:useBean id="java" scope="page" class="vote.DBConnect"/>
4: <%
5: String question=request.getParameter("question");
6: String s2 = new String(question.getBytes("ISO-8859-1"),"GBK");
7: if(question==null){
8: question ="";
9: }
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:524)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:435)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
root cause
java.lang.NullPointerException
org.apache.jsp.add_jsp._jspService(add_jsp.java:68)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:393)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
note The full stack trace of the root cause is available in the Apache Tomcat/6.0.13 logs.
--------------------------------------------------------------------------------
Apache Tomcat/6.0.13
--------------------编程问答-------------------- 你数据库用mysql的话要跟这里的编码一致,这个乱码我也遇过 --------------------编程问答-------------------- 先检查你的数据第一次乱码出现在什么时候补充:Java , Web 开发