jsp技术

〖摘要:〗
jsp操作文件:读取文件和写入文件,下面分别介绍一.读取文件,二.写入文件

一。读取文件
1。使用ServletContext,只能读取Servlet上下文中的文件。
2。java.io.FileReader对象,可以读取任何位置的文件。
下面我们介绍两种方式的编码:

1。使用ServletContext,只能读取Servlet上下文中的文件。
在ServletContext接口中定义了java.io.InputStream ,getsourceAsStream(java.lang.String path)来访问给定的资源。使用了InputStream对象后对字符要进行重新编码。请看下面的readFile.jsp文件。



readFile.jsp
<%@ page contentType="text/html; charset=gb2312" import="java.io.*"%>

<%
try
{
//使用ServletContext装入文件资源
InputStream in=getServletContext().getResourceAsStream(
"/file.txt");//获取给定的资源
String file=
"";
int temp=0;
while((temp=in.read())!=-1)
{
file+=(char)temp;
}
//关闭输入流。
in.close();
out.println(new String(file.getBytes(
"iso-8859-1"))); //重新编码
out.flush();
}
catch(Exception e)
{
out.println(e);
e.printStackTrace();
}
%>
<------------------------->
为提高速度,减少占用资源,避免出现中文问题,我们使用带缓冲的输入流来读取文件资源。让我们来看readFile2.jsp文件


readFile2.jsp
<%@ page contentType=
"text/html; charset=gb2312" import="java.io.*" buffer="64kb"%>
<%
try
{
InputStream in=getServletContext().getResourceAsStream(
"/file.txt");
String file=
"";
String temp=
"";

BufferedReader buffer=new BufferedReader(new InputStreamReader(new BufferedInputStream(in)));
while((temp=buffer.readLine())!=null)
{
file+=temp;
}
buffer.close();
in.close();
out.println(file);
out.flush();
}
catch(Exception e)
{
out.println(e);
e.printStackTrace();
}
%>


-------------------------------------------------------------------------------------------------
2。使用java.io.FileReader对象,可以读取任何位置的文件。

<%@ page contentType=
"text/html; charset=gb2312" import="java.io.*" buffer="64kb"%>
<%
try
{
BufferedReader in=new BufferedReader(new FileReader(
"c:\\UserSession.java"));
String file=
"";
String temp=
"";
while((temp=in.readLine())!=null)
{
file+=temp;
}
in.close();
out.println(file);
out.flush();
}
catch(Exception e)
{
out.println(e);
e.printStackTrace();
}
%>




