今天我做注册登录的时候碰到了非常奇怪的编码问题,请各位高手指教一下:
我建的项目的编码是UTF-8,所有的页面也是UTF-8的编码,框架我只用了struts2,在struts.xml文件我也加了这样一句:
<constant name="struts.il8n.encoding" value="UTF-8"/>
然后我的数据库的编码也都改成UTF-8了
可是在测试的时候就出问题了:
我重启tomcat后地磁登录的时候编码是对的,数据库里存进去的数据也对了
可第二次注册就出问题了,数据库存进去的全是乱码,
然后我在action里面打印出来也是乱码
一下是我的注册的actionpackage user.web;import user.entity.User;
import user.dao.AddUserDao;
import com.opensymphony.xwork2.Action;
public class RegokAction implements Action{ private User user;
private String message1,message; public String execute() throws Exception{
int imsg=0;
System.out.println("name = "+user.getName());
String name = new String(user.getName().getBytes("iso-8859-1"), "utf-8");
System.out.println("name += "+name);
String username = new String(user.getUsername().getBytes(),"utf-8");
String department = new String(user.getDepartment().getBytes(),"utf-8");
User user1=new User();
user1.setName(name);
user1.setPassword(user.getPassword());
user1.setUsername(username);
user1.setDepartment(department);
System.out.println(user.getName());
System.out.println(name);
AddUserDao addUser=new AddUserDao();
addUser.Addone(user);
imsg=addUser.getImsg();
if(imsg==1){
message="注册成功,请登录";
return "SUCCESS";
}
else if(imsg==2){
message1="注册失败!该用户名已经存在,请使用其他用户名注册";
return "ERROR";
}
else{
message1="未知错误,请重新注册";
return "ERROR";
}
}
public String getMessage1(){
return message1;
}
public String getMessage(){
return message;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user= user;
}}
我建的项目的编码是UTF-8,所有的页面也是UTF-8的编码,框架我只用了struts2,在struts.xml文件我也加了这样一句:
<constant name="struts.il8n.encoding" value="UTF-8"/>
然后我的数据库的编码也都改成UTF-8了
可是在测试的时候就出问题了:
我重启tomcat后地磁登录的时候编码是对的,数据库里存进去的数据也对了
可第二次注册就出问题了,数据库存进去的全是乱码,
然后我在action里面打印出来也是乱码
一下是我的注册的actionpackage user.web;import user.entity.User;
import user.dao.AddUserDao;
import com.opensymphony.xwork2.Action;
public class RegokAction implements Action{ private User user;
private String message1,message; public String execute() throws Exception{
int imsg=0;
System.out.println("name = "+user.getName());
String name = new String(user.getName().getBytes("iso-8859-1"), "utf-8");
System.out.println("name += "+name);
String username = new String(user.getUsername().getBytes(),"utf-8");
String department = new String(user.getDepartment().getBytes(),"utf-8");
User user1=new User();
user1.setName(name);
user1.setPassword(user.getPassword());
user1.setUsername(username);
user1.setDepartment(department);
System.out.println(user.getName());
System.out.println(name);
AddUserDao addUser=new AddUserDao();
addUser.Addone(user);
imsg=addUser.getImsg();
if(imsg==1){
message="注册成功,请登录";
return "SUCCESS";
}
else if(imsg==2){
message1="注册失败!该用户名已经存在,请使用其他用户名注册";
return "ERROR";
}
else{
message1="未知错误,请重新注册";
return "ERROR";
}
}
public String getMessage1(){
return message1;
}
public String getMessage(){
return message;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user= user;
}}
package user.web;import user.entity.User;
import user.dao.AddUserDao;
import com.opensymphony.xwork2.Action;
public class RegokAction implements Action{ private User user;
private String message1,message; public String execute() throws Exception{
int imsg=0;
System.out.println("name = "+user.getName());
AddUserDao addUser=new AddUserDao();
addUser.Addone(user);
imsg=addUser.getImsg();
if(imsg==1){
message="注册成功,请登录";
return "SUCCESS";
}
else if(imsg==2){
message1="注册失败!该用户名已经存在,请使用其他用户名注册";
return "ERROR";
}
else{
message1="未知错误,请重新注册";
return "ERROR";
}
}
public String getMessage1(){
return message1;
}
public String getMessage(){
return message;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user= user;
}}以上是没有加任何调试代码的action
我想应该都有一个这样的设置,看下编码是什么
package user.entity;import java.io.Serializable;public class User implements Serializable{ /**
*
*/
private static final long serialVersionUID = 4062665848394842235L;
private String name;
private String username;
private String password;
private String department; public User(){
}
public String getName(){
return this.name;
}
public void setName(String name){
this.name=name;
}
public String getUsername(){
return this.username;
}
public void setUsername(String username){
this.username=username;
}
public String getPassword(){
return this.password;
}
public void setPassword(String password){
this.password=password;
}
public String getDepartment(){
return this.department;
}
public void setDepartment(String department){
this.department=department;
}
}以上是User.java
这是web.xml<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>Struts Blank</display-name> <filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> <welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list></web-app>
struts2 出乱码大概 就jsp页面 和数据库问题 好像你 数据库是保证了还有个 就是 那个配置了
action的System.out.println("name = "+user.getName());
输出的是name = ???è??
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
String path = request.getContextPath();
String basePath = path+"/";
%><html>
<head>
<title>用户注册</title>
<script type="text/javascript">
function checkRegister(){
if(document.getElementById("name").value==''){
alert("用户名不能为空!");
document.getElementById("name").focus();
return false;
}
if(document.getElementById("password").value==''){
alert("用户密码不能为空!");
document.getElementById("password").focus();
return false;
}
if(document.getElementById("repassword").value!=document.getElementById("password").value){
alert("两次输入的密码不一致!");
document.getElementById("repassword").focus();
return false;
}
if(document.getElementById("department").value==''){
alert("所属部门不能为空!");
document.getElementById("department").focus();
return false;
}
return true;
}
</script>
</head>
<body>
<h2>用户注册</h2>
<form action="Regok.action" method="post" id="userRegister">
<table align="center" border="0">
<tr>
<td align="right">用户名:</td>
<td><input type="text" id="name" name="user.name"/></td>
</tr>
<tr>
<td align="right">密码:</td>
<td><input type="password" id="password" name="user.password"/></td>
</tr>
<tr>
<td align="right">重复密码:</td>
<td><input type="password" id="repassword" name="repassword"/></td>
</tr>
<tr>
<td align="right">昵称:</td>
<td><input type="text" id="username" name="user.username"/></td>
</tr>
<tr>
<td align="right">所属部门:</td>
<td><input type="text" id="department" name="user.department"/></td>
</tr>
<tr>
<td align="center"><input type="submit" name="submit" value="注册" onclick="return checkRegister();"/></td>
<td><input type="reset" name="reset" value="重置"/><br/>
</td>
</tr>
<tr>
<td colspan="2"><font color="red"><s:property value="message1" /></font></td>
</tr>
</table>
</form>
</body>
</html>
action的System.out.println("name = "+user.getName());
输出的是name = ???è??
<head>
<title>用户注册</title>
<script type="text/javascript">
function checkRegister(){
if(document.getElementById("name").value==''){
alert("用户名不能为空!");
document.getElementById("name").focus();
return false;
}
if(document.getElementById("password").value==''){
alert("用户密码不能为空!");
document.getElementById("password").focus();
return false;
}
if(document.getElementById("repassword").value!=document.getElementById("password").value){
alert("两次输入的密码不一致!");
document.getElementById("repassword").focus();
return false;
}
if(document.getElementById("department").value==''){
alert("所属部门不能为空!");
document.getElementById("department").focus();
return false;
}
return true;
}
</script>
</head>
<body>
<h2>用户注册</h2>
<form action="Regok.action" method="post" id="userRegister">
<table align="center" border="0">
<tr>
<td align="right">用户名:</td>
<td><input type="text" id="name" name="user.name"/></td>
</tr>
<tr>
<td align="right">密码:</td>
<td><input type="password" id="password" name="user.password"/></td>
</tr>
<tr>
<td align="right">重复密码:</td>
<td><input type="password" id="repassword" name="repassword"/></td>
</tr>
<tr>
<td align="right">昵称:</td>
<td><input type="text" id="username" name="user.username"/></td>
</tr>
<tr>
<td align="right">所属部门:</td>
<td><input type="text" id="department" name="user.department"/></td>
</tr>
<tr>
<td align="center"><input type="submit" name="submit" value="注册" onclick="return checkRegister();"/></td>
<td><input type="reset" name="reset" value="重置"/><br/>
</td>
</tr>
<tr>
<td colspan="2"><font color="red"></font></td>
</tr>
</table>
</form>
</body>
</html>
你先加上<meta http-equiv="content-type
虽然感觉不是这个问题。
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
还是没有用
pageEncoding="utf-8"%>怪乱的
<%@ page language="java" contentType="text/html; charset=utf-8"%>
是页面的跳转问题
我从另一个页面直接跳到注册的jsp页面,没有走struts了
这样就导致了跳转过来之后的注册页面的编码不对