<%@ page contentType="text/html;charset=gb2312"%>
<html>
<jsp:useBean id="friends" scope="page" class="friend.friend"/>
<title>好友录</title>
<head>
<h1>
我的好友录
</h1>
</head>
<body>
<a href=addfriend.jsp>添加新纪录</a>
<a href=changefriend.jsp>修改选中记录</a>
<a href=deleatefriend.jsp>删除选中记录</a><hr>
</body>
</html>--------------------------------------------------------
java代码:package friend;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class friend extends HttpServlet {
private String[] name;
friend(){
}
friend(String[] info){
name=info;
}
public Vector readFile() {
try{
//先得到文件路径
String fileName=getServletConfig().getServletContext().getRealPath("/myFriend.txt");
//打开文件
FileInputStream fis=new FileInputStream(fileName);
//将字节流转换为字符流
InputStreamReader isr=new InputStreamReader(fis);
//将字符流转换为带有缓冲的字符流
BufferedReader br=new BufferedReader(isr);
Vector friends=new Vector();
String line=br.readLine();
while(line!=null){
//将一行内容分解为各个数据项
String info[]=line.split(",");
//创建封装对象
friend f=new friend(info);
//添加到Vector集合
friends.add(f);
//读下一行
line=br.readLine();
}
return friends;
}
catch(Exception e){
return null;
}
}
}
friend.class存在web-inf下的classes中了
<html>
<jsp:useBean id="friends" scope="page" class="friend.friend"/>
<title>好友录</title>
<head>
<h1>
我的好友录
</h1>
</head>
<body>
<a href=addfriend.jsp>添加新纪录</a>
<a href=changefriend.jsp>修改选中记录</a>
<a href=deleatefriend.jsp>删除选中记录</a><hr>
</body>
</html>--------------------------------------------------------
java代码:package friend;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class friend extends HttpServlet {
private String[] name;
friend(){
}
friend(String[] info){
name=info;
}
public Vector readFile() {
try{
//先得到文件路径
String fileName=getServletConfig().getServletContext().getRealPath("/myFriend.txt");
//打开文件
FileInputStream fis=new FileInputStream(fileName);
//将字节流转换为字符流
InputStreamReader isr=new InputStreamReader(fis);
//将字符流转换为带有缓冲的字符流
BufferedReader br=new BufferedReader(isr);
Vector friends=new Vector();
String line=br.readLine();
while(line!=null){
//将一行内容分解为各个数据项
String info[]=line.split(",");
//创建封装对象
friend f=new friend(info);
//添加到Vector集合
friends.add(f);
//读下一行
line=br.readLine();
}
return friends;
}
catch(Exception e){
return null;
}
}
}
friend.class存在web-inf下的classes中了
这个不是无参构造吗
import java.io.*;
import java.util.*;
import javax.servlet.http.*;
public class Friend extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
private String name;
private String sex;
private String age;
private String qq;
private String mobile;
private String email;
private String address;
public void setName(String name) {
this.name = name;
}
public void setSex(String sex) {
this.sex = sex;
}
public void setAge(String age) {
this.age = age;
}
public void setQq(String qq) {
this.qq = qq;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public void setEmail(String email) {
this.email = email;
}
public void setAddress(String address) {
this.address = address;
}
public String getName() {
return name;
}
public String getSex() {
return sex;
}
public String getAge() {
return age;
}
public String getQq() {
return qq;
}
public String getMobile() {
return mobile;
}
public String getEmail() {
return email;
}
public String getAddress() {
return address;
}
public Friend(){
}
public Friend(String[] info){
name=info[0];
sex=info[1];
age=info[2];
qq=info[3];
mobile=info[4];
email=info[5];
address=info[6];
}
public Vector<Object> readFile() throws Exception {
try{
//先得到文件路径
String fileName=getServletConfig().getServletContext().getRealPath("/myFriend.txt");
//打开文件
FileInputStream fis=new FileInputStream(fileName);
//将字节流转换为字符流
InputStreamReader isr=new InputStreamReader(fis);
//将字符流转换为带有缓冲的字符流
BufferedReader br=new BufferedReader(isr);
Vector<Object> friends=new Vector<Object>();
String line=br.readLine();
while(line!=null){
//将一行内容分解为各个数据项
String info[]=line.split(",");
//创建封装对象
Friend f=new Friend(info);
//添加到Vector集合
friends.add(f);
//读下一行
line=br.readLine();
}
return friends;
}
catch(Exception e){
return null;
}
}
}
代码修改如上,但是依然不好使
报错:The value for the useBean class attribute a.friend is invalid
Friend.javapackage friend;import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;public class Friend extends HttpServlet {
private static final long serialVersionUID = 1L;
private String[] name; public Friend() { } public Friend(String[] info) {
name = info;
} public Vector readFile() {
try {
// 先得到文件路径
String fileName = getServletConfig().getServletContext()
.getRealPath("/myFriend.txt");
// 打开文件
FileInputStream fis = new FileInputStream(fileName);
// 将字节流转换为字符流
InputStreamReader isr = new InputStreamReader(fis);
// 将字符流转换为带有缓冲的字符流
BufferedReader br = new BufferedReader(isr);
Vector friends = new Vector();
String line = br.readLine();
while (line != null) {
// 将一行内容分解为各个数据项
String info[] = line.split(",");
// 创建封装对象
Friend f = new Friend(info);
// 添加到Vector集合
friends.add(f);
// 读下一行
line = br.readLine();
}
return friends;
} catch (Exception e) {
return null;
}
}
}
aa.jsp
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="friend.Friend"%>
<html>
<jsp:useBean id="friends" scope="page" class="friend.Friend"/>
<title>好友录</title>
<head>
<h1>
我的好友录
</h1>
</head>
<body>
<a href=addfriend.jsp>添加新纪录</a>
<a href=changefriend.jsp>修改选中记录</a>
<a href=deleatefriend.jsp>删除选中记录</a><hr>
</body>
</html>
可以出来界面的