OK,下面开始进行帖子信息获取的servlet设计
package com.axbbs.bo;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.servlet.RequestDispatcher;
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.Dao.GetRePosDAO;
/**
* @程序编写 阿汐
* @版本 1.0
* @说明 仿wc的jsp版本小论坛
* @本模块是用于获取帖子内容信息的Servlet类
* @并且功能并不需要太多,因此本版本没有采用传统意义上的MVC开发模式,而是针对
* @vo,bo类进行了精简,用po类来共用vo类,用servlet类来共用bo类,很奇怪的开发模式
* @我都开始佩服起自己来了 050
* @日期 2008.09.01
*
*/
public class GetPostInfo extends HttpServlet {
/**
* Constructor of the object.
*/
public GetPostInfo() {
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();
GetPostListDAO gpld = new GetPostListDAO();
list=gpld.getPost(request.getParameter( "id"));
//得到回帖信息
ArrayList reList = new ArrayList();
GetRePosDAO grpd = new GetRePosDAO();
reList=grpd.getPost(request.getParameter( "id"));
//HttpSession session = request.getSession();
HttpSession session = request.getSession();
session.setAttribute( "viewList", list);
session.setAttribute( "reviewList", reList);
RequestDispatcher requestDispatcher=request.getRequestDispatcher( "viewThread.jsp");
requestDispatcher.forward(request,response);
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 {
response.setContentType( "text/html");
PrintWriter out = response.getWriter();
out
.println( "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the POST method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
/**
* 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.RequestDispatcher;
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.Dao.GetRePosDAO;
/**
* @程序编写 阿汐
* @版本 1.0
* @说明 仿wc的jsp版本小论坛
* @本模块是用于获取帖子内容信息的Servlet类
* @并且功能并不需要太多,因此本版本没有采用传统意义上的MVC开发模式,而是针对
* @vo,bo类进行了精简,用po类来共用vo类,用servlet类来共用bo类,很奇怪的开发模式
* @我都开始佩服起自己来了 050
* @日期 2008.09.01
*
*/
public class GetPostInfo extends HttpServlet {
/**
* Constructor of the object.
*/
public GetPostInfo() {
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();
GetPostListDAO gpld = new GetPostListDAO();
list=gpld.getPost(request.getParameter( "id"));
//得到回帖信息
ArrayList reList = new ArrayList();
GetRePosDAO grpd = new GetRePosDAO();
reList=grpd.getPost(request.getParameter( "id"));
//HttpSession session = request.getSession();
HttpSession session = request.getSession();
session.setAttribute( "viewList", list);
session.setAttribute( "reviewList", reList);
RequestDispatcher requestDispatcher=request.getRequestDispatcher( "viewThread.jsp");
requestDispatcher.forward(request,response);
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 {
response.setContentType( "text/html");
PrintWriter out = response.getWriter();
out
.println( "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the POST method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occure
*/
public void init() throws ServletException {
// Put your code here
}
}
最后一部,现在已经能够拿到关于帖子的主贴和回帖信息了,那么我们就在jsp页面中去读就OK了。 viewThread.jsp的设计
<%@ page language=
"java"
import=
"java.util.*" pageEncoding=
"GBK"%>
<%@ page import= "com.axbbs.po.PostPo" %>
<%@ page import= "com.axbbs.po.RePostPo" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+ "://"+request.getServerName()+":"+request.getServerPort()+path+"/";
/**主贴信息定义**/
String PostTitle="";
String PostContent="";
String PostUserName="";
String PostTime="";
/**主贴信息定义结束**/
/**回帖信息定义**/
String rePostUserName="";
String rePostContent="";
String rePostImgPath="";
String rePostFace="";
int rePostIsHide;
int rePostFlag;
/**回帖信息定义结束**/
%>
<!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" />
<LINK href="css/right.css" rel=stylesheet>
<SCRIPT language=JavaScript src="js/face2.js"></SCRIPT>
<SCRIPT>
<!--
function gosubmit(){
if (document.revert.txtContent.value.length==0){
alert("请填好内容!");
document.revert.txtContent.focus();
return false;
}
return true;
}
//匿名查看
function desAnonymous(){
return confirm('查看匿名将花费500MP。确定要查看吗?');
}
-->
</SCRIPT>
</head>
<body >
<%
ArrayList arrayList =(ArrayList)session.getAttribute("viewList");
if(arrayList!=null){
for(int i = 0;i<arrayList.size();i++){
PostPo pp = (PostPo)arrayList.get(i);
//主贴信息
PostTitle=pp.getPostTitle();
PostContent=pp.getPostContent();
PostUserName=pp.getPostUserName();
PostTime=pp.getPosttime();
//回帖信息
}
}
else{
return ;
}
%>
<SCRIPT src="js/setBG.js"></SCRIPT>
<P><%=PostTitle %> <A title=加为关注帖 style="CURSOR: hand"
οnclick=document.focusForm282868.submit(); target=focusIframe>【关注】</A>
<HR>
<%=PostContent%><BR>
<BR><BR>---<%=PostUserName %><BR>(发帖时间:<%=PostTime %>)<BR><BR><br/>
<%
//回帖内容
ArrayList reArrayList =(ArrayList)session.getAttribute("reviewList");
if(reArrayList!=null){
int num=1;
for(int j=0;j<reArrayList.size();j++){
RePostPo rpp = (RePostPo)reArrayList.get(j);
rePostUserName=rpp.getPostReUserName();
rePostContent=rpp.getPostReContent();
rePostImgPath=rpp.getPostReImgPath();
rePostIsHide=rpp.isPostReIsHide();
rePostFlag=rpp.getPostReFlag();
rePostFace=rpp.getPostReFace();
//楼层
out.print("回答("+ num +"):");
//内容
out.print(rePostContent);
//包子
if(rePostFace.equals("")!=true && rePostFace!=null ){
out.print("<img src=/face/"+rePostFace+">"+"<br/>");
}
//图片
if(rePostImgPath!=""){
out.print("<br/><img src="+rePostImgPath+">"+"<br/>");
}
//回帖人
out.print("---"+rePostUserName+"<br/><br/>");
num++;
}
}
%>
<HR/>
<FORM name=revert action=<%=path%>/ActionPost method=post target=mainFrame
encType=multipart/form-data><INPUT type=hidden value=282868 name=id>
<P>帖子内容:<TEXTAREA name="txtContent" rows=4 cols=35></TEXTAREA><A id=show
name=show></A><BR>相关图片:<INPUT size=36 value="http://" name=txtInterfixImg><BR>上传图片:<INPUT type=file name=txtImageFile><BR>(jpg/gif
200K以内,严禁使用上传图片功能反复上传签名图!违者删除ID!)<BR>我要举旗: <INPUT type=radio CHECKED value=no
name=flag>无 <INPUT type=radio value=0 name=flag><IMG
src="p_w_picpaths/redFlag.gif" width="17" height="16">
<INPUT type=radio value=1 name=flag><IMG
src="p_w_picpaths/greenFlag.gif" width="17" height="16"> (需要200MP)<BR>
<SELECT οnchange=showimg()
size=1 name=face1> <OPTION value=0 selected>0</OPTION> <OPTION
value=1>1</OPTION> <OPTION value=2>2</OPTION> <OPTION value=3>3</OPTION>
<OPTION value=4>4</OPTION> <OPTION value=5>5</OPTION> <OPTION
value=6>6</OPTION> <OPTION value=7>7</OPTION> <OPTION value=8>8</OPTION>
<OPTION value=9>9</OPTION></SELECT> <SELECT οnchange=showimg() size=1
name=face2> <OPTION value=0 selected>0</OPTION> <OPTION value=1>1</OPTION>
<OPTION value=2>2</OPTION> <OPTION value=3>3</OPTION> <OPTION
value=4>4</OPTION> <OPTION value=5>5</OPTION> <OPTION value=6>6</OPTION>
<OPTION value=7>7</OPTION> <OPTION value=8>8</OPTION> <OPTION
value=9>9</OPTION></SELECT> <SELECT οnchange=showimg() size=1 name=face3>
<OPTION value=0 selected>0</OPTION> <OPTION value=1>1</OPTION> <OPTION
value=2>2</OPTION> <OPTION value=3>3</OPTION> <OPTION value=4>4</OPTION>
<OPTION value=5>5</OPTION> <OPTION value=6>6</OPTION> <OPTION
value=7>7</OPTION> <OPTION value=8>8</OPTION> <OPTION
value=9>9</OPTION></SELECT> <A href="http://img.mop.com/static/face.html"
target=_blank>查看</A> <INPUT type=checkbox value=true name=anonymous/>匿名(需要300MP) <INPUT
type=hidden value=282868 name=subId /> <INPUT onClick="return gosubmit();" type=submit value=提交 name=B1/></P></FORM>
<HR/>
<SCRIPT>
//查看楼主匿名
function desAnonymousForMaster(obj){
if(confirm('查看匿名将花费500MP。确定要查看吗?')){
obj.href ='desAnonymous.jsp?wcId=282868&floorNum=0';
return true;
}
else{
return false;
}
}
</SCRIPT>
</BODY>
</html>
<%@ page import= "com.axbbs.po.PostPo" %>
<%@ page import= "com.axbbs.po.RePostPo" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+ "://"+request.getServerName()+":"+request.getServerPort()+path+"/";
/**主贴信息定义**/
String PostTitle="";
String PostContent="";
String PostUserName="";
String PostTime="";
/**主贴信息定义结束**/
/**回帖信息定义**/
String rePostUserName="";
String rePostContent="";
String rePostImgPath="";
String rePostFace="";
int rePostIsHide;
int rePostFlag;
/**回帖信息定义结束**/
%>
<!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" />
<LINK href="css/right.css" rel=stylesheet>
<SCRIPT language=JavaScript src="js/face2.js"></SCRIPT>
<SCRIPT>
<!--
function gosubmit(){
if (document.revert.txtContent.value.length==0){
alert("请填好内容!");
document.revert.txtContent.focus();
return false;
}
return true;
}
//匿名查看
function desAnonymous(){
return confirm('查看匿名将花费500MP。确定要查看吗?');
}
-->
</SCRIPT>
</head>
<body >
<%
ArrayList arrayList =(ArrayList)session.getAttribute("viewList");
if(arrayList!=null){
for(int i = 0;i<arrayList.size();i++){
PostPo pp = (PostPo)arrayList.get(i);
//主贴信息
PostTitle=pp.getPostTitle();
PostContent=pp.getPostContent();
PostUserName=pp.getPostUserName();
PostTime=pp.getPosttime();
//回帖信息
}
}
else{
return ;
}
%>
<SCRIPT src="js/setBG.js"></SCRIPT>
<P><%=PostTitle %> <A title=加为关注帖 style="CURSOR: hand"
οnclick=document.focusForm282868.submit(); target=focusIframe>【关注】</A>
<HR>
<%=PostContent%><BR>
<BR><BR>---<%=PostUserName %><BR>(发帖时间:<%=PostTime %>)<BR><BR><br/>
<%
//回帖内容
ArrayList reArrayList =(ArrayList)session.getAttribute("reviewList");
if(reArrayList!=null){
int num=1;
for(int j=0;j<reArrayList.size();j++){
RePostPo rpp = (RePostPo)reArrayList.get(j);
rePostUserName=rpp.getPostReUserName();
rePostContent=rpp.getPostReContent();
rePostImgPath=rpp.getPostReImgPath();
rePostIsHide=rpp.isPostReIsHide();
rePostFlag=rpp.getPostReFlag();
rePostFace=rpp.getPostReFace();
//楼层
out.print("回答("+ num +"):");
//内容
out.print(rePostContent);
//包子
if(rePostFace.equals("")!=true && rePostFace!=null ){
out.print("<img src=/face/"+rePostFace+">"+"<br/>");
}
//图片
if(rePostImgPath!=""){
out.print("<br/><img src="+rePostImgPath+">"+"<br/>");
}
//回帖人
out.print("---"+rePostUserName+"<br/><br/>");
num++;
}
}
%>
<HR/>
<FORM name=revert action=<%=path%>/ActionPost method=post target=mainFrame
encType=multipart/form-data><INPUT type=hidden value=282868 name=id>
<P>帖子内容:<TEXTAREA name="txtContent" rows=4 cols=35></TEXTAREA><A id=show
name=show></A><BR>相关图片:<INPUT size=36 value="http://" name=txtInterfixImg><BR>上传图片:<INPUT type=file name=txtImageFile><BR>(jpg/gif
200K以内,严禁使用上传图片功能反复上传签名图!违者删除ID!)<BR>我要举旗: <INPUT type=radio CHECKED value=no
name=flag>无 <INPUT type=radio value=0 name=flag><IMG
src="p_w_picpaths/redFlag.gif" width="17" height="16">
<INPUT type=radio value=1 name=flag><IMG
src="p_w_picpaths/greenFlag.gif" width="17" height="16"> (需要200MP)<BR>
<SELECT οnchange=showimg()
size=1 name=face1> <OPTION value=0 selected>0</OPTION> <OPTION
value=1>1</OPTION> <OPTION value=2>2</OPTION> <OPTION value=3>3</OPTION>
<OPTION value=4>4</OPTION> <OPTION value=5>5</OPTION> <OPTION
value=6>6</OPTION> <OPTION value=7>7</OPTION> <OPTION value=8>8</OPTION>
<OPTION value=9>9</OPTION></SELECT> <SELECT οnchange=showimg() size=1
name=face2> <OPTION value=0 selected>0</OPTION> <OPTION value=1>1</OPTION>
<OPTION value=2>2</OPTION> <OPTION value=3>3</OPTION> <OPTION
value=4>4</OPTION> <OPTION value=5>5</OPTION> <OPTION value=6>6</OPTION>
<OPTION value=7>7</OPTION> <OPTION value=8>8</OPTION> <OPTION
value=9>9</OPTION></SELECT> <SELECT οnchange=showimg() size=1 name=face3>
<OPTION value=0 selected>0</OPTION> <OPTION value=1>1</OPTION> <OPTION
value=2>2</OPTION> <OPTION value=3>3</OPTION> <OPTION value=4>4</OPTION>
<OPTION value=5>5</OPTION> <OPTION value=6>6</OPTION> <OPTION
value=7>7</OPTION> <OPTION value=8>8</OPTION> <OPTION
value=9>9</OPTION></SELECT> <A href="http://img.mop.com/static/face.html"
target=_blank>查看</A> <INPUT type=checkbox value=true name=anonymous/>匿名(需要300MP) <INPUT
type=hidden value=282868 name=subId /> <INPUT onClick="return gosubmit();" type=submit value=提交 name=B1/></P></FORM>
<HR/>
<SCRIPT>
//查看楼主匿名
function desAnonymousForMaster(obj){
if(confirm('查看匿名将花费500MP。确定要查看吗?')){
obj.href ='desAnonymous.jsp?wcId=282868&floorNum=0';
return true;
}
else{
return false;
}
}
</SCRIPT>
</BODY>
</html>
好了,最终的效果图就是这样(主贴和回帖信息分开是为了便于后台的管理)
转载于:https://blog.51cto.com/axiii/102340