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支持多语言的更多相关文章

  1. 如何在 js 代码中使用 jsp 标签或 Java 代码

    JSP 标签还是很方便的,比如 Struts.Spring 等提供给我们的 JSP 标签,可以用它们来获取变量或进行一些计算.比如 struts2 的 <s:url value="/a ...

  2. 初学Java Web(6)——JSP学习总结

    为什么要学习 JSP Servlet 的短板: Servlet 的出现,是为了解决动态输出网页的问题. 虽然这样做目的能达到,但是存在一些缺陷: 在 Servlet 输出网页片段非常恶心 (可读性差, ...

  3. JSP标签的用法

    JSP动作标签: 通过动作标签,程序员可以在JSP页面中把页面的显示功能部分 封装起来,是整个页面更简洁和易于维护 <jsp:useBean> 装载一个将在JSP页面中使用的JavaBea ...

  4. 快速分页:jsp标签pager-taglib

    一:简介 Pager-taglib,支持多种风格的分页显示.实际上它是一个Jsp标签库,为在JSP上显示分页信息而设计的一套标签,通过这些标签的不同的组 合,会形成多种不一样的分页页面,风格各异.它既 ...

  5. java web学习总结(二十七) -------------------JSP标签介绍

    一.JSP标签介绍 JSP标签也称之为Jsp Action(JSP动作)元素,它用于在Jsp页面中提供业务逻辑功能,避免在JSP页面中直接编写java代码,造成jsp页面难以维护. 二.JSP常用标签 ...

  6. java攻城师之路--复习java web之jsp入门_El表达式_JSTL标签库

    JSP 技术掌握:JSP语法 + EL + JSTL 为什么sun推出 JSP技术 ? Servlet 生成网页比较复杂,本身不支持HTML语法,html代码需要通过response输出流输出,JSP ...

  7. 复习java web之jsp入门_El表达式_JSTL标签库

    JSP 技术掌握:JSP语法 + EL + JSTL 为什么sun推出 JSP技术 ? Servlet 生成网页比较复杂,本身不支持HTML语法,html代码需要通过response输出流输出,JSP ...

  8. jsp不解析el表达式,不识别jstl标签,找不到http://java.sun.com/jsp/jstl/core

    问题描述: jsp页面中el表达式,例如:${pageContext.request.contextPath},原样呈现,未被解析. 解决方案: 为jsp页添加page指令如下: <%@ pag ...

  9. js 和 css 中 不能使用 jsp 页面中一些 标签 和 java 代码等,应注意

    js  和 css 中 不能使用 jsp  页面中一些 标签 和 java 代码等,应注意 如 ${ }  <%%>  等

随机推荐

  1. 如何交换a和b两个整数的值,不用额外空间

    这个题貌似完全颠覆一般的Logic:交换两个整数需要一个额外的空间用于保存: t = b; b = a; a  = t; 粗看上去似乎没有办法,但是仔细想一下,既然不能用额外的空间,那么能用的方法就只 ...

  2. 刚子扯谈:源于Chanel的图片描述

    文/刚子 2013年8月9日 北京晴 猴晒猴晒 开片语:真心不知道今天该分享点啥?先扯几句牢骚,我个人认为对朋友也够意思,虽然他们有的时候挺操蛋的,虽然还简称哥们儿,虽然还一起交流,但已经变成了无谓的 ...

  3. Linux学习之十二、命令别名与历史命令

    命令别名配置: alias, unalias 那么需要下达『 ls -al | more 』这个命令,我是觉得很烦啦! 要输入好几个单字!那可不可以使用 lm 来简化呢?当然可以,你可以在命令行下面下 ...

  4. Zookeeper介绍

    Zookeeper是一个分布式的开源系统,目的是为分布式应用提供协调一致性服务. 分布式应用可以在Zookeeper提供的简单原语集之上构造更高层次的服务.比如统一命名服务.状态同步服务.集群管理.分 ...

  5. Python中文字符的理解:str()、repr()、print

    Python中文字符的理解:str().repr().print 字数1384 阅读4 评论0 喜欢0 都说Python人不把文字编码这块从头到尾.从古至今全研究通透的话是完全玩不转的.我终于深刻的理 ...

  6. C#中使用日志类,添加dll时出现错误

    警告 1 未能解析引用的程序集 “log4net, Version=1.2.10.0, Culture=neutral, PublicKeyToken=1b44e1d426115821, proces ...

  7. asp.net预览图片

    Aspx code <table> <tr> <td class="style3"> <asp:Label ID="Label1 ...

  8. 【phpcms-v9】如何实现在含有子栏目的栏目下添加内容?

    对于题目的解释: 假设现在有一个一级栏目 为:栏目1 其下有二级栏目  :栏目1=>栏目11,栏目1=>栏目12,栏目1=>栏目13 同时栏目1下有文章列表 : 栏目1-----文章 ...

  9. php将xml文件转化为数组:simplexml_load_string

    <?php $str = <<<XML <?xml version="1.0" encoding="ISO-8859-1"?> ...

  10. HTML豆ちしき

    HTML文档里所有的空白符(空格,Tab,换行,回车)会被浏览器忽略,唯一的例外是空格,对空格的处理方式是所有连续的空格被当成一个空格,不管有一个,还是两个,还是100个.之所以有这样的规则是因为忽略 ...