JSP开拓应用是,中文乱码是个比力稀有的标题,其根源是:Web容器默许的字符处置编码是ISO-8859-1。
实例1、JSP页面显现时
html
head
title 中文乱码——JSP页面显现时 /title
/head
body
center
br/
h1 木兰辞拟古决尽词柬友 /h1
p 人生若只如初见,何事金风抽丰悲画扇。 /p
p 随便变却故人心,却道故人心易变。 /p
p 骊山语罢清宵半,泪雨霖铃终不怨。 /p
p 何如薄幸锦衣郎,比翼连枝当日愿。 /p
/center
/body
/html
运转成果:
处置方式:为其指定中文字符集, html 前插手
%@ page contentType= text/html;charset=gb2312 %
实例2、JSP页面传送中文参数时
注册页面:
%@ page contentType= text/html;charset=gb2312 %
html
head
title 中文乱码——JSP页面传送中文参数时 /title
/head
body
h2 恳求账号: /h2
form action= userMsg.jsp method= POST
p 邮箱: nbsp; input type= text name= email id= email / p/
p 昵称: nbsp; input type= text name= nickname id= nickname / p/
p 暗码: nbsp; input type= password name= password id= password / p/
p 性别: nbsp; input type= radio name= sex id= sex value= 男 / 男
input type= radio name= sex id= sex value= 女 / 女 p/
textarea name= introduction id= introduction rows= 5 cols= 27 一句话先容自身... /textarea
p input type= submit value= 提交恳求 /p
/form
/body
/html
小我信息页面:
%@ page contentType= text/html;charset=gb2312 %
html
head
title 中文乱码——JSP页面传送中文参数时 /title
/head
body
center
h2 用户信息: /h2
% String email = request.getParameter( email %
% String nickname = request.getParameter( nickname %
% String password = request.getParameter( password %
% String sex = request.getParameter( sex %
% String introduction = request.getParameter( introduction %
p 邮箱: nbsp; % out.print(email); % p/
p 昵称: nbsp; % out.print(nickname); % p/
p 暗码: nbsp; % out.print(password); % p/
p 性别: nbsp; % out.print(sex); % p/
p 小我先容: %out.print(introduction); % /p
/center
/body
/html
运转成果:
处置方式:点窜小我信息页面以下
%@ page contentType= text/html;charset=gb2312 %
html
head
title 中文乱码——JSP页面传送中文参数时 /title
/head
body
h2 用户信息: /h2
% String email = newString(request.getParameter( email ).getBytes( ISO-8859-1 ), gb2312 %
% String nickname = newString(request.getParameter( nickname ).getBytes( ISO-8859-1 ), gb2312 %
% String password = newString(request.getParameter( password ).getBytes( ISO-8859-1 ), gb2312 %
% String sex = newString(request.getParameter( sex ).getBytes( ISO-8859-1 ), gb2312 ;%
% String introduction = newString(request.getParameter( introduction ).getBytes( ISO-8859-1 ), gb2312 ;%
p 邮箱: % out.print(email); % p/
p 昵称: % out.print(nickname); % p/
p 暗码: % out.print(password); % p/
p 性别: % out.print(sex); % p/
p 小我先容: %out.print(introduction); % /p
/body
/html
实例3、Servlet处置中文参数时
注册页面:
%@ page contentType= text/html;charset=gb2312 %
%@ page import= test.UserMsg %
html
head
title 中文乱码——JSP页面传送中文参数时 /title
/head
body
h2 恳求账号: /h2
form action= ./UserMsg method= POST
p 邮箱: input type= text name= email id= email / p/
p 昵称: input type= text name= nickname id= nickname / p/
p 暗码: input type= password name= password id= password / p/
p 性别: input type= radio name= sex id= sex value= 男 / 男
input type= radio name= sex id= sex value= 女 / 女 p/
textarea name= introduction id= introduction rows= 5 cols= 27 一句话先容自身... /textarea
p input type= submit value= 提交恳求 /p
/form
/body
/html
UserMsg.java(Servlet)
package test;
importjava.io.IOException;
importjava.io.PrintWriter;
importjava.io.UnsupportedEncodingException;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
public classUserMsg extends HttpServlet{
public void doGet(HttpServletRequestrequest,
HttpServletResponse response){
doPost(request, response);
}
public void doPost(HttpServletRequestrequest,
HttpServletResponse response){
try {
request.setCharacterEncoding( gb2312 );
} catch (UnsupportedEncodingExceptione) {
e.printStackTrace();
}
PrintWriter out = null;
try {
out = response.getWriter();
} catch (IOException e1) {
e1.printStackTrace();
}
out.print( html );
out.print( body );
out.print( h2 + 用户信息: + /h2 );
out.print( p + 邮箱: +request.getParameter( email )+ p/ );
out.print( p + 昵称: +request.getParameter( nickname )+ p/ );
out.print( p + 暗码: +request.getParameter( password )+ p/ );
out.print( p + 性别: +request.getParameter( sex )+ p/ );
out.print( p + 小我先容: +request.getParameter( introduction )+ p/ );
out.print( /html );
out.print( /body );
}
}
运转成果:
处置方式:在doPost中插手:
response.setContentType( text/html; charset=gb2312