<%
response.setHeader("Content-Language","es");
out.println("<html><head><title>En Español</title></head>");
out.println("<body>");
out.println("<h3>En espa%ntilde;ol</h3>");
out.println("¡Hola Mundo!");
out.println("</body></html>");
%>
q行l果:
¡Hola Mundo! <% 支持多国语言的Hello World
//中文 //韩国?br />void processKorean()throws Exception
在HTML字符实体表示中,ñ代表? ñ "字符Q用response.setHeader("Content-Language","es");来设|HTML昄时要使用的语a.
Unicode
Unicode字符标准是一个字W编码系l?它支持互相{?处理和显C现在世界上不同语言的文?在Java语言?字符,字符串和标始W在内部使用16位的Unicode 2.0字符集表C?Unicode使用 " \uxxxx" 来表CZ个字W??56个Unicode字符和ISO-8859-1标准(Latin-1)的前256个字W一?在Unicode世界? " ñ " ?" \u00f1 " 表示.看怎么用Unicode来编写西班牙语的Hello World:
<%@page contentType="text/html;charset=ISO-8859-1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"
response.setHeader("Content-Language","es");
out.println("<html><head><title>En Espa\u00f1ol</title></head>");
out.println("<body>");
out.println("<h3>En espa\u00f1ol</h3>");
out.println("\u00f1Hola Mundo!");
out.println("</body></html>");
%>
用语选择语言的页?
语言处理面代码:
<%@page contentType="text/plain;charset=UTF-8"
import="java.io.*,java.text.*,java.util.*,javax.servlet.jsp.*" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%!
Locale locale;
DateFormat format;
JspWriter writer;
%>
<%!
//p
void processEnglish()throws Exception
{
locale=new Locale("en","US");
format=DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG,locale);
writer.println("in english");
writer.println("<br>");
writer.println("HelloWorld");
writer.println(format.format(new Date()));
writer.flush();
}
void processChinese()throws Exception
{
locale=new Locale("zh","");
format=DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG,locale);
writer.println("in Chinese");
writer.println("<br>");
writer.println("\u4f60\u597d\u4e16\u754c");
writer.println(format.format(new Date()));
writer.flush();
}
{
locale=new Locale("ko","");
format=DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG,locale);
writer.println("in Korean");
writer.println("<br>");
writer.println("\uc548\ud558\uc138\uacc4");
writer.println(format.format(new Date()));
writer.flush();
}
//.........................其他的语a省略
%>
<%
//获得h的语U?br /> String language=(String)request.getParameter("language");
int lan=Integer.parseInt(language);
%>
<%
writer=out;
switch(lan)
{
case 1:processEnglish();break;
case 2:processChinese();break;
case 3:processKorean();break;
//case 4:processRussian();break;
//case 5:processSpanish();break;
// case 6:processJapanese();break;
}
%>
</body>
</html>
处理的思\很简?首先获得h的语U?然后Ҏ不同的语a来做不同的处?
]]>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>JSP Page</title>
</head>
<body>
<h1>Login Checking Page</h1>
<%
String name=request.getParameter("name");
String password=request.getParameter("password");
//Connection the Database,loading
//int logCount=resultSet.getInt("count");
//java.util.Date lastLoginTime=resultSet.getDate("LastLoginTime");
//q里单设|logCount和lastLoginTime的?br /> UserSession user=new UserSeesion();
user.setUserId(name);
user.setIsLogin(true);
user.setLastLoginTime(new java.util.Date());
user.setLogCount(10);
session.setAttribute("userSession",user)
response.sendRedirect("welcome.jsp");
%>
</body>
</html>
整个登陆面的过E是q样?
(1)获得用户的登陆信?br />(2)q接数据库进行权限验?br />(3)如果通过验证,那么d用户的注册信?br />(4)把用L注册信息保存C个userSession对象?br />(5)把userSession对象保存到Session内徏对象?br />(6)把视图派发到下一个显C页?br />注意:session.setAttribute("userSession",user)把userSession的一个对象设|到Session?Session只能保存对象,不能保存原始的数据类?比如:
session.setAttribute("count",10)
是非法的语句,如果要把gؓ10的整C存到Session?需要用以下的Ҏ:
session.setAttribute("count",new Integer(10));
然后在另一个页面?br />(Integer)session.getAttribute("count");
把这个整数读出来.
我们用如下方法在另一个页面中把userSesseion对象d出来:
<%@page contentType="text/html;charset=gb2312" language="java"
import="java.sql.*,dory.*" errorPage=""%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
UserSession user=(UserSession)session.getAttribute("userSession");
try
{
if(user.isLogin())
{
out.print("welcome,your login id is:"+user.getUserId());
out.print("your last login time is:"+user.getLastLoginTime());
out.print("now you are the:"+user.getLogCount()+"times logging this website");
}
else
{
response.sendRedirect("login.html");
}
}
catch(Exception e)
{
response.sendRedirect("login.html");
}
%>
</body>
</html>
可以看出,通过UserSession user=(UserSession)session.getAttribute("userSession");代码来读取在前一个页面中讄的对?然后再从q个对象d一些相兛_?当然我们也可以用JavaBean的Ş式来d.
2.使用隐含菜单
q种方式通过隐含菜单的Ş式把数据传递到下一个页?它有两个局限?
.只能在相ȝ两个面之间传递数?br />.客户端可以用查看网|代码的方式获得表单中的数?安全性不?br />它的实现很简?
<form action="target.jsp">
<input type="hidden" name="test" value="abc">
<input type="hidden" name="test2" value="def">
</form>
在另外一个页面中,通过q样来获得数?
String test=request.getParameter("test");
String test2=request.getParameter("test2");
3.使用Cookie
和Session不同,Cookie是放在客L?׃客户考虑到安全应素可能会用cookie,q样在用cookie׃遇到ȝ?
b.在不同的用户之间׃n数据
在不同的在不同的用户之间׃n数据最常见的方法是使用ServletContext和application对象,通过在一个用户那里设|属性在另一个用户那里获得这个属?
1.使用ServletContext
在JSP面中可以通过getServletContext()Ҏ获得ServletContext对象.在这U情况下不同的用户通过它来工n数据,看下面的实现代码:
<%@page contentType="text/html;charset=gb2312" language="java"
import="java.sql.*,javax.servlet.*,javax.servlet.http.*,dory.*" errorPage="" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"<%
request.setCharacterEncoding("gb2312");
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>JSP Page</title>
</head>
<body>
a simple chatting room
<br><hr><font color="red">
<%
String content=(String)getServletContext().getAttribute(new String("chatTopic_1"));
out.print(content);
getServletContext().setAttribute("chatTopic_1",content+(String)request.getParameter("content")
+"<br>");
%>
</font>
<hr>
<form accept="Servelt Context_chat.jsp">
<input type="text" name="content">
<input type="submit" value="speak">
</form>
</body>
</html>
2.application对象
application对象对应于每个web应用来说只有一?它用和ServletContext差不?如下:
<%@page contentType="text/html;charset=gb2312" language="java"
import="java.sql.*,javax.servlet.*,javax.servlet.http.*,dory.*" errorPage="" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"<%
request.setCharacterEncoding("gb2312");
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>JSP Page</title>
</head>
<body>
a simple chatting room
<br><hr><font color="red">
<%
String content=(String)application.getAttribute(new String("chatTopic_1"));
out.print(content);
application.setAttribute("chatTopic_1",content+(String)request.getParameter("content")
+"<br>");
%>
</font>
<hr>
<form accept="Servelt Context_chat.jsp">
<input type="text" name="content">
<input type="submit" value="speak">
</form>
</body>
</html>
可以得到ServletContext和application的实现机制基本上一?