我需要获取username,我的代码是:String username=new String(request.getParameter("username").getBytes("utf-8"));在数据库中添加成功后,我在数据库中看到的是乱码,System.out.println(username);在控制台打印的也是乱码。
请教各位,这是怎么回事啊?
11 个解决方案
#1
控制台打印乱码,数据库插入自然乱码。
先保证控制台打印不乱码,
试试看
String username=new String(request.getParameter("username").getBytes("ISO-8859-1"));
或者
String username=new String(request.getParameter("username").getBytes("ISO-8859-1"), "UTF-8");
先保证控制台打印不乱码,
试试看
String username=new String(request.getParameter("username").getBytes("ISO-8859-1"));
或者
String username=new String(request.getParameter("username").getBytes("ISO-8859-1"), "UTF-8");
#2
1楼的应该行,试试差不多。。。
#3
页面的编码是utf-8,String(request.getParameter("username").getBytes("utf-8"));乱码
String(request.getParameter("username").getBytes("GB2312"));乱码
String(request.getParameter("username").getBytes("iso-8859-1"))乱码
String(request.getParameter("username").getBytes("GB2312"));乱码
String(request.getParameter("username").getBytes("iso-8859-1"))乱码
#4
请再试一下
String username=new String(request.getParameter("username").getBytes("ISO-8859-1"), "UTF-8");
若不行,
请贴全部页面代码。
String username=new String(request.getParameter("username").getBytes("ISO-8859-1"), "UTF-8");
若不行,
请贴全部页面代码。
#5
我试过了,还是乱码,现贴部分代码如下:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@page import="function.Insert;"%>
<html>
<head>
<title>My JSP 'login_sp.jsp' starting page</title>
<style type="text/css">
<!--
.STYLE1 {
font-family: "华文新魏";
font-size: 36px;
font-weight: bold;
color: #990099;
}
.STYLE2 {
font-family: "华文新魏";
font-size: 24px;
font-weight: bold;
color: #990099;
}
-->
</style>
</head>
<%
if((request.getParameter("username")!=null)&&(request.getParameter("password")!=null))
{
String username=new String(request.getParameter("username").getBytes("utf-8"));
String password=new String(request.getParameter("password").getBytes("utf-8"));
Insert insert = new Insert();
if(insert.stu_insert_login(username,password))
{
response.sendRedirect("stu_query_login_user.jsp");
System.out.println(username+" "+password);
}
else
{
response.sendRedirect("stu_insert_login_failure.jsp");
}
}
%>
<body>
<div align="center"><span class="STYLE1">用户注册</span><br>
</div>
<form name="form1" method="post" action="">
<table width="337" border="6" align="center">
<tr>
<td width="120" height="40"><div align="center"><span class="STYLE2">用户名:</span></div></td>
<td width="191"><div align="center">
<input name="username" type="text" id="username">
</div></td>
</tr>
<tr>
<td height="40" class="STYLE2"><div align="center">密 码:</div></td>
<td><div align="center">
<input name="password" type="password" id="password">
</div></td>
</tr>
<tr>
<td height="40" colspan="2"><div align="center">
<input name="submit" type="submit" class="STYLE2" id="submit" value="提交">
<input name="reset" type="reset" class="STYLE2" id="reset" value="重置">
</div></td>
</tr>
</table>
</form>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@page import="function.Insert;"%>
<html>
<head>
<title>My JSP 'login_sp.jsp' starting page</title>
<style type="text/css">
<!--
.STYLE1 {
font-family: "华文新魏";
font-size: 36px;
font-weight: bold;
color: #990099;
}
.STYLE2 {
font-family: "华文新魏";
font-size: 24px;
font-weight: bold;
color: #990099;
}
-->
</style>
</head>
<%
if((request.getParameter("username")!=null)&&(request.getParameter("password")!=null))
{
String username=new String(request.getParameter("username").getBytes("utf-8"));
String password=new String(request.getParameter("password").getBytes("utf-8"));
Insert insert = new Insert();
if(insert.stu_insert_login(username,password))
{
response.sendRedirect("stu_query_login_user.jsp");
System.out.println(username+" "+password);
}
else
{
response.sendRedirect("stu_insert_login_failure.jsp");
}
}
%>
<body>
<div align="center"><span class="STYLE1">用户注册</span><br>
</div>
<form name="form1" method="post" action="">
<table width="337" border="6" align="center">
<tr>
<td width="120" height="40"><div align="center"><span class="STYLE2">用户名:</span></div></td>
<td width="191"><div align="center">
<input name="username" type="text" id="username">
</div></td>
</tr>
<tr>
<td height="40" class="STYLE2"><div align="center">密 码:</div></td>
<td><div align="center">
<input name="password" type="password" id="password">
</div></td>
</tr>
<tr>
<td height="40" colspan="2"><div align="center">
<input name="submit" type="submit" class="STYLE2" id="submit" value="提交">
<input name="reset" type="reset" class="STYLE2" id="reset" value="重置">
</div></td>
</tr>
</table>
</form>
</body>
</html>
#6
试着把java小脚本放另外一个页面吧,这样写太丑了,没看出来什么问题
#7
谢谢你的意见,我会改的。
#8
请在<%@page import="function.Insert;"%>上面加
<% request.setCharacterEncoding("UTF-8"); %>
请在html head中加
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
然后就没有必要这样了
String username=new String(request.getParameter("username").getBytes("utf-8"));
直接
String username=request.getParameter("username");
<% request.setCharacterEncoding("UTF-8"); %>
请在html head中加
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
然后就没有必要这样了
String username=new String(request.getParameter("username").getBytes("utf-8"));
直接
String username=request.getParameter("username");
#9
先不要用什么特殊的字体,先换成普通的字体试一下。
#10
楼主请先确定是获取的字符是不是乱码,如果是请转换
String username=new String(request.getParameter("username").getBytes("utf-8"));
然后在控制台输出看看
如果是插入数据库乱码,看看是不是数据库的编码和你插入字符的编码是否一样
String username=new String(request.getParameter("username").getBytes("utf-8"));
然后在控制台输出看看
如果是插入数据库乱码,看看是不是数据库的编码和你插入字符的编码是否一样
#11
你可以加一个全局的编码转换就OK了 界面是UTF-8 不代表到了ACTION里还是的
#1
控制台打印乱码,数据库插入自然乱码。
先保证控制台打印不乱码,
试试看
String username=new String(request.getParameter("username").getBytes("ISO-8859-1"));
或者
String username=new String(request.getParameter("username").getBytes("ISO-8859-1"), "UTF-8");
先保证控制台打印不乱码,
试试看
String username=new String(request.getParameter("username").getBytes("ISO-8859-1"));
或者
String username=new String(request.getParameter("username").getBytes("ISO-8859-1"), "UTF-8");
#2
1楼的应该行,试试差不多。。。
#3
页面的编码是utf-8,String(request.getParameter("username").getBytes("utf-8"));乱码
String(request.getParameter("username").getBytes("GB2312"));乱码
String(request.getParameter("username").getBytes("iso-8859-1"))乱码
String(request.getParameter("username").getBytes("GB2312"));乱码
String(request.getParameter("username").getBytes("iso-8859-1"))乱码
#4
请再试一下
String username=new String(request.getParameter("username").getBytes("ISO-8859-1"), "UTF-8");
若不行,
请贴全部页面代码。
String username=new String(request.getParameter("username").getBytes("ISO-8859-1"), "UTF-8");
若不行,
请贴全部页面代码。
#5
我试过了,还是乱码,现贴部分代码如下:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@page import="function.Insert;"%>
<html>
<head>
<title>My JSP 'login_sp.jsp' starting page</title>
<style type="text/css">
<!--
.STYLE1 {
font-family: "华文新魏";
font-size: 36px;
font-weight: bold;
color: #990099;
}
.STYLE2 {
font-family: "华文新魏";
font-size: 24px;
font-weight: bold;
color: #990099;
}
-->
</style>
</head>
<%
if((request.getParameter("username")!=null)&&(request.getParameter("password")!=null))
{
String username=new String(request.getParameter("username").getBytes("utf-8"));
String password=new String(request.getParameter("password").getBytes("utf-8"));
Insert insert = new Insert();
if(insert.stu_insert_login(username,password))
{
response.sendRedirect("stu_query_login_user.jsp");
System.out.println(username+" "+password);
}
else
{
response.sendRedirect("stu_insert_login_failure.jsp");
}
}
%>
<body>
<div align="center"><span class="STYLE1">用户注册</span><br>
</div>
<form name="form1" method="post" action="">
<table width="337" border="6" align="center">
<tr>
<td width="120" height="40"><div align="center"><span class="STYLE2">用户名:</span></div></td>
<td width="191"><div align="center">
<input name="username" type="text" id="username">
</div></td>
</tr>
<tr>
<td height="40" class="STYLE2"><div align="center">密 码:</div></td>
<td><div align="center">
<input name="password" type="password" id="password">
</div></td>
</tr>
<tr>
<td height="40" colspan="2"><div align="center">
<input name="submit" type="submit" class="STYLE2" id="submit" value="提交">
<input name="reset" type="reset" class="STYLE2" id="reset" value="重置">
</div></td>
</tr>
</table>
</form>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@page import="function.Insert;"%>
<html>
<head>
<title>My JSP 'login_sp.jsp' starting page</title>
<style type="text/css">
<!--
.STYLE1 {
font-family: "华文新魏";
font-size: 36px;
font-weight: bold;
color: #990099;
}
.STYLE2 {
font-family: "华文新魏";
font-size: 24px;
font-weight: bold;
color: #990099;
}
-->
</style>
</head>
<%
if((request.getParameter("username")!=null)&&(request.getParameter("password")!=null))
{
String username=new String(request.getParameter("username").getBytes("utf-8"));
String password=new String(request.getParameter("password").getBytes("utf-8"));
Insert insert = new Insert();
if(insert.stu_insert_login(username,password))
{
response.sendRedirect("stu_query_login_user.jsp");
System.out.println(username+" "+password);
}
else
{
response.sendRedirect("stu_insert_login_failure.jsp");
}
}
%>
<body>
<div align="center"><span class="STYLE1">用户注册</span><br>
</div>
<form name="form1" method="post" action="">
<table width="337" border="6" align="center">
<tr>
<td width="120" height="40"><div align="center"><span class="STYLE2">用户名:</span></div></td>
<td width="191"><div align="center">
<input name="username" type="text" id="username">
</div></td>
</tr>
<tr>
<td height="40" class="STYLE2"><div align="center">密 码:</div></td>
<td><div align="center">
<input name="password" type="password" id="password">
</div></td>
</tr>
<tr>
<td height="40" colspan="2"><div align="center">
<input name="submit" type="submit" class="STYLE2" id="submit" value="提交">
<input name="reset" type="reset" class="STYLE2" id="reset" value="重置">
</div></td>
</tr>
</table>
</form>
</body>
</html>
#6
试着把java小脚本放另外一个页面吧,这样写太丑了,没看出来什么问题
#7
谢谢你的意见,我会改的。
#8
请在<%@page import="function.Insert;"%>上面加
<% request.setCharacterEncoding("UTF-8"); %>
请在html head中加
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
然后就没有必要这样了
String username=new String(request.getParameter("username").getBytes("utf-8"));
直接
String username=request.getParameter("username");
<% request.setCharacterEncoding("UTF-8"); %>
请在html head中加
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
然后就没有必要这样了
String username=new String(request.getParameter("username").getBytes("utf-8"));
直接
String username=request.getParameter("username");
#9
先不要用什么特殊的字体,先换成普通的字体试一下。
#10
楼主请先确定是获取的字符是不是乱码,如果是请转换
String username=new String(request.getParameter("username").getBytes("utf-8"));
然后在控制台输出看看
如果是插入数据库乱码,看看是不是数据库的编码和你插入字符的编码是否一样
String username=new String(request.getParameter("username").getBytes("utf-8"));
然后在控制台输出看看
如果是插入数据库乱码,看看是不是数据库的编码和你插入字符的编码是否一样
#11
你可以加一个全局的编码转换就OK了 界面是UTF-8 不代表到了ACTION里还是的