这个代码是当我输入一个字,就显示一个字,为什么我输入英文就能显示,一旦我输入中文,例如"我"字,只显示??,而不显示我字呢?我知道中文是两个字节的,所以有两个?,但我想显示的是中文啊.不是乱码!
下面我也设置了gb2312,为什么还不显示中文呢?谢谢
代码如下: <%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<%@ page import="java.util.*"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>测试</title>
</head> <body>
<form name="form1" method="post" action="instring.jsp">
<input name="jcs" type="text" id="text">
<input type="submit" name="Submit" value="提交">
</form>
<%!String s=null;%>
<%
s=request.getParameter("jcs");
if(s==null)
{
out.println("你输入空的字符串");
}
else
{
out.println("你输入的是"+s);
}
%> </body>
</html>
下面我也设置了gb2312,为什么还不显示中文呢?谢谢
代码如下: <%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<%@ page import="java.util.*"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>测试</title>
</head> <body>
<form name="form1" method="post" action="instring.jsp">
<input name="jcs" type="text" id="text">
<input type="submit" name="Submit" value="提交">
</form>
<%!String s=null;%>
<%
s=request.getParameter("jcs");
if(s==null)
{
out.println("你输入空的字符串");
}
else
{
out.println("你输入的是"+s);
}
%> </body>
</html>
如果是繁体的话就常常出现这种现象
只要把contentType="text/html; charset=gb2312"
取消就可以了
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<%@ page import="java.util.*"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>测试</title>
</head> <body>
<%!public String getStr(String str)
{
try
{
String temp_p=str;
byte[] temp_t=temp_p.getBytes("ISO-8859-1");
String temp=new String(temp_t);
return temp;
}
catch(Exception e)
{
e.printStackTrace();
}
return "null";
}
%>
<form name="form1" method="post" action="test.jsp">
<input name="jcs" type="text" id="text">
<input type="submit" name="Submit" value="提交">
</form>
<%
String s=getStr(request.getParameter("jcs"));
if(s==null)
{
out.println("你输入空的字符串");
}
else
{
out.println("你输入的是"+s);
}
%> </body>
</html>
请转
if(s != null)
s = new String(s.getBytes("ISO8859-1"),"GB2312");
但建议全部用utf-8 ,如
<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>
<%@ page import="java.util.*"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>测试</title>
</head><body>
<form name="form1" method="post" action="instring.jsp">
<input name="jcs" type="text" id="text">
<input type="submit" name="Submit" value="提交">
</form>
<%!String s=null;%>
<%
request.setCharacterEncoding("utf-8");
s=request.getParameter("jcs");
if(s==null)
{
out.println("你输入空的字符串");
}
else
{
out.println("你输入的是"+s);
}
%></body>
</html>