首先设计关于帖子列表的servlet类,来进行页面和逻辑代码之间的分离和控制,这里我直接省略了bo层,权且用servlet来代替bo
com.axbbs.bo 下新建一个 GetPostsList类
内容如下:
package com.axbbs.bo;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.axbbs.Dao.GetPostListDAO;
import com.axbbs.po.PostPo;
/**
* @程序编写 阿汐
* @版本 1.0
* @说明 仿wc的jsp版本小论坛
* @本模块是用于获取帖子列表信息的Servlet类
* @并且功能并不需要太多,因此本版本没有采用传统意义上的MVC开发模式,而是针对
* @vo,bo类进行了精简,用po类来共用vo类,用servlet类来共用bo类,很奇怪的开发模式
* @日期 2008.09.01
*
*/
public class GetPostsList extends HttpServlet {
/**
* Constructor of the object.
*/
public GetPostsList() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType( "text/html");
PrintWriter out = response.getWriter();
//创建一个集合,用来存储帖子列表的各种信息
ArrayList list = new ArrayList();
//创建DAO对象
GetPostListDAO gpld = new GetPostListDAO();
//将DAO执行的ViewList方法所返回的帖子列表信息赋值给list 集合对象
list=gpld.ViewList();
//创建session对象以期将帖子列表信息在页面上显示
HttpSession session = request.getSession();
//设置session viewlist的属性为list对象
session.setAttribute( "viewList", list);
//页面跳转到menu.jsp,也就是帖子列表显示的页面
response.sendRedirect( "menu.jsp");
out.flush();
out.close();
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occure
*/
public void init() throws ServletException {
// Put your code here
}
}
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.axbbs.Dao.GetPostListDAO;
import com.axbbs.po.PostPo;
/**
* @程序编写 阿汐
* @版本 1.0
* @说明 仿wc的jsp版本小论坛
* @本模块是用于获取帖子列表信息的Servlet类
* @并且功能并不需要太多,因此本版本没有采用传统意义上的MVC开发模式,而是针对
* @vo,bo类进行了精简,用po类来共用vo类,用servlet类来共用bo类,很奇怪的开发模式
* @日期 2008.09.01
*
*/
public class GetPostsList extends HttpServlet {
/**
* Constructor of the object.
*/
public GetPostsList() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType( "text/html");
PrintWriter out = response.getWriter();
//创建一个集合,用来存储帖子列表的各种信息
ArrayList list = new ArrayList();
//创建DAO对象
GetPostListDAO gpld = new GetPostListDAO();
//将DAO执行的ViewList方法所返回的帖子列表信息赋值给list 集合对象
list=gpld.ViewList();
//创建session对象以期将帖子列表信息在页面上显示
HttpSession session = request.getSession();
//设置session viewlist的属性为list对象
session.setAttribute( "viewList", list);
//页面跳转到menu.jsp,也就是帖子列表显示的页面
response.sendRedirect( "menu.jsp");
out.flush();
out.close();
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occure
*/
public void init() throws ServletException {
// Put your code here
}
}
开始设计menu.jsp
<%@ page language= "java" import= "java.util.*" pageEncoding= "GBK"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+ "://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
//在jsp页面导入PostPo包,用来进行类型强制转换用
<%@ page import="com.axbbs.po.PostPo" %>
<style type="text/css">
<!--
*{
font-size:12px;
}
body{
font-size: 12px;
background-color: #A9DAFF;
}
a{
TEXT-DECORATION:none;
color:#000000;
}
.STYLE1 {
color: #FF0000;
font-weight: bold;
}
.STYLE4 {color: #000033}
-->
</style>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK" />
<title></title>
</head>
<body>
<div id="head">
<select name="select">
<option value="1">1日内</option>
<option value="2">2日内</option>
<option value="3">3日内</option>
</select>
<select name="select2">
<option>时间</option>
<option>浏览</option>
<option>回复</option>
</select>
<input type="submit" name="Submit" value="ALL" />
<input type="submit" name="Submit2" value="SELF" />
发贴 必读 <span class="STYLE1">关注</span> 刷新</div>
<br/>
<span class="STYLE4">用户在本站所发表言论只代表其个人观点,不代表本站立场. </span><br/>
<br/>
<%
//创建一个ArrayList集合对象用来存储帖子列表的信息
ArrayList arrayList =(ArrayList)session.getAttribute("viewList");
//集合存在的情况下继续
if(arrayList!=null){
//循环读取集合的信息,有很多帖子....
for(int i = 0;i<arrayList.size();i++){
//对集合进行类型强制转换用以便于取值
PostPo pp = (PostPo)arrayList.get(i);
//输出帖子列表的信息
out.print("<a href=GetPostInfo?id="+pp.getPostId()+" target=mainFrame title="+pp.getPosttime()+">◆ "+pp.getPostTitle()+"("+pp.getPostReply()+"/"+pp.getPostHits() + ")"+"</a></br>");
}
}
%>
</body>
</html>
<%@ page import="com.axbbs.po.PostPo" %>
<style type="text/css">
<!--
*{
font-size:12px;
}
body{
font-size: 12px;
background-color: #A9DAFF;
}
a{
TEXT-DECORATION:none;
color:#000000;
}
.STYLE1 {
color: #FF0000;
font-weight: bold;
}
.STYLE4 {color: #000033}
-->
</style>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK" />
<title></title>
</head>
<body>
<div id="head">
<select name="select">
<option value="1">1日内</option>
<option value="2">2日内</option>
<option value="3">3日内</option>
</select>
<select name="select2">
<option>时间</option>
<option>浏览</option>
<option>回复</option>
</select>
<input type="submit" name="Submit" value="ALL" />
<input type="submit" name="Submit2" value="SELF" />
发贴 必读 <span class="STYLE1">关注</span> 刷新</div>
<br/>
<span class="STYLE4">用户在本站所发表言论只代表其个人观点,不代表本站立场. </span><br/>
<br/>
<%
//创建一个ArrayList集合对象用来存储帖子列表的信息
ArrayList arrayList =(ArrayList)session.getAttribute("viewList");
//集合存在的情况下继续
if(arrayList!=null){
//循环读取集合的信息,有很多帖子....
for(int i = 0;i<arrayList.size();i++){
//对集合进行类型强制转换用以便于取值
PostPo pp = (PostPo)arrayList.get(i);
//输出帖子列表的信息
out.print("<a href=GetPostInfo?id="+pp.getPostId()+" target=mainFrame title="+pp.getPosttime()+">◆ "+pp.getPostTitle()+"("+pp.getPostReply()+"/"+pp.getPostHits() + ")"+"</a></br>");
}
}
%>
</body>
</html>
OK
最终我们访问程序就是这样了,这三条数据是我在数据库中手动添加用作测试的.
本文转自阿汐 51CTO博客,原文链接:http://blog.51cto.com/axiii/97508,如需转载请自行联系原作者