jsp显示数据库中数据的问题,顶者有分! 我的网站是jsp+servlet+javabean的,主页上需要显示数据库中的一个表中的数据,已做了一个bean用来将数据封装到Arraylist,现在我在jsp中使用<usebean>来显示数据,可是这样做,符合mvc吗?如果不符合mvc,那怎么做最好? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 晕,这么大的便宜不检....我顶~~~嘻嘻,你使用struts了么?如果没有,那你这个连c(controller)都没有,所以称不上了。如何做简单,使用当今struts,pring,hibernate之类的框架了......这方面参考一下"天乙社区”吧,国人开源的东东....给你顶 顶一下,顺便提一下,个人不同意楼上的说法。struts只是一个优秀MVC框架,不用struts不等于没用MVC。另:建议楼主还是先理解什么是MVC,然后在看自己有没有用MVC。 我想楼主应该是初学者吧,如果可能的话,先将程序功能实现了,再考虑模式的问题。 顶一个,不断学习,我理解MVC就是在不断的看源码,写代码中明白了,其实大部分问题都是要悟的, 晕。俺.....请使用类似于struts的framework吧......刚开始没有必要看源代码,熟练运用之后,再看源代码,体会更深...呵呵~~~ 在jsp中使用<usebean>来显示数据可以啊要不view中怎么显示数据 你用SERVLET了没有,没有用到SERVLET就不能算MVC了一些数据库的功能你可以放到SERVLET里调用,JSP页面里可以用FUNCTION来提交给SERVLET 因为我做的是一个购物车,有一个存放商品的表,现在需要一个页面来显示商品列表,就这么简单,我觉得奇怪了,好像谁都没遇到过这种需求?--------------------------------------------------------你说的这种情况可以用jsp:useBean 当然也可以直接用Bean bean=new Bean(); 只是显示的话就不用进SERVLET了 MVC三层架构就是理论,自己怎么用着舒服就怎么用,如果LZ是初学,建议只用struts,什么spring和hibernate之类的,先别考虑了,struts最能体现MVC模式了,提交到action上,调用javabean还有就是我个人不太建议用ArrayList,用HashMap吧 可以用userbean 建议先掌握mvc 在学习struts 这样上手很快 只要知道MVC是什么意思,你就会明白了。 看到MV ,没看到C这样做,可以的。 如果你要体现MVC那就要把商品建一个form类来封装商品啊 其实MVC,每个系统里几乎都可以提取出一个MVC框架,只要你那是有展示,有逻辑,有模型的VIEW,BEAN是MODEL,CONTROL,主要是那种思想,规范编程,才会使系统易编写,易维护. 先要能跑起来得程序才是好程序,等你跑起来之后再慢慢改造,可以用struts试试。 其实MVC,每个系统里几乎都可以提取出一个MVC框架,只要你那是有展示,有逻辑,有模型的VIEW,BEAN是MODEL,CONTROL,主要是那种思想,规范编程,才会使系统易编写,易维护.表示贊同 数据封装Beanpackage article.entity;import java.util.Date;public class ArticleBean { private long articleId; private String articleTitle; private String articleContent; private long catalogId; private long userId; private Date createTime; public ArticleBean() { } public String getArticleContent() { return articleContent; } public void setArticleContent(String articleContent) { this.articleContent = articleContent; } public long getArticleId() { return articleId; } public void setArticleId(long articleId) { this.articleId = articleId; } public String getArticleTitle() { return articleTitle; } public void setArticleTitle(String articleTitle) { this.articleTitle = articleTitle; } public long getCatalogId() { return catalogId; } public void setCatalogId(long catalogId) { this.catalogId = catalogId; } public Date getCreateTime() { return createTime; } public void setCreateTime(Date createTime) { this.createTime = createTime; } public long getUserId() { return userId; } public void setUserId(long userId) { this.userId = userId; }} 操作封装Beanpackage common.dao;import java.sql.Connection;import java.sql.ResultSet;import java.sql.PreparedStatement;import java.sql.Statement;import java.sql.DriverManager;import java.sql.SQLException;public class ConnectionBean { //数据库驱动类名 private static final String DRIVER_CLASS = "sun.jdbc.odbc.JdbcOdbcDriver"; //数据库源名 private static final String URL = "jdbc:odbc:JDBCSQLDemo_JSPTest"; //数据库登录用户名 private static final String USER = "sa"; //用户名密码 private static final String PASSWORD = ""; public ConnectionBean() { } //加载驱动类,获得数据库链接 public static Connection getConnection() { Connection con = null; try { Class.forName(DRIVER_CLASS); con = DriverManager.getConnection(URL, USER, PASSWORD); }catch(ClassNotFoundException cnfe){ System.out.println("加载数据库驱动失败"); } catch(SQLException e) { System.out.println("创建数据库连接失败!"); } return con; } //关闭数据库链接 public static void closeConnection(Connection con) { try { if(con != null && !con.isClosed()) { con.close(); } }catch(SQLException e) { e.printStackTrace(); } } //关闭结果集 public static void closeResultSet(ResultSet res) { try { if (res != null) { res.close(); res = null; } } catch (SQLException e) { e.printStackTrace(); } } //关闭预编译statement public static void closeStatement(PreparedStatement pStatement) { try { if (pStatement != null) { pStatement.close(); pStatement = null; } } catch (SQLException e) { e.printStackTrace(); } } //重载closeStatement,关闭statement public static void closeStatement(Statement st) { try { if (st != null) { st.close(); st = null; } } catch (SQLException e) { e.printStackTrace(); } }} 操作封装Bean2package article.dao;import article.entity.ArticleBean;import common.dao.ConnectionBean;import common.entity.PageBean;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.util.ArrayList;import java.util.Date;import java.text.SimpleDateFormat;public class ArticleDBOperation { public ArticleBean getOneArticle(long lngArticleId) { Connection con = ConnectionBean.getConnection(); PreparedStatement pstmt = null; ResultSet rs = null; String strsql = "select * from article_info where article_id = ?"; ArticleBean articleBean = new ArticleBean(); try { pstmt = con.prepareStatement(strsql); pstmt.setLong(1, lngArticleId); rs = pstmt.executeQuery(); if (rs.next()) { articleBean.setArticleId(rs.getLong("article_id")); articleBean.setArticleTitle(rs.getString("article_title") .trim()); articleBean.setArticleContent(rs.getString("article_content") .trim()); articleBean.setCatalogId(rs.getLong("catalog_id")); articleBean.setUserId(rs.getLong("user_id")); articleBean.setCreateTime(rs.getDate("create_time")); } } catch (SQLException ex) { ex.printStackTrace(); } finally { ConnectionBean.closeResultSet(rs); ConnectionBean.closeStatement(pstmt); ConnectionBean.closeConnection(con); } return articleBean; } public ArrayList getPageArticle(PageBean pageBean) { ArrayList articleList = new ArrayList(); Connection con = null; PreparedStatement pStatement = null; ResultSet rs = null; int rows = pageBean.getRowsInPage(); long currentPage = pageBean.getCurrentPage(); try { con = ConnectionBean.getConnection(); String strSql; strSql = "select top " + rows + " * from article_info where article_id not in(select top " + (rows * (currentPage - 1)) + " article_id from article_info order by article_id) order by article_id"; pStatement = con.prepareStatement(strSql); rs = pStatement.executeQuery(); while (rs.next()) { ArticleBean articleBean = new ArticleBean(); articleBean.setArticleId(rs.getLong("article_id")); articleBean.setArticleTitle(rs.getString("article_title")); articleBean.setArticleContent(rs.getString("article_content")); articleBean.setCatalogId(rs.getLong("catalog_id")); articleBean.setUserId(rs.getLong("user_id")); articleBean.setCreateTime(rs.getDate("create_time")); articleList.add(articleBean); } } catch (Exception e) { e.printStackTrace(); } finally { ConnectionBean.closeResultSet(rs); ConnectionBean.closeStatement(pStatement); ConnectionBean.closeConnection(con); } return articleList; } public ArrayList getAllArticle() { ArrayList articleList = new ArrayList(); Connection con = ConnectionBean.getConnection(); PreparedStatement pstmt = null; ResultSet rs = null; String strsql = "select * from article_info"; try { pstmt = con.prepareStatement(strsql); rs = pstmt.executeQuery(); while (rs.next()) { ArticleBean articleBean = new ArticleBean(); articleBean.setArticleId(rs.getLong("article_id")); articleBean.setArticleTitle(rs.getString("article_title") .trim()); articleBean.setArticleContent(rs.getString("article_content") .trim()); articleBean.setCatalogId(rs.getLong("catalog_id")); articleBean.setUserId(rs.getLong("user_id")); articleBean.setCreateTime(rs.getDate("create_time")); articleList.add(articleBean); } } catch (SQLException ex) { ex.printStackTrace(); } finally { ConnectionBean.closeResultSet(rs); ConnectionBean.closeStatement(pstmt); ConnectionBean.closeConnection(con); } // Iterator it = catalogList.iterator(); // while(it.hasNext()){ // CatalogBean catalogBean1 = (CatalogBean) it.next(); // System.out.println(catalogBean1.getCatalogTitle()); // } return articleList; } public int insertOneArticle(ArticleBean articleBean) { int result = 0; Connection con = ConnectionBean.getConnection(); PreparedStatement pstmt = null; String strsql = "insert into article_info values (?,?,?,?,?)"; SimpleDateFormat simpleDateFormat = new SimpleDateFormat( "yyyy-MM-dd hh:mm:ss"); String strCreateTime = simpleDateFormat.format(new Date()); try { pstmt = con.prepareStatement(strsql); pstmt.setString(1, articleBean.getArticleTitle()); pstmt.setString(2, articleBean.getArticleContent()); pstmt.setLong(3, articleBean.getCatalogId()); pstmt.setLong(4, articleBean.getUserId()); pstmt.setString(5, strCreateTime); result = pstmt.executeUpdate(); } catch (SQLException ex) { ex.printStackTrace(); } finally { ConnectionBean.closeStatement(pstmt); ConnectionBean.closeConnection(con); } return result; } public int updateOneArticle(ArticleBean articleBean) { int result = 0; Connection con = ConnectionBean.getConnection(); PreparedStatement pstmt = null; String strsql = "update article_info set article_title = ? , article_content = ? , catalog_id = ? , user_id = ? , create_time = ? where article_id = ?"; SimpleDateFormat simpleDateFormat = new SimpleDateFormat( "yyyy-MM-dd hh:mm:ss"); String strCreateTime = simpleDateFormat.format(new Date()); try { pstmt = con.prepareStatement(strsql); pstmt.setString(1, articleBean.getArticleTitle()); pstmt.setString(2, articleBean.getArticleContent()); pstmt.setLong(3, articleBean.getCatalogId()); pstmt.setLong(4, articleBean.getUserId()); pstmt.setString(5, strCreateTime); pstmt.setLong(6, articleBean.getArticleId()); result = pstmt.executeUpdate(); } catch (SQLException ex) { ex.printStackTrace(); } finally { ConnectionBean.closeStatement(pstmt); ConnectionBean.closeConnection(con); } return result; }} struts 还是 jsf 看准了再学啊 业务处理Servletpackage common.servlet;import java.io.IOException;import java.io.PrintWriter;import java.util.*;import javax.servlet.RequestDispatcher;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import article.dao.ArticleDBOperation;public class GetAllArticleServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=GBK"); PrintWriter out = response.getWriter(); ArrayList articleList = new ArticleDBOperation().getAllArticle(); String strJsp = new String(request.getParameter("strJsp").getBytes("ISO8859_1")).trim();// request.setAttribute("articleList",articleList);// RequestDispatcher rd = request.getRequestDispatcher("../../"+strJsp);// rd.forward(request,response); request.getSession().setAttribute("articleList",articleList); response.sendRedirect("../../"+strJsp); out.flush(); out.close(); }} 显示页面Jsp<%@ page language="java" import="java.util.*" pageEncoding="GBK"%><%@ page import="javax.servlet.jsp.PageContext"%><%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c"%><%String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <base href="<%=basePath%>"> <title>My JSP 'articlelist.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <meta http-equiv="content-type" content="text/html; charset=GBK"> <link rel="stylesheet" type="text/css" href="news_css.css"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <table width=90% align=center border="0" cellpadding="0" cellspacing="0"> <tr> <td height=10 /> </tr> <tr> <td class=pageName_red valign=bottom> <IMG src="images/arrow_down_standard.gif"> 文章列表(点击您需要编辑或删除的新闻标题,再进行操作。) </td> </tr> <tr> <td height=10></td> </tr> <tr> <TD height=80%> <form action="article_add" method="post"> <table width=100% align=center border="1" cellpadding="4" cellspacing="0" bordercolor=#c4c4c4> <tr bgcolor=#DFDFDF> <td align=center width=15%> 序号 </td> <td align=center width=20%> 标题 </td> <td align=center width=65%> 内容提要: </td> </tr> <c:forEach var="articleBean" items="${sessionScope.articleList}" varStatus="status"> <c:choose> <c:when test="${status.count%2 == 0}"> <c:set var="bgcolor" value="#ccffff" scope="page" /> </c:when> <c:otherwise> <c:set var="bgcolor" value="#ffffff" scope="page" /> </c:otherwise> </c:choose> <tr bgcolor=${bgcolor} > <td align=center width=15%> ${status.count} </td> <td align=center width=20%> <a href="article/servlet/GetOneArticleServlet?strJsp=article/articleedit.jsp&articleId=${articleBean.articleId}">${articleBean.articleTitle}</a> </td> <td align=center width=65%> ${articleBean.articleContent} </td> </tr> </c:forEach> </table> </form> </TD> </tr> </table> </body></html> 数据封装Bean-----------ArticleBean----------------对应数据库某一个表的某一条记录操作封装Bean-----------ConnectionBean-------------连接数据库操作封装Bean2----------ArticleDBOperation---------具体某一个表的某一些操作。若是封装并传递的是表的一条记录,则使用一个对应数据封装Bean的实例;若是封装并传递的是表的一组记录,则使用一个集合类(一般是ArrayList)的实例,而这个集合类实例的每一个元素为一个对应数据封装Bean的实例 对于楼主的问题,该类的核心方法为public ArrayList getAllArticle() {}业务处理Servlet--------GetAllArticleServlet-------调用ArticleDBOperation的getAllArticle()方法,并将所得到的集合类实例放入会话属性中,然后传递给显示页面的Jsp 对于楼主的问题,该类的核心代码为 ArrayList articleList = new ArticleDBOperation().getAllArticle(); request.getSession().setAttribute("articleList",articleList); String strJsp = new String(request.getParameter("strJsp").getBytes("ISO8859_1")).trim(); response.sendRedirect("../../"+strJsp);显示页面Jsp------------articlelist.jsp------------使用标准标签库和EL表达式语言直接访问会话属性,并显示Bean属性 对于楼主的问题,该类的核心代码为<c:forEach var="articleBean" items="${sessionScope.articleList}"> *访问会话属性* ... ${articleBean.articleContent} *显示Bean属性* </c:forEach> 启动tomcat报错,求大神解救啊!!! 二级菜单 兼容的问题 求高手解决。。急急急急急急!!! request怎样取得映射后的路径 JAVA操作excel,获得公式值的问题~~~~~ SOS,jsp高手进来看看,帮帮小弟,jsp+db2出现异常错误!!怎么改都没找到问题根源!! 期望彻底解决乱码问题,希望大家支持 jsp 如何实现图片放大,缩小, 求助!!!!!!! java怎么实现地图导航 war包解析提示找不到类,求助 一个关于循环的问题,我无论怎么想就是不理不出思路,那位可以帮帮我?先谢了 jsp生成动态统计图表
我顶~~~嘻嘻,你使用struts了么?如果没有,那你这个连c(controller)都没有,所以称不上了。如何做简单,使用当今struts,pring,hibernate之类的框架了......
这方面参考一下"天乙社区”吧,国人开源的东东....
给你顶
顺便提一下,个人不同意楼上的说法。
struts只是一个优秀MVC框架,不用struts不等于没用MVC。
另:建议楼主还是先理解什么是MVC,然后在看自己有没有用MVC。
我想楼主应该是初学者吧,如果可能的话,先将程序功能实现了,再考虑模式的问题。
请使用类似于struts的framework吧......刚开始没有必要看源代码,熟练运用之后,再看源代码,体会更深...呵呵~~~
可以啊
要不view中怎么显示数据
一些数据库的功能你可以放到SERVLET里调用,JSP页面里可以用FUNCTION来提交给SERVLET
--------------------------------------------------------
你说的这种情况可以用jsp:useBean 当然也可以直接用Bean bean=new Bean(); 只是显示的话就不用进SERVLET了
还有就是我个人不太建议用ArrayList,用HashMap吧
这样做,可以的。
VIEW,BEAN是MODEL,CONTROL,主要是那种思想,规范编程,才会使系统易编写,易维护.
其实MVC,每个系统里几乎都可以提取出一个MVC框架,只要你那是有展示,有逻辑,有模型的
VIEW,BEAN是MODEL,CONTROL,主要是那种思想,规范编程,才会使系统易编写,易维护.
表示贊同
private long articleId; private String articleTitle; private String articleContent; private long catalogId; private long userId; private Date createTime; public ArticleBean() { } public String getArticleContent() {
return articleContent;
} public void setArticleContent(String articleContent) {
this.articleContent = articleContent;
} public long getArticleId() {
return articleId;
} public void setArticleId(long articleId) {
this.articleId = articleId;
} public String getArticleTitle() {
return articleTitle;
} public void setArticleTitle(String articleTitle) {
this.articleTitle = articleTitle;
} public long getCatalogId() {
return catalogId;
} public void setCatalogId(long catalogId) {
this.catalogId = catalogId;
} public Date getCreateTime() {
return createTime;
} public void setCreateTime(Date createTime) {
this.createTime = createTime;
} public long getUserId() {
return userId;
} public void setUserId(long userId) {
this.userId = userId;
}}
package common.dao;import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.PreparedStatement;
import java.sql.Statement;
import java.sql.DriverManager;
import java.sql.SQLException;public class ConnectionBean {
//数据库驱动类名
private static final String DRIVER_CLASS =
"sun.jdbc.odbc.JdbcOdbcDriver";
//数据库源名
private static final String URL = "jdbc:odbc:JDBCSQLDemo_JSPTest";
//数据库登录用户名
private static final String USER = "sa";
//用户名密码
private static final String PASSWORD = "";
public ConnectionBean() {
}
//加载驱动类,获得数据库链接
public static Connection getConnection() {
Connection con = null;
try {
Class.forName(DRIVER_CLASS);
con = DriverManager.getConnection(URL, USER, PASSWORD);
}catch(ClassNotFoundException cnfe){
System.out.println("加载数据库驱动失败");
}
catch(SQLException e) {
System.out.println("创建数据库连接失败!");
}
return con;
}
//关闭数据库链接
public static void closeConnection(Connection con) {
try {
if(con != null && !con.isClosed()) {
con.close();
}
}catch(SQLException e) {
e.printStackTrace();
}
}
//关闭结果集
public static void closeResultSet(ResultSet res) {
try {
if (res != null) {
res.close();
res = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
//关闭预编译statement
public static void closeStatement(PreparedStatement pStatement) {
try {
if (pStatement != null) {
pStatement.close();
pStatement = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
//重载closeStatement,关闭statement
public static void closeStatement(Statement st) {
try {
if (st != null) {
st.close();
st = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
import common.dao.ConnectionBean;
import common.entity.PageBean;import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;import java.util.ArrayList;
import java.util.Date;import java.text.SimpleDateFormat;public class ArticleDBOperation {
public ArticleBean getOneArticle(long lngArticleId) {
Connection con = ConnectionBean.getConnection();
PreparedStatement pstmt = null;
ResultSet rs = null; String strsql = "select * from article_info where article_id = ?";
ArticleBean articleBean = new ArticleBean(); try {
pstmt = con.prepareStatement(strsql);
pstmt.setLong(1, lngArticleId);
rs = pstmt.executeQuery();
if (rs.next()) {
articleBean.setArticleId(rs.getLong("article_id"));
articleBean.setArticleTitle(rs.getString("article_title")
.trim());
articleBean.setArticleContent(rs.getString("article_content")
.trim());
articleBean.setCatalogId(rs.getLong("catalog_id"));
articleBean.setUserId(rs.getLong("user_id"));
articleBean.setCreateTime(rs.getDate("create_time"));
}
} catch (SQLException ex) {
ex.printStackTrace();
} finally {
ConnectionBean.closeResultSet(rs);
ConnectionBean.closeStatement(pstmt);
ConnectionBean.closeConnection(con);
} return articleBean;
} public ArrayList getPageArticle(PageBean pageBean) {
ArrayList articleList = new ArrayList();
Connection con = null;
PreparedStatement pStatement = null;
ResultSet rs = null; int rows = pageBean.getRowsInPage();
long currentPage = pageBean.getCurrentPage(); try {
con = ConnectionBean.getConnection();
String strSql;
strSql = "select top "
+ rows
+ " * from article_info where article_id not in(select top "
+ (rows * (currentPage - 1))
+ " article_id from article_info order by article_id) order by article_id";
pStatement = con.prepareStatement(strSql);
rs = pStatement.executeQuery();
while (rs.next()) {
ArticleBean articleBean = new ArticleBean();
articleBean.setArticleId(rs.getLong("article_id"));
articleBean.setArticleTitle(rs.getString("article_title"));
articleBean.setArticleContent(rs.getString("article_content"));
articleBean.setCatalogId(rs.getLong("catalog_id"));
articleBean.setUserId(rs.getLong("user_id"));
articleBean.setCreateTime(rs.getDate("create_time"));
articleList.add(articleBean);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
ConnectionBean.closeResultSet(rs);
ConnectionBean.closeStatement(pStatement);
ConnectionBean.closeConnection(con);
}
return articleList;
} public ArrayList getAllArticle() {
ArrayList articleList = new ArrayList(); Connection con = ConnectionBean.getConnection();
PreparedStatement pstmt = null;
ResultSet rs = null; String strsql = "select * from article_info"; try {
pstmt = con.prepareStatement(strsql);
rs = pstmt.executeQuery();
while (rs.next()) {
ArticleBean articleBean = new ArticleBean();
articleBean.setArticleId(rs.getLong("article_id"));
articleBean.setArticleTitle(rs.getString("article_title")
.trim());
articleBean.setArticleContent(rs.getString("article_content")
.trim());
articleBean.setCatalogId(rs.getLong("catalog_id"));
articleBean.setUserId(rs.getLong("user_id"));
articleBean.setCreateTime(rs.getDate("create_time"));
articleList.add(articleBean);
}
} catch (SQLException ex) {
ex.printStackTrace();
} finally {
ConnectionBean.closeResultSet(rs);
ConnectionBean.closeStatement(pstmt);
ConnectionBean.closeConnection(con);
} // Iterator it = catalogList.iterator();
// while(it.hasNext()){
// CatalogBean catalogBean1 = (CatalogBean) it.next();
// System.out.println(catalogBean1.getCatalogTitle());
// } return articleList;
} public int insertOneArticle(ArticleBean articleBean) {
int result = 0; Connection con = ConnectionBean.getConnection();
PreparedStatement pstmt = null; String strsql = "insert into article_info values (?,?,?,?,?)"; SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
"yyyy-MM-dd hh:mm:ss"); String strCreateTime = simpleDateFormat.format(new Date()); try {
pstmt = con.prepareStatement(strsql);
pstmt.setString(1, articleBean.getArticleTitle());
pstmt.setString(2, articleBean.getArticleContent());
pstmt.setLong(3, articleBean.getCatalogId());
pstmt.setLong(4, articleBean.getUserId());
pstmt.setString(5, strCreateTime);
result = pstmt.executeUpdate();
} catch (SQLException ex) {
ex.printStackTrace();
} finally {
ConnectionBean.closeStatement(pstmt);
ConnectionBean.closeConnection(con);
}
return result;
} public int updateOneArticle(ArticleBean articleBean) {
int result = 0; Connection con = ConnectionBean.getConnection();
PreparedStatement pstmt = null; String strsql = "update article_info set article_title = ? , article_content = ? , catalog_id = ? , user_id = ? , create_time = ? where article_id = ?"; SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
"yyyy-MM-dd hh:mm:ss"); String strCreateTime = simpleDateFormat.format(new Date()); try {
pstmt = con.prepareStatement(strsql);
pstmt.setString(1, articleBean.getArticleTitle());
pstmt.setString(2, articleBean.getArticleContent());
pstmt.setLong(3, articleBean.getCatalogId());
pstmt.setLong(4, articleBean.getUserId());
pstmt.setString(5, strCreateTime);
pstmt.setLong(6, articleBean.getArticleId());
result = pstmt.executeUpdate();
} catch (SQLException ex) {
ex.printStackTrace();
} finally {
ConnectionBean.closeStatement(pstmt);
ConnectionBean.closeConnection(con);
}
return result;
}
}
import java.io.PrintWriter;
import java.util.*;import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import article.dao.ArticleDBOperation;public class GetAllArticleServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { doPost(request,response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { response.setContentType("text/html;charset=GBK");
PrintWriter out = response.getWriter(); ArrayList articleList = new ArticleDBOperation().getAllArticle();
String strJsp = new String(request.getParameter("strJsp").getBytes("ISO8859_1")).trim();// request.setAttribute("articleList",articleList);
// RequestDispatcher rd = request.getRequestDispatcher("../../"+strJsp);
// rd.forward(request,response);
request.getSession().setAttribute("articleList",articleList);
response.sendRedirect("../../"+strJsp);
out.flush();
out.close();
}
}
<%@ page import="javax.servlet.jsp.PageContext"%>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c"%><%String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'articlelist.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<meta http-equiv="content-type" content="text/html; charset=GBK">
<link rel="stylesheet" type="text/css" href="news_css.css">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head> <body>
<table width=90% align=center border="0" cellpadding="0" cellspacing="0">
<tr>
<td height=10 />
</tr>
<tr>
<td class=pageName_red valign=bottom>
<IMG src="images/arrow_down_standard.gif">
文章列表(点击您需要编辑或删除的新闻标题,再进行操作。)
</td>
</tr>
<tr>
<td height=10></td>
</tr>
<tr>
<TD height=80%>
<form action="article_add" method="post">
<table width=100% align=center border="1" cellpadding="4" cellspacing="0" bordercolor=#c4c4c4>
<tr bgcolor=#DFDFDF>
<td align=center width=15%>
序号
</td>
<td align=center width=20%>
标题
</td>
<td align=center width=65%>
内容提要:
</td>
</tr>
<c:forEach var="articleBean" items="${sessionScope.articleList}" varStatus="status"> <c:choose>
<c:when test="${status.count%2 == 0}">
<c:set var="bgcolor" value="#ccffff" scope="page" />
</c:when>
<c:otherwise>
<c:set var="bgcolor" value="#ffffff" scope="page" />
</c:otherwise>
</c:choose> <tr bgcolor=${bgcolor} >
<td align=center width=15%>
${status.count}
</td>
<td align=center width=20%>
<a href="article/servlet/GetOneArticleServlet?strJsp=article/articleedit.jsp&articleId=${articleBean.articleId}">${articleBean.articleTitle}</a>
</td>
<td align=center width=65%>
${articleBean.articleContent}
</td>
</tr>
</c:forEach> </table>
</form>
</TD>
</tr>
</table>
</body>
</html>
对于楼主的问题,该类的核心方法为public ArrayList getAllArticle() {}业务处理Servlet--------GetAllArticleServlet-------调用ArticleDBOperation的getAllArticle()方法,并将所得到的集合类实例放入会话属性中,然后传递给显示页面的Jsp
对于楼主的问题,该类的核心代码为
ArrayList articleList = new ArticleDBOperation().getAllArticle();
request.getSession().setAttribute("articleList",articleList);
String strJsp = new String(request.getParameter("strJsp").getBytes("ISO8859_1")).trim();
response.sendRedirect("../../"+strJsp);显示页面Jsp------------articlelist.jsp------------使用标准标签库和EL表达式语言直接访问会话属性,并显示Bean属性
对于楼主的问题,该类的核心代码为
<c:forEach var="articleBean" items="${sessionScope.articleList}"> *访问会话属性*
...
${articleBean.articleContent} *显示Bean属性*
</c:forEach>