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. [amazonaccess 1]logistic.py 特征提取

    ---恢复内容开始--- 本文件对应logistic.py amazonaccess介绍: 根据入职员工的定位(员工角色代码.角色所属家族代码等特征)判断员工是否有访问某资源的权限 logistic. ...

  2. 适合入门自学服装裁剪滴书(更新ing)

    [♣]适合入门自学服装裁剪滴书(更新ing) [♣]适合入门自学服装裁剪滴书(更新ing) 适合入门自学服装裁剪滴书(更新ing) 来自: 裁缝阿普(不为良匠,便为良医.) 2014-04-06 23 ...

  3. hdu4740【杭州网赛、模拟、有点搜索?】

    当时看了这题就感觉so easy...  本来不想写的,后来感觉是不是可以练一下搜索水平.. 比赛时有人过了就没写.       比赛完了写一下. 实现还不是那么顺利,  囧 本来自己以为这题能练下搜 ...

  4. javascript高级知识点——继承

    代码信息来自于http://ejohn.org/apps/learn/. 继承是如何工作的 function Person(){} function Ninja(){} Ninja.prototype ...

  5. web基础-web工作原理,http协议,浏览器缓存

    1,web工作原理 2,http协议 3,浏览器缓存 4,cookie和session -------------------------------------------------------- ...

  6. linux可执行文件目录的区别

    /bin./sbin./usr/bin./usr/sbin目录的区别 在linux下我们经常用到的四个应用程序的目录是/bin./sbin./usr/bin./usr/sbin .而四者存放的文件一般 ...

  7. OpenCV学习(2)--基本数据结构

    OpenCV的基本数据结构 CvPoint:表示图像中的点 CvPoint2D32f:二维空间中的点 CvPoint3D32f:三维空间中的点 这些都是结构体,并不是C++语言中的类,所以他们的构造函 ...

  8. C语言队列的实现

    队列是常用的数据结构之一,下面给出一个链式队列的实现: 头文件Queue.h #ifndef Queue_H #define Queue_H typedef int Item; typedef str ...

  9. C语言——strlen()和sizeof的区别

    strlen()和sizeof()的区别: strlen()——>C字符串库函数,返回字符串的真实长度.它是从内存某位置开始扫描,直到碰到结束符'\0'停止,返回计数器值. sizeof()—— ...

  10. 在Win7的IIS上搭建FTP服务及用户授权——转载!!

    原文地址:http://blog.sina.com.cn/s/blog_6cccb1630100q0qg.html FTP服务 FTP是文件传输协议(File Transfer Protocol)的简 ...