使用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 代码等,应注意 如 ${ } <%%> 等
随机推荐
- 【HDU】4923 Room and Moor(2014多校第六场1003)
Room and Moor Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) ...
- [转]Asp.Net MVC使用HtmlHelper渲染,并传递FormCollection参数的陷阱 【转】
在Asp.Net MVC 1.0编程中,我们经常遇见这样的场景,在新建一个对象时候,通过HtmlHelper的方式在View模型中渲染Html控件,当填写完相关内容后,通过Form把需要新建的内容Po ...
- python 网页爬虫+保存图片+多线程+网络代理
今天,又算是浪费了一天了.python爬虫,之前写过简单的版本,那个时候还不懂原理,现在算是收尾吧. 以前对网页爬虫不了解,感觉非常神奇,但是解开这面面纱,似乎里面的原理并不是很难掌握.首先,明白一个 ...
- winform CheckedListBox实现全选/全不选
/全选 private void button3_Click(object sender, EventArgs e) { for (int i ...
- C#中log4net使用方法(一)
Log4net是一个第三方开源组件,它设计的主要目的是组合,生成日志信息,同时将配置保存到各种存储介质或者展现平台中,在实际项目中,Log4net可以保存系统运行情况,可以在系统出现异常时,根据保存的 ...
- Datatable根据多行排序
DataTable dt = new DataTable(); dt.Columns.Add("ID", typeof(int)); dt.Columns.Add("Na ...
- User cannot be resolved to a type
出现 User cannot be resolved to a type 不知道具体问题出在哪里但是我经过将全路径输入并保存后错误消失 将User选上,然后点击保存就可以了! 最后我发现错误消失了!
- Java Tomcat Glassfish Weblogic远程debug(remote debug)
tomcat ./catalina.sh jpda start 这条命令启动tomcat,它就会监听8000端口,等待调试器的连接. 默认监听8000端口,通过设置环境变量JPDA_ADDRESS指定 ...
- mybatis动态sql语句问题
1.关于mybatis的insertintoselect命令未结束问题 添加: useGeneratedKeys="false" 官网的解释是 允许 JD ...
- NOIP2012模拟试题 121105【奶牛排队(tahort)
3.奶牛排队(tahort) [ 问题描述] 奶牛在熊大妈的带领下排成了一条直队. 显然,不同的奶牛身高不一定相同…… 现在,奶牛们想知道,如果找出一些连续的奶牛,要求最左边的奶牛A是最矮的,最右边的 ...