使用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>
上一篇:vijosP1413 Valentine’s Present


下一篇:php 数据连接 基础