怎么让servlet支持中文显示,我的webserver是tomcat,系统win2k

zergling 2001-07-25 05:15:57
源码:

/*
作者:何志强[hhzqq@21cn.com]
日期:2000-08-10
版本:1.0
功能:Servlet基础例程 - HelloServlet
*/

import java.io.*;
import java.text.*; //MessageFormat
import javax.servlet.*;
import javax.servlet.http.*;

public class HelloServlet extends HttpServlet{
//页面标题
protected static final String strTitle = "Servlet基础例程 - HelloServlet";

//页眉
protected static final String strHeader =
"<html>"+
"<head>"+
"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\">"+
"<title>{0}</title>"+
"</head>"+
"<body>";

//页脚
protected static final String strFooter =
"</body>"+
"</html>";

//表单
protected static final String strForm =
"<form action=\"{0}\" method=\"post\">"+
"您尊姓大名:<input type=\"text\" name=\"name\">"+
"<input type=\"submit\" name=\"submit\" value=\"提交\">"+
"</form>";

protected static final String strHello =
"您好,{0},欢迎来到Servlet/JSP世界!";

//出错信息
protected static final String strError =
"<h2><font color=\"#ff0000\">{0}</font></h2>";

protected void doGet(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{
process(req,resp);
}

protected void doPost(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{
process(req,resp);
}

protected void process(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{
try{
String submit = req.getParameter("submit");
if(submit==null)
printForm(req,resp);
else
printHello(req,resp);
}
catch(Exception e){
printError(e.toString(),req,resp);
}
}

protected void printForm(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{
//在使用PrintWriter前得先设置Content Type
resp.setContentType("text/html");

PrintWriter out = resp.getWriter();

//输出页眉
out.print(MessageFormat.format(strHeader,new Object[]{strTitle+" - 请输入尊姓大名"}));

//输出表单
out.print(MessageFormat.format(strForm,new Object[]{req.getContextPath()+req.getServletPath()}));

//输出页脚
out.print(strFooter);

out.flush();
}

protected void printHello(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{
//获取用户输入的数据
String name = req.getParameter("name");

if(name==null)
name = "无名氏";

//在使用PrintWriter前得先设置Content Type
resp.setContentType("text/html");

PrintWriter out = resp.getWriter();

//输出页眉
out.print(MessageFormat.format(strHeader,new Object[]{strTitle+" - 欢迎您"}));

//输出欢迎信息
out.print(MessageFormat.format(strHello,new Object[]{name}));

//输出页脚
out.print(strFooter);

out.flush();
}

protected void printError(String error, HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{
//在使用PrintWriter前得先设置Content Type
resp.setContentType("text/html");

PrintWriter out = resp.getWriter();

//输出页眉
out.print(MessageFormat.format(strHeader,new Object[]{strTitle+" - 出错信息"}));

//输出出错信息
out.print(MessageFormat.format(strError,new Object[]{error}));

//输出页脚
out.print(strFooter);

out.flush();
}
}

...全文
157 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
qhliaok 2001-09-04
  • 打赏
  • 举报
回复
nicolas(nicolas)

great!!!


nicolas 2001-07-27
  • 打赏
  • 举报
回复
解决方法:
1.修改区域设置,改为"英语(美国)",重启机器。
2.在jsp页面中加入一条语句:
<%@ page contentType="text/html;charset=gb2312"%>

要在jsp页面中正常显示中文信息,先把欲显示口语信息串"ISO8859-1"转化后:
a.当区域设置为"英语(美国)"时,上面这条语句加入起作用;
b.当区域设置为"中文(中国)"时,上面这条语句不能加入,才能正常显示中文。
3.在编译servlet和jsp时加入代码选项。
javac -encoding iso8859-1 myservlet.java
在jsp的zone配置文件中,修改编译参数为:
compiler=builtin-javac-encoding ISO8859-1
4.在classpath中加入i18n.jar的路径
5.最士的办法
try{
out.println(new((new String("我家在南方")).getBytes("GBK"),"ISO8859-1"));
}catch(UnsupportedEncodingException e)
{
//......
}

或者用下面这个函数:
<%
public String getStr(String str){
try{
String temp_p=str;
byte[] temp_t=temp_p.getBytes("ISO8859-1");
String temp=new String(temp_t);
return temp;
}catch(Exception e){
}
return "null";
}
%>
javastone 2001-07-27
  • 打赏
  • 举报
回复
在HelloServlet 的首行加上以下一段试试。
private static final String CONTENT_TYPE = "text/html;charset=gb2312";

response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
out.print(...);
应该没问题。

backlove 2001-07-27
  • 打赏
  • 举报
回复
up不了了吧,我帮你
zergling 2001-07-27
  • 打赏
  • 举报
回复
up
zergling 2001-07-26
  • 打赏
  • 举报
回复
up
zergling 2001-07-25
  • 打赏
  • 举报
回复
up
JSP实践之旅 电子书版



序言--关于JSP实践之旅
简明介绍
JSP内幕
JSP官方白皮书
国内不谈java
基本语法介绍
2001年度Java最佳技术和产品
JSP入门介绍
三种Web开发主流技术的评价之PHP
三种Web开发主流技术的评价之ASP
三种Web开发主流技术的评价之JSP
使用JSP技术设计电子商务应用系统——从入门到精通
JSP手册
关于PHP的一种评论:
有感于《一个最近完成的JAVA项目的反思》
ASP/PHP/JSP大比拼
动态网页制作技术JSP与ASP的比较
ASP与JSP的比较
关于JSP开发/支撑平台
Jakarta-Tomcat 简明中文版用户指南第一部分
Apache Tomcat 4.0的新特性
Tomcat3.1新特性
如何在Windows 9x环境中配置Apache + Tomcat.JSP
JSWDK环境安装与配置
Resin服务器平台介绍
Resin在IIS中的安装配置
JRun2.3平台介绍
Unify eWave ServletExec
WebSphere应用服务器
Windows2000上安装Apache+ApacheJserv+gnujsp之完全攻略
如何同时安装并支持PHP和JSP
redhat下tomcat的安装
Windows NT 4.0下安装Apache+Servlet+JSP
Redhat+apache+jserv+jsdk
JSP语法介绍
SJP语法详解
JSP/Servlet 中的汉字编码问题
javamail在JSP中的应用
javamail何志强篇
JDBC精要
jsp在win2k/oracle上的应用
如何用UML为JSP建模
通用信息发布程序
JSP概述及音乐店设计
不用odbc直接连接SQL Server
jsp的出错处理
jsp的wap应用
jsp实现购物程序
用jspsmart Bean实现文件上载
用jsp连接mySQL数据库
基本内置组件
JDBC应用示例
一个用JSP做的日历
JSP技巧集锦(一)
关于JSP写文件的补充
JSP文件操作:读取,写入和追加
在jsp中实现分页

81,117

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