java国际化

自己写的一个比较简单的国际化
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="java.util.*"%>
<%
  ResourceBundle resource=(ResourceBundle)session.getAttribute("resource");
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
   
    
    <title>My JSP 'index.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  
  <body>
  <form>&nbsp; 
    <%=resource.getString("username")%>
    <input type="text" name="name"/><br/>
    
    <%=resource.getString("password")%>
    <input type="password" name="password"/><br/>
    
    <input type="submit" value="<%=resource.getString("submit") %>" />
    <input type="reset" value="<%=resource.getString("reset")%>"/>
    </form>
  </body>
</html>
过滤器:

package com.px.test;


import java.io.IOException;
import java.util.Locale;
import java.util.ResourceBundle;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class ResourceBundleFilter implements Filter {
	String Resource;
	public void destroy() {
		// TODO Auto-generated method stub

	}

	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		HttpServletRequest req=(HttpServletRequest)request;
		HttpServletResponse res=(HttpServletResponse)response;
        Locale locale=req.getLocale();
        ResourceBundle resource=ResourceBundle.getBundle(Resource, locale);
        HttpSession sess=req.getSession();
        sess.setAttribute("resource", resource);
        chain.doFilter(request, response);
        
	}

	public void init(FilterConfig arg0) throws ServletException {
		 Resource=arg0.getInitParameter("Resource");

	}

}
两个properties文件

username=username
password=password
submit=submit
reset=reset

中文properties

username=\u7528\u6237\u540D
password=\u5BC6\u7801
submit=\u63D0\u4EA4
reset=\u91CD\u7F6E

你可能感兴趣的:(java,html,jsp,servlet)