使用jsp标签和java资源管理实现jsp支持多语言
1.编写一个Serverlet并设置服务器启动是初始化该Servlet,并在初始化方法中实现对java的资源加载;
DispatcherServlet.java
package mypack; import java.io.IOException;
import java.io.InputStream;
import java.util.Properties; import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class DispatcherServlet extends HttpServlet{ private String target="/hello.jsp";
public void init(ServletConfig config)
throws ServletException {
System.out.println("服务器启动时设置初始化servlet并且加载java资源");
super.init(config);
try {
Properties ps=new Properties();
Properties ps_ch=new Properties();
ServletContext cxt=config.getServletContext();
InputStream in=cxt.getResourceAsStream("/WEB-INF/messageresource.properties");
ps.load(in);
InputStream in_ch=cxt.getResourceAsStream("/WEB-INF/messageresource_ch.properties");
ps_ch.load(in_ch);
cxt.setAttribute("ps",ps);
cxt.setAttribute("ps_ch",ps_ch);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} @Override
protected void doDelete(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
super.doDelete(req, resp);
} @Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
super.doGet(req, resp);
this.doPost(req, resp);
} @Override
protected void doPost(HttpServletRequest request, HttpServletResponse resp)
throws ServletException, IOException { String username = request.getParameter("username");
// Get the password from the request
String password = request.getParameter("password"); request.setAttribute("USER", username);
request.setAttribute("PASSWORD", password);
ServletContext context = getServletContext(); System.out.println("Redirecting to " + target);
RequestDispatcher dispatcher =
context.getRequestDispatcher(target);
dispatcher.forward(request, resp);
// return ;
}
public void destroy() {
} }
2.资源的具体内容
messageresource.properties
hello.title = helloapp
hello.hello = Hello
login.title = helloapp
login.user = User Name
login.password = Password
login.submit =Submit
messageresource_ch.properties
hello.title = helloappµÄhelloÒ³Ãæ
hello.hello = ÄãºÃ
login.title = helloappµÄµÇÂ¼Ò³Ãæ
login.user = Óû§Ãû
login.password = ¿ÚÁî
login.submit =\u00CC\u00E1\u00BD\u00BB
3.主页为语言选择页面
index.html
<html>
<head>
<title>helloapp</title>
</head>
<body >
<p><font size="7">Welcome to HelloApp</font></p>
<p><a href="login.jsp?language=English">English version </a>
<p><a href="login.jsp?language=Chinese">Chinese version </a>
</body>
</html>
4.选择语言后跳转到jsp登录页面,并传递language参数,然后在jsp登录页面将传过来的language参数保存到httpSession中,页面body中的标签负责根据设置的参数
从java资源中选择打印对应的显示文字:
login.jsp
<%@page contentType="text/html; charset=GB2312" %>
<%@taglib uri="/WEB-INF/mytaglib.tld" prefix="mm" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<% String language=request.getParameter("language");
if(language==null)language="English";
session.setAttribute("language",language);
%>
<html>
<head>
<title><mm:message key="login.title"/> </title> </head> <body> <form action="dispatcher" method="post" name="logForm">
<center>
<table>
<tr><td><mm:message key="login.user"/>:</td><td><input type="text" name="username"/></td></tr>
<tr><td><mm:message key="login.password"/>:</td><td><input type="password" name="password"/></td></tr>
<tr><td><input type="submit" value="<mm:message key="login.submit"/>" name="submit"></td></tr>
</table>
</center>
</form>
</body>
</html>
5.标签的处理类
MessageTag.java
package mypack; import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Properties; import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport; public class MessageTag extends TagSupport{ private String key=null; public MessageTag() {
}
@Override
public int doEndTag() throws JspException {
// TODO Auto-generated method stub Properties ps=(Properties)pageContext.getAttribute("ps",pageContext.APPLICATION_SCOPE);
Properties ps_ch=(Properties)pageContext.getAttribute("ps_ch",pageContext.APPLICATION_SCOPE);
HttpSession session=pageContext.getSession();
String language=(String)session.getAttribute("language");
String message=null;
try {
if(language!=null && language.equals("Chinese")){
message=(String)ps_ch.get(key);
message=new String(message.getBytes("ISO-8859-1"),"GB2312");
}else
message=(String)ps.get(key);
pageContext.getOut().print(message); } catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return super.doEndTag();
} @Override
public void release() {
// TODO Auto-generated method stub
super.release();
} public String getKey() {
return key;
} public void setKey(String key) {
this.key = key;
} }
6.当login页面接受参数后跳转到处理组件DispatcherServlet.java,然后把参数放到request属性中再重定位到欢迎界面
hello.jap
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@page contentType="text/html; charset=GB2312" %>
<%@taglib uri="/WEB-INF/mytaglib.tld" prefix="mm" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title><mm:message key="hello.title"/></title>
</head> <body>
<b><mm:message key="hello.hello"/>:<%=request.getAttribute("USER") %></b>
</body>
</html>
7.自定义标签库的描述:
mytaglib.tld
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE taglib
PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN"
"http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">
<taglib>
<tlibversion>1.0</tlibversion>
<jspversion>1.1</jspversion>
<shortname>mytaglib</shortname>
<uri>/mytaglib</uri> <tag>
<name>message</name>
<tagclass>mypack.MessageTag</tagclass>
<bodycontent>empty</bodycontent>
<info>produce message by key</info>
<attribute>
<name>key</name>
<required>true</required>
</attribute>
</tag> </taglib>
8.拦截器以及标签库都在web.xml中注册
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>mypack.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/dispatcher</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
<welcome-file>index.html</welcome-file>
<welcome-file>login.jsp</welcome-file>
<welcome-file>hello.jsp</welcome-file>
</welcome-file-list>
<resource-ref>
<description>DB Conection</description>
<res-ref-name>jdbc/test</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref> <taglib>
<taglib-uri>/mytaglib</taglib-uri>
<taglib-location>/WEB-INF/mytaglib.tld</taglib-location>
</taglib> </web-app>
使用jsp标签和java资源管理实现jsp支持多语言的更多相关文章
- 如何在 js 代码中使用 jsp 标签或 Java 代码
JSP 标签还是很方便的,比如 Struts.Spring 等提供给我们的 JSP 标签,可以用它们来获取变量或进行一些计算.比如 struts2 的 <s:url value="/a ...
- 初学Java Web(6)——JSP学习总结
为什么要学习 JSP Servlet 的短板: Servlet 的出现,是为了解决动态输出网页的问题. 虽然这样做目的能达到,但是存在一些缺陷: 在 Servlet 输出网页片段非常恶心 (可读性差, ...
- JSP标签的用法
JSP动作标签: 通过动作标签,程序员可以在JSP页面中把页面的显示功能部分 封装起来,是整个页面更简洁和易于维护 <jsp:useBean> 装载一个将在JSP页面中使用的JavaBea ...
- 快速分页:jsp标签pager-taglib
一:简介 Pager-taglib,支持多种风格的分页显示.实际上它是一个Jsp标签库,为在JSP上显示分页信息而设计的一套标签,通过这些标签的不同的组 合,会形成多种不一样的分页页面,风格各异.它既 ...
- java web学习总结(二十七) -------------------JSP标签介绍
一.JSP标签介绍 JSP标签也称之为Jsp Action(JSP动作)元素,它用于在Jsp页面中提供业务逻辑功能,避免在JSP页面中直接编写java代码,造成jsp页面难以维护. 二.JSP常用标签 ...
- java攻城师之路--复习java web之jsp入门_El表达式_JSTL标签库
JSP 技术掌握:JSP语法 + EL + JSTL 为什么sun推出 JSP技术 ? Servlet 生成网页比较复杂,本身不支持HTML语法,html代码需要通过response输出流输出,JSP ...
- 复习java web之jsp入门_El表达式_JSTL标签库
JSP 技术掌握:JSP语法 + EL + JSTL 为什么sun推出 JSP技术 ? Servlet 生成网页比较复杂,本身不支持HTML语法,html代码需要通过response输出流输出,JSP ...
- jsp不解析el表达式,不识别jstl标签,找不到http://java.sun.com/jsp/jstl/core
问题描述: jsp页面中el表达式,例如:${pageContext.request.contextPath},原样呈现,未被解析. 解决方案: 为jsp页添加page指令如下: <%@ pag ...
- js 和 css 中 不能使用 jsp 页面中一些 标签 和 java 代码等,应注意
js 和 css 中 不能使用 jsp 页面中一些 标签 和 java 代码等,应注意 如 ${ } <%%> 等
随机推荐
- javascript第九课"闭包"
所谓闭包:就是一个函数内部又定义了一个函数,而这个函数能访问外部函数作用域范围内的变量,这个内部函数就叫做闭包! js中的面向对象都是使用闭包来实现的 闭包里使用的变量会现在当前函数内搜索,没有的 ...
- vs快捷键
Ctrl+E,D ----格式化全部代码 Ctrl+A+K+FCtrl+E,F ----格式化选中的代码 Ctrl+K+FCTRL + SHIFT + B生成解决方案 Alt+B+B 或 F6 生成当 ...
- 列"xx"不在表Table中
在数据库中用了left join来查一个表的所有列和另一个表的一个列,但无论用IDataReader还是DataSet都不能获取到另一个表的列,调试时总是说没有那个值,但在数据库中执行语句又有.一直想 ...
- cocos2dx新建工程分析
这里我新建了一个cocos的工程叫做hello,没有的自己翻上一页教程 运行一下 出来是这个样子的: 左下角是帧频,可以设置显示或是不显示,中间是图片精灵,右下角是关闭按钮,然后上面是一个hello ...
- java InputStream使用
InputStream读取流有三个方法,分别为read(),read(byte[] b),read(byte[] b, int off, int len).其中read()方法是一次读取一个字节,效率 ...
- java泛型问题 关于警告:XX is a raw type
(本文例子适用于JDK 5.0, 学习请先安装并配置!!!) 我们从一个简单的例子开始:假设我们现在需要一个专用来存储字符串的List,该如何实现?呵呵,这还不简单,且看如下代码: ...
- 重拾php---以及zend-studio 的使用快捷方式
感觉好久没有碰php了,今天心血来潮,重新入门.先整理一下刚刚学习的笔记. 一个字符串是用双引号括起来的一个词或一个句字,比如:“Hi,imooc!”.你可以用PHP语言输出把这个字符串输出,像这样: ...
- CSS自学笔记(6):CSS的模型
CSS 框模型 (Box Model) 规定了元素框处理元素内容.内边距.边框和外边距的方式. 图片来源:w3school.com.cn 元素(element)是一个html文档的实际内容区域,依次由 ...
- Delphi中运行时改变panel的位置及大小(WM_SysCommand消息配合参数使用,很奇妙) good
procedure TForm1.pnl1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Inte ...
- Silverlight下的Socket通讯
http://www.cnblogs.com/chengxingliang/archive/2012/05/24/2515100.html 在Silverlight中进行通讯,只能使用4502-453 ...