1.在不同页面或者用户之间共享数据
同一用户,不同页面之间共享数据
*把数据保存在Session中;
1)javaBean的scope="session";
2)session的setAttribute()和getAttribute();
*通过cookie;
1)new Cookie()和response.addCookie();
*通过隐含的表单;
2)<input type="hidden" name="" value=""/>;
*通过ServletContext对象;
1)JSP页面中使用getServletContext()方法获得ServletContext()即application;
2)pageContext.setAttribute(String name, Object value, int scope);
*通过Application对象;
1)application.setAttribute();
*通过文件系统或者数据库;
2.不同用户之间共享数据
*通过ServletContext对象;
*通过Application对象;
*通过文件系统或者数据库;
getServletConfig()
在servlet初始化时,容器传递进来一个ServletConfig对象并保存在servlet实例中,
该对象允许访问两项内容:初始化参数和ServletContext对象,前者通常由容器在文件中指定,
允许在运行时向sevrlet传递有关调度信息,比如说getServletConfig().getInitParameter("debug")
后者为servlet提供有关容器的信息。此方法可以让servlet在任何时候获得该对象及配置信息。
getServletContext()
一个servlet可以使用getServletContext()方法得到web应用的servletContext
即而使用getServletContext的一些方法来获得一些值
比如说getServletContext().getRealPath("/")来获得系统绝对路径
getServletContext().getResource("WEB-INF/config.xml")来获得xml文件的内容
3.创建错误页面
1)使用errorPage和isErrorPage;
2)使用
<error-page>
<error-code>404</error-code>
<location>/pageNotFound.html</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/internalError.html</location>
</error-page>
<error-page>
<error-type>java.lang.NumberFormatException</error-type>
<location>/NumberFormatException,html</location>
</error-page>
4.国际化问题(Internationalization I18N)
1)response.setHeader("Content-Language", "es");设置HTML要显示的语言
JDK中native2ascii工具,可以将汉字等语种转换成UTF-8;
5.乱码问题
1)GB18030>GBK>GB2312;
2)<%@ page contentType="text/html;charset=gb18030"%>
request.setCharacterEncoding("gb18030");
3)
<%! String trans(String chi)
{
String result = null;
byte temp [];
try
{
temp=chi.getBytes("iso-8859-1");
result = new String(temp);
}
catch(UnsupportedEncodingException e)
{
System.out.println (e.toString());
}
return result;
}
%>
6.JSP文件操作
1)使用ServletContext
InputStream in = getServletContext().getResourceAsStream("/file.txt");
int temp = 0;
String file = "";
while((temp = in.read()) != -1) {
file += (char)temp;
}
in.close();
2)使用带缓冲的BufferedReader
BufferedReader buffer = new BuferedReader(new InputStreamReader(new BufferedInputStream(in));
while(buffer.readLine() != null)
3)
BufferedReader = new BufferedReader(new FileReader("c:/as.txt"));
4)写入文件
PrintWriter writer = new PrintWriter(new BufferedWriter(new FileReader("c:/as.txt")));
FileWriter("c:/asd.txt", true);
7.上传文件
1)方法必须是post和enctype="multipart/form-data"
<form action="" method="post" enctype="multipart/form-data"/>
<input type="file" name="file2" size="20">
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<%@ page import="com.jspsmart.upload.*"%>
<jsp:useBean id="mySmartUpload" scope="page" class="com.jspsmart.upload.SmartUpload" />
<html>
<head>
<title>上载附件 </title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body>
<center>正在上传文件...
<%
//上载附件
try
{
mySmartUpload.initialize(pageContext);
mySmartUpload.service(request,response);
mySmartUpload.upload();
String fn=fn=mySmartUpload.getFiles().getFile(0).getFileName();
mySmartUpload.save("upload/");//文件保存的目录为upload
out.println("已经成功上传了文件,请查看<a href=upload/"+fn+">这里</a>,看文件是否上传成功");
}
catch(Exception e)
{
e.printStackTrace();
}
%>
<a href=FileUpload.html>重新上传</a>
</body>
</html>