二.写入文件
1。使用java.io.FileReader对象
2。使用FileOutputStream
下面让我们来看代码:
<%@ page contentType=
"text/html; charset=gb2312" import="java.io.*"%>
<html>
<body>
<%
request.setCharacterEncoding(
<
继续阅读其余的  1339 字
air_tuyh   2005-10-09 18:52:49 阅读:2026  评论:0  引用:0
〖摘要:〗
servlet上下文监听

servlet上下文:在服务器上使用Session对象来维持同单一客户相关的状态,为多个用户的Web应用维持一个状态时使用Servlet环境(Context).
常用方法:getAttribute(Stringname),getContext(Stringuripath),removeAttribute(String name),setAttribute(String name,Object object)


监听ServletContext的信息:
实现ServletContextListener,ServletContextAttributeListener接口,以便可以监听ServletContext创建,销毁以及它的属性的变化的信息。并且通过private void logout(String message)方法把信息打印到c:\\test.txt文件中。

ServletContext初始化是在服务器启动时,销毁在关闭时。


下面让我们来看以下MyServletContextListener.java这个程序来深入了解以上的知识点。



package com.jspdev.ch8;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;
import java.io.*;


public final class MyServletContextListener
implements ServletContextListener,ServletContextAttributeListener {

private ServletContext context = null;

/**
*以下代码实现ServletContextListener接口。
*/

public void contextDestroyed(ServletContextEvent sce) {

logout(
"contextDestroyed()-->ServletContext被销毁");
this.context = null;

}

public void contextInitialized(ServletContextEvent sce) {

this.context = sce.getServletContext();
logout(
"contextInitialized()-->ServletContext初始化了");

}
//ServletContextListener

/**
*以下代码实现 ServletContextAttributeListener接口
*/

public void attributeAdded(ServletContextAttributeEvent scae) {

logout(
"增加了一个ServletContext属性:attributeAdded('" + scae.getName() + "', '" +
scae.getValue() +
"')");

}

public void attributeRemoved(ServletContextAttributeEvent scae) {

logout(
"删除了一个ServletContext属性:attributeRemoved('" + scae.getName() + "', '" +
scae.getValue() +
"')");

}


public void attributeReplaced(ServletContextAttributeEvent scae) {

logout(
"某个ServletContext的属性被改变:attributeReplaced('" + scae.getName() + "', '" +
scae.getValue() +
"')");

}

private void logout(String message) {

PrintWriter out=null;
try
{
out=new PrintWriter(new FileOutputStream(
"c:\\test.txt",true));
out.println(new java.util.Date().toLocaleString()+
"::Form ContextListener: " + message);
out.close();
}
catch(Exception e)
{
out.close();
e.printStackTrace();
}

}

}





<--------------------------------------->
部署这个监听器
web.xml

<web-app>
<li
继续阅读其余的  1826 字
air_tuyh   2005-09-21 21:41:18 阅读:2762  评论:4  引用:0
jsp中错误处理页面


举例说明:mustBeError.jsp
<%@ page contentType=
"text/html; charset=gb2312"
language=
"java" import="java.sql.*,javax.servlet.*,javax.servlet.http.*" errorPage="error.jsp" %>
<%
//这个页面一定会出错。
int i=0;
int j=1;
out.println(j/i);
%>


在此中通过errorPage=
"error.jsp"来指定出错时错误处理页面


<--------------------------->
error.jsp

<%@ page contentType=
"text/html; charset=gb2312" language="java" isErrorPage="true" import="java.io.*"%>
<html>
<head>
<title>出错了!</title>
<meta http-equiv=
"Content-Type" content="text/html; charset=gb2312">
</head>
<body>
出错了!<br>
发生了以下的错误:
<br><hr><font color=red><hr>
getMessage():<br>
<%=exception.getMessage()%><br><hr>
getLocalizedMessage():<br>
<%=exception.getLocalizedMessage()%><br><hr>
PrintStatckTrace():<br>
<%
StringWriter sw=new StringWriter();
PrintWriter pw=new PrintWriter(sw);
exception.printStackTrace(pw);
out.println(sw);
%><br>
</font></body>
</html>


在error.jsp中,page指令中指定:isErrorPage=
"true"

<---------------------->
在web.xml中声明异常和错误页面

例如:
<?xml version=
"1.0" encoding="ISO-8859-1"?>

<web-app xmlns=
"http://java.sun.com/xml/ns/j2ee"
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://java.sun.com/xml/ns/j2ee web-app_2_4.xsd"
version=
"2.4">
<error-page>
<error-code>404</error-code>
<location>/pageNotFound.html</location>
</error-page>

<error-page>
<exception-type>java.lang.NumberFormatException</exception-type>
<location>/NumberFormatException.html</location>
</error-page>

</web-app>

说明:指定错误代码为404是,调用/pageNotFound.html
也可以指定错误类型如:java.lang.NumberFormatException,出现此错误类型是调用/NumberFormatException.html

air_tuyh   2005-09-17 21:35:24 阅读:3983  评论:1  引用:0
〖摘要:〗
JSP中<jsp:setProperty>,<jsp:getProperty>,<jsp:useBean>的使用简单事例。


包括:TestBean.java register.jsp register.html
说明:register.jsp register.html放入 tomcat安装目录\tomcat\webapps\ch3

TestBean.java编译后的class文件放入 tomcat安装目\tomcat\webapps\ch3\WEB-INF\classes\com\jspdev\ch3


三个元素的具体使用很容易理解但是却不好用文字解说,在这我就不具体说明了。通过实际操作事例后具体分析就可明白。


<----------------------------->

register.html

<html>
<body>
用户信息注册:<br><hr>
<form method=
"get" action="register.jsp">
<table>
<tr><td>姓名:<input name=
"userName" type="text"></td></tr>
<tr><td>密码:<input name=
"password" type="password"></td></tr>
<tr><td>年龄:<input name=
"age" type="text"></td></tr>
<tr><td><input type=submit value=
"submit"></td></tr>
</table>
</form>
</body>
</html>

<----------------------------------------------------->

register.jsp


<%@ page contentType=
"text/html;charset=gb2312"%>
<jsp:useBean id=
"user" scope="page" class="com.jspdev.ch3.TestBean"/>
<jsp:setProperty name=
"user" property="userName" value="UserName"/>
<jsp:setProperty name=
"user" property="password" param="password"/>
<jsp:setProperty name=
"user" property="age" param="age"/>
<html>
<body>
注册成功:<br>
<hr>
使用bean属性方法:<br>
用户名:<%=user.getUserName() %><br>
密码:<%=user.getPassword() %><br>
年龄:<%=user.getAge()%><br>
<hr>
使用getProperty:<br>
用户名:<jsp:getProperty name=
"user" property="userName"/><br>
密码:<jsp:getProperty name=
"user" property="password"/><br>
年龄:<jsp:getProperty name=
"user" property="age"/><br>
</body>
</html>



<------------------------------------------------>

TestBean.java


package com.jspdev.ch3;

public class TestBean
{
public String userName;
public String password;
public int age;

public void setUserName(String name)
{
this.userName=name;
}
public void setPassword(String password)

{
this.password=password;
}
public
继续阅读其余的  697 字
air_tuyh   2005-09-09 18:53:44 阅读:2232  评论:0  引用:0
Copyright@2006 powered by YuLog