码迷,mamicode.com
首页 > Web开发 > 详细

jsp中文显示乱码的解决办法

时间:2015-09-17 13:41:57      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:

表单提交时,获取输入的中文参数时,浏览器会出现中文乱码,这时在表单响应的jsp文件中加入request.setCharacterEncoding("utf-8");即可解决。相关代码如下:


附:表单提交的jsp文件内容:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>


<html>

  <head>

    <META http-equiv="Content-Type" content="text/html; charset=utf-8">

    <title>测试JSP内置对象</title>

  </head>

  

  <body>

 <form action="internalObjResult.jsp" method="post">

  uname:<input type="text" name="uname"><br/>

  pwd:<input type="password" name="pwd"><br/>

  hobbies:<input type="text" name="hobby">&nbsp;

  <input type="text" name="hobby"><br/>

  submit:<input type="submit" name="submit"/>

 </form>

  </body>

</html>

表单响应的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>

  <META http-equiv="Content-Type" content="text/html; charset=utf-8">

    <title>My JSP ‘internalObjResult.jsp‘ starting page</title>

  </head>

  

  <body>

  <%request.setCharacterEncoding("utf-8");//不加此句,浏览器端中文会显示乱码

  String uname=request.getParameter("uname");

  out.println(uname+"<br>");

  Enumeration<?> e=request.getParameterNames();//不加<?>会有警告

  while(e.hasMoreElements()){

  out.println(e.nextElement()+"<br>");

  }

  out.println("所有values:"+"<br>");

  String[] s=request.getParameterValues("hobby");

  for(String temp:s){

  out.println(temp+"<br>");

  }

  %>

  

  </body>

</html>


jsp中文显示乱码的解决办法

标签:

原文地址:http://my.oschina.net/u/2430057/blog/507526

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!