初学J2EE遭遇乱码问题 eclipse中已经设置为utf-8.页面显示前面都没乱后面就乱了,不知道为啥,改变过tomcat的编码模式,还是不行网上各种方法解决不了啊。求大神。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 servlet中做如下操作 //设置解码格式 response.setContentType("text/html"); request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8");jsp中,首行如下写法:<%@ page contentType="text/html; charset=utf-8" language="java" errorPage="" pageEncoding="utf-8"%>检查utf-8的拼写。还不行,请发下jsp和servlet的代码吧。 在JSp开头<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>还有在页面内加入request.setCharacterEncoding("utf-8");例如input.jsp:<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"contentType="text/html;charset=utf-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <base href="<%=basePath%>"> <title>My JSP 'input.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <div align="center"> <form method="post" action="SaveName.jsp"> 您的姓名:<input type="text" name="username" size="20"> <p><input type="submit" value="提交" > <input type="reset" value="重置"> </p> </form> </div> </body></html>下面是SavaName.jsp<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" contentType="text/html;charset=utf-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <base href="<%=basePath%>"> <title>My JSP 'SaveName.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <% request.setCharacterEncoding("utf-8"); String name = request.getParameter("username");//获取上一页面输入的用户名 out.println("hello!"+name); %> </body></html>效果:提交后 先通过控制台检查是在哪个层乱码了,先看看传输到servlet的时候试试不是乱码,一般都是这里乱码。如果怎么都解决不了,可以用以下办法:假设乱码字符串为str1str2 = new String (str1.getBytes() ,"utf-8");直接把乱码字符串重编码 crystal reports 水晶报表疑问 Tomcat之死新说---SSH无异常篇 请教Form表单提交问题 一个很奇怪的问题 如何从子窗口传值 jsp连接access的问题 数据查询后记录集关闭的小问题。 java信息提示 令我困惑的一个问题!!!!希望高手答复,在线等待! 数据库查询到的ResultSet中的内容为什么不能循环输出? spring 加mybatis 最基本的CURD ,第一次用,不熟悉,,帮帮忙 JAVA如何解析这一段JSON?
//设置解码格式
response.setContentType("text/html");
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");jsp中,首行如下写法:
<%@ page contentType="text/html; charset=utf-8" language="java" errorPage="" pageEncoding="utf-8"%>检查utf-8的拼写。还不行,请发下jsp和servlet的代码吧。
charset=UTF-8" pageEncoding="UTF-8"%>还有在页面内加入
request.setCharacterEncoding("utf-8");
例如input.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"contentType="text/html;charset=utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'input.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head>
<body>
<div align="center">
<form method="post" action="SaveName.jsp">
您的姓名:<input type="text" name="username" size="20">
<p><input type="submit" value="提交" > <input type="reset" value="重置">
</p>
</form>
</div>
</body>
</html>
下面是SavaName.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" contentType="text/html;charset=utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'SaveName.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head>
<body>
<%
request.setCharacterEncoding("utf-8");
String name = request.getParameter("username");//获取上一页面输入的用户名
out.println("hello!"+name);
%>
</body>
</html>
效果:提交后
先看看传输到servlet的时候试试不是乱码,一般都是这里乱码。
如果怎么都解决不了,可以用以下办法:
假设乱码字符串为str1
str2 = new String (str1.getBytes() ,"utf-8");
直接把乱码字符串重编码