| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2165 人关注过本帖
标题:[求助]这里是request的问题吗?封装在ArrayList中的City对象
取消只看楼主 加入收藏
hackboyo
Rank: 1
等 级:新手上路
帖 子:152
专家分:0
注 册:2006-10-27
收藏
 问题点数:0 回复次数:2 
[求助]这里是request的问题吗?封装在ArrayList中的City对象

首先我有三个类,DAO.java(存放对数据的操作方法),DBCity.java(提供City表中各列的get和set 方法),myCon.java(提供对数据库的连接),另外还有一个jsp页面,用来显示,还有一个servlet用来接收页面传来的参数

各类内容如下:
DAO.java
package mypro;
import java.sql.*;
import java.sql.Connection;
import java.util.ArrayList;
import java.io.*;

public class DAO {
public DAO() {
}

public static ArrayList getAllCity(String father) throws Exception{
Connection con =myCon.getCon();
String sql = "select * from city where father = "+father+"";
Statement stm = con.createStatement();
ResultSet rs = stm.executeQuery(sql);
ArrayList list = new ArrayList();
while(rs.next()){
DBCity db = new DBCity();
db.setCityID(rs.getString("cityID"));
db.setCity(rs.getString("city"));
db.setFather(rs.getString("father"));
list.add(db);
}
rs.close();
stm.close();
con.close();
return list;
}

}



DBCity.java
package mypro;

public class DBCity {
private String cityID;
private String city;
private String father;
public DBCity() {
}

public void setCityID(String cityID) {
this.cityID = cityID;
}

public void setCity(String city) {
this.city = city;
}

public void setFather(String father) {
this.father = father;
}

public String getCityID() {
return cityID;
}

public String getCity() {
return city;
}

public String getFather() {
return father;
}

}

myCon.java
package mypro;

import java.sql.DriverManager;
import java.sql.Connection;

public class myCon {
public myCon() {
}
public static Connection getCon() throws Exception{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
Connection con = DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;databasename=52tsrjBBS","sa","");
System.out.println("连接数据库成功");
return con;
}

}

jsp1.jsp

<%@ page contentType="text/html; charset=GBK" %>
<html>
<head>
<title>
jsp1
</title>
<script language="javascript" type="">
function onClick_A(){
<%java.util.ArrayList list = (java.util.ArrayList)request.getAttribute("cityList");
System.out.println("得到传来的list的长度为:"+list.size()); ←-----程序中加这个就报错
%>
}
</script>
</head>
<body bgcolor="#ffffff">
<form action="">
<a href="servlet1?father=130000" onclick="onClick_A()">测试</a>
</form>
</body>
</html>



servlet1.java

package mypro;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;

public class Servlet1 extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GBK";

//Initialize global variables
public void init() throws ServletException {
}

//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
String father = request.getParameter("father");
System.out.println("得到传来的father的值:"+father);
try {
ArrayList list = DAO.getAllCity(father);
if(list!=null){
System.out.println("list的长度为:" + list.size());
request.setAttribute("cityList", list);
RequestDispatcher dis = request.getRequestDispatcher("jsp1.jsp");
dis.forward(request, response);
}else{
ArrayList list1 = new ArrayList();
list1 = DAO.getAllCity("140000");
request.setAttribute("cityList",list1);
RequestDispatcher dis = request.getRequestDispatcher("jsp1.jsp");
dis.forward(request, response);
}
} catch (Exception ex) {
}
out.close();
}

//Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
doGet(request, response);
}

//Clean up resources
public void destroy() {
}
}



流程如:jsp页面提供一超连接,点击后传一参数到servlet1,通过DAO中的方法,得到一个ArrayList对象,在此ArrayList对象中封装的DBCity对象,再通过servlet中用request装这个list发给jsp页面,在jsp页面中取出DBCity及其各列的值

搜索更多相关主题的帖子: City ArrayList request 对象 封装 
2007-08-14 18:48
hackboyo
Rank: 1
等 级:新手上路
帖 子:152
专家分:0
注 册:2006-10-27
收藏
得分:0 
请教各位大虾


先谢了


在线等,急
2007-08-14 18:49
hackboyo
Rank: 1
等 级:新手上路
帖 子:152
专家分:0
注 册:2006-10-27
收藏
得分:0 
以下是引用Gramary在2007-8-14 19:08:41的发言:

你这里出现的错误
是不是空指针异常呢???
好象是这样写的呢

是空指针导常
但就是不知道问题出现在哪里


能说说吗?

2007-08-14 20:54
快速回复:[求助]这里是request的问题吗?封装在ArrayList中的City对象
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017545 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved