哎有出问题了救救我吧,我为什么我总会在这种问题上纠缠不清
这是一段JAVA类代码,用struts做的一个东西的登录功能如下:
public class UserAction extends MappingDispatchAction { UserDaoIMPL dao = new UserDaoIMPL(); // 用户登录
public ActionForward login(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) { UserForm userForm = (UserForm) form;
Cookie cookie = new Cookie("userName",userForm.getUsername());// 1
cookie.setMaxAge(60*60*24*365); response.addCookie(cookie);// 2
userForm = dao.login(userForm.getUsername(), userForm.getPassword()); //查找用户是否存在login.jsp 页面username代表用户名如下:
<%@ page language="java" pageEncoding="UTF-8"%>
....
<html:text property="username" value="${cookie.userName.value}"/></td>就几行代码完成了cookie功能,在登录的时候,我输入非中文字符的时候可以登录进去,用中文登录的时候报错如下:
严重: Servlet.service() for servlet ActionServlet threw exception
java.lang.IllegalArgumentException: Control character in cookie value, consider BASE64 encoding your value
at org.apache.tomcat.util.http.ServerCookie.maybeQuote2(ServerCookie.java:365)
at org.apache.tomcat.util.http.ServerCookie.maybeQuote2(ServerCookie.java:358)
at org.apache.tomcat.util.http.ServerCookie.appendCookieValue(ServerCookie.java:268)
at org.apache.catalina.connector.Response.addCookieInternal(Response.java:976)
at org.apache.catalina.connector.Response.addCookie(Response.java:945)
at org.apache.catalina.connector.ResponseFacade.addCookie(ResponseFacade.java:343)
at cn.com.tarena.mvc.action.StudentAction.setLoginStatus(StudentAction.java:55)
at cn.com.tarena.mvc.action.StudentAction.login(StudentAction.java:35)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
已经添加乱码过滤器如下: 还有web也加了...
public class EncodingFilter implements Filter { public void destroy() {
// TODO Auto-generated method stub } public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain arg2) throws IOException, ServletException {
arg0.setCharacterEncoding("utf-8");
arg2.doFilter(arg0, arg1); } public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub }
将utf-8改成别的编码还是错的
当我把以上注释那点的 1和2处去掉的时候,用中文和非中文都能登录成功,请大家支招,谢谢!
这是一段JAVA类代码,用struts做的一个东西的登录功能如下:
public class UserAction extends MappingDispatchAction { UserDaoIMPL dao = new UserDaoIMPL(); // 用户登录
public ActionForward login(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) { UserForm userForm = (UserForm) form;
Cookie cookie = new Cookie("userName",userForm.getUsername());// 1
cookie.setMaxAge(60*60*24*365); response.addCookie(cookie);// 2
userForm = dao.login(userForm.getUsername(), userForm.getPassword()); //查找用户是否存在login.jsp 页面username代表用户名如下:
<%@ page language="java" pageEncoding="UTF-8"%>
....
<html:text property="username" value="${cookie.userName.value}"/></td>就几行代码完成了cookie功能,在登录的时候,我输入非中文字符的时候可以登录进去,用中文登录的时候报错如下:
严重: Servlet.service() for servlet ActionServlet threw exception
java.lang.IllegalArgumentException: Control character in cookie value, consider BASE64 encoding your value
at org.apache.tomcat.util.http.ServerCookie.maybeQuote2(ServerCookie.java:365)
at org.apache.tomcat.util.http.ServerCookie.maybeQuote2(ServerCookie.java:358)
at org.apache.tomcat.util.http.ServerCookie.appendCookieValue(ServerCookie.java:268)
at org.apache.catalina.connector.Response.addCookieInternal(Response.java:976)
at org.apache.catalina.connector.Response.addCookie(Response.java:945)
at org.apache.catalina.connector.ResponseFacade.addCookie(ResponseFacade.java:343)
at cn.com.tarena.mvc.action.StudentAction.setLoginStatus(StudentAction.java:55)
at cn.com.tarena.mvc.action.StudentAction.login(StudentAction.java:35)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
已经添加乱码过滤器如下: 还有web也加了...
public class EncodingFilter implements Filter { public void destroy() {
// TODO Auto-generated method stub } public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain arg2) throws IOException, ServletException {
arg0.setCharacterEncoding("utf-8");
arg2.doFilter(arg0, arg1); } public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub }
将utf-8改成别的编码还是错的
当我把以上注释那点的 1和2处去掉的时候,用中文和非中文都能登录成功,请大家支招,谢谢!
这样来:
public ActionForward login(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {UserForm userForm = (UserForm) form;
try {
String username = new String(userForm.getUsername().getBytes("ISO-8859-1"), "gb2312" );[/color[color=#FF0000]]//原来乱码可以这样解决!
Cookie cookie = new Cookie("userName",username);
cookie.setMaxAge(60*60*24*365); response.addCookie(cookie);
userForm = dao.login(userForm.getUsername(), userForm.getPassword());
String username = new String(request.getParameter("username").getBytes("ISO-8859-1"), "UTF-8" ); //传过来的username是中文值的时候显示"???"乱码
页面都是utf-8
request.getParameter("username")username参数是这样传的:
<a href="tomodify.asp?username=${studentList.username}" >${studentList.username}</a>
添加过滤器了,在tomcat的service文件也加了URIEncoding="UTF-8"了啊。改成GB2312问题是能解决,
2tomcat默认解释的是iso8859-1;uriencoding就是对他的修改
3.如果有过滤器,那你又可以设置。
4.struts中有一个类,继承RequestProcessor类设置。
5。在程序中显示设置。
以上是uri的编码过程。
request.setContentType="text/html;charset=UTF -8"
和
String username = new String(request.getParameter("username").getBytes("ISO-8859-1"), "UTF-8" )