⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 servletindex.java

📁 北大青鸟ACCP5.0课程项目 青鸟论坛
💻 JAVA
字号:
package com.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.dao.CommonDAO;
import com.dao.ReplyInfoDAO;
import com.dao.SectionInfoDAO;
import com.dao.TopicInfoDAO;
import com.entity.*;
import com.page.IndexPage;

public class servletIndex extends HttpServlet {

	private ServletConfig config = null;
	private TopicInfoDAO dao = new TopicInfoDAO();

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String s = config.getInitParameter("character");
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();

		// 声明DAO对象
		SectionInfoDAO section_dao = new SectionInfoDAO();
		TopicInfoDAO topic_dao = new TopicInfoDAO();
		ReplyInfoDAO reply_dao = new ReplyInfoDAO();
		// 声明变量------
		int sParentId, sId, tId = 0;
		List<SectionInfo> listParent = null;
		List<SectionInfo> listChild = null;
		List<SectionInfo> supersList = new ArrayList<SectionInfo>();
		SuperSection supers = null;
		SubSection subs = null;
		List<SectionInfo> subsList = null;
		//
		listParent = section_dao.getSectionById(0);// 先遍历根板块
		for (int i = 0; i < listParent.size(); i++) {
			supers = new SuperSection();// 创建父版块对象

			sParentId = listParent.get(i).getSid();
			listChild = section_dao.getSectionById(sParentId);// 再遍历子板块

			// 获得父版块id
			supers.setSid(listParent.get(i).getSid());
			// 获得父版块名称
			supers.setSname(listParent.get(i).getSname());
			
			subsList = new ArrayList<SectionInfo>();
			for (int j = 0; j < listChild.size(); j++) {
				subs = new SubSection();// 创建子版块对象
				IndexPage temp1 = null;
				IndexPage temp2 = null;
				sId = listChild.get(j).getSid();
				temp1 = topic_dao.getALLTopicLastTimeById(sId);// 获得主帖子最后发帖的记录
				// 获得子版块id
				subs.setSid(listChild.get(j).getSid());
				// 获得子版块名称
				subs.setSname(listChild.get(j).getSname());
				// 获得所有主帖数
				subs.setStopiccount(listChild.get(j).getStopiccount());
				// 获得最后发帖的记录
				if (temp1 != null) {// 如果有跟帖,则显示跟帖最后发帖的记录,否则显示主帖的记录
					tId = temp1.getTid();
					temp2 = reply_dao.getAllReplyLastTimeById(sId, tId);// 获得跟帖子最后发帖的记录
					
					//获得最后发帖的主帖标题
					subs.setTitle(temp1.getTitle());
					
					if (temp2 != null) {//
						// 获得最后发帖的用户名称
						subs.setAuthor(temp2.getAuthor());
						//获得最后发帖的主帖id
						subs.setTid(temp2.getTid());
						//获得最后发帖的时间
						subs.setLasttimepost("[&nbsp" + CommonDAO.getDateFormat(temp2
								.getPublishtime()) + "&nbsp]");
					} else {// 获得同一版块所有帖子最后发帖的记录
						// 获得最后发帖的用户名称
						subs.setAuthor(temp1.getAuthor());
						//获得最后发帖的主帖id
						subs.setTid(temp1.getTid());
						//获得最后发帖的时间
						subs.setLasttimepost("[&nbsp" + CommonDAO.getDateFormat(temp1
								.getPublishtime()) + "&nbsp]");
					}
				} else {// 如果没有主帖,则显示“从来”
					subs.setLasttimepost("从未");
				}
				subsList.add(subs);
			}
			supers.setSubsectionlist(subsList);// 
			supersList.add(supers);// 获得在index页面显示的版块
		}
		// 设置request对象的属性
		request.setAttribute("supersList", supersList);
		request.getRequestDispatcher("index.jsp").forward(request, response);
	}

	public void init(ServletConfig config) throws ServletException {
		this.config = config;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -