JSP在使用URL传递中文参数的时候遇到的乱码问题
来源:互联网 发布:三菱plc模拟软件 编辑:程序博客网 时间:2024/05/01 17:29
今天在写作业的时候,又遇到了这个问题……几年前学JSP的时候就曾经遇到过,那时候不知道上网查的什么办法,就给解决了。但再次碰到的时候,总是又忘记,然后又是一顿google…… 这次我索性把这个问题及其解决办法写出来吧,免得以后再忘记……
这个问题描述如下:
在我的web project中,有那么几个JSP。其中有index.jsp,里头定义了form(发送到middle.jsp),然后form有文本框的输入项。其次是response.jsp,它用于获取index.jsp发过来的request的参数,其中也包括获取中文参数。还有middle.jsp,用于把response.jsp包含进去……
当我点击发送的时候,哈哈,问题来了,response.jsp里头显示的request.getParameter("XXXX")的值(其实应该是个中文值)是一堆问号……我的index.jsp页面用的编码是UTF-8,response也是,项目的编码也是UTF-8。好了,问题描述完毕……
不说别的,先上代码
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="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>Hello</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 bgcolor="white">
<img src="duke.waving.gif">
<h2>Hello, my name is Duke. What's yours?</h2>
<form method="post">
<div>
<table width="50%">
<tr>
<td width="30%">用户名</td><td width="70%"><input type="text" name="username" size="25"></td>
</tr>
<tr>
<td>用户生日</td><td><input type="text" name="birthday" size="25"></td>
</tr>
<tr>
<td><input type="submit" value="Submit"></td>
<td><input type="reset" value="Reset"></td>
</tr>
</table>
</div>
</form>
</body>
<%
if( request.getParameter("username") != null && request.getParameter("birthday") != null){
String myparameter = "Passion!";
request.setCharacterEncoding("UTF-8");
request.setAttribute("another",myparameter);
if( request.getRequestDispatcher("middle.jsp") != null ){
request.getRequestDispatcher("middle.jsp").forward(request,response);
}
}
%>
</html>
middle.jsp
<%@ page language="java" import="java.util.*" pageEncoding="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 'middle.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 bgcolor="white">
<%@ include file="response.jsp" %>
</body>
</html>
response.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String userName = new String(request.getParameter("username").getBytes("iso8859-1"),"UTF-8");
String birthday = new String(request.getParameter("birthday").getBytes("iso8859-1"),"UTF-8");
String myparameter = (String)request.getAttribute("another");
%>
用户名:<%=userName %>
用户生日:<%=birthday %>
用户自定义参数:<%=myparameter %>
- JSP在使用URL传递中文参数的时候遇到的乱码问题
- JSP通过URL传递中文参数乱码的问题
- 在jsp页面的url链接传递中文参数的乱码问题
- url传递中文参数乱码的问题
- jsp使用URL编码传递中文参数问题※※jsp传递中文参数乱码问题
- jsp使用URL编码传递中文参数问题※※jsp传递中文参数乱码问题
- jsp使用URL编码传递中文参数乱码问题
- JSP使用URL编码传递中文参数问题的解决
- jsp中url传递中文参数乱码的暂时解决方案
- 关于jsp中,以url形式传递中文参数的乱码的问题
- JSP使用get方法提交表单或者超链接传递参数时候中文乱码的解决方法
- URL传递中文参数中乱码问题的解决方案
- ajax提交-----URL中文参数传递后的乱码问题
- 解决通过url传递中文参数乱码的问题
- Spring3 传递URL参数中文乱码的问题
- https的URL参数传递中文乱码问题
- url中传递中文参数出现乱码的问题
- jsp通过URL向页面传递中文参数乱码问题
- POJ 1006 解同余方程
- Batch Jobs
- 让程序支持文件拖拽
- 软件开发人员绩效评价参考标准
- java 导出 txt
- JSP在使用URL传递中文参数的时候遇到的乱码问题
- 快排测试程序
- pam_ldap详细配置
- 【转】 Java开源Web测试工具列表
- 关于sizeof(转载)
- windows下修改php.ini位置
- 推荐20个关于CSS3优秀学习资源
- 结构体与联合体的用法
- 将数据库查询结果插入到相关表格的若干事项