www.pudn.com > SSPP.rar > ProjectInfo.java


package edu.neu.sspp.servlet; 
 
import java.io.IOException; 
import java.util.ArrayList; 
import java.util.Iterator; 
import java.util.Set; 
 
import javax.servlet.ServletException; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
 
import org.hibernate.Transaction; 
 
import edu.neu.sspp.PageSeparator; 
import edu.neu.sspp.hibernate.HibernateSessionFactory; 
import edu.neu.sspp.hibernate.TProject; 
import edu.neu.sspp.hibernate.TProjectDAO; 
import edu.neu.sspp.hibernate.TUser; 
 
public class ProjectInfo extends HttpServlet { 
 
	/** 
	 * Constructor of the object. 
	 */ 
	public ProjectInfo() { 
		super(); 
	} 
 
	/** 
	 * Destruction of the servlet. 
*/ public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } /** * The doGet method of the servlet.
* * 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 { String projectID = request.getParameter("proj"); TProjectDAO projectDAO = new TProjectDAO(); TProject project = projectDAO.findById(projectID); if(project != null) { TUser user = project.getTUser(); if(request.getParameter("name").equals(user.getUserName())) { project.setBrowse(project.getBrowse() + 1); Transaction transaction = HibernateSessionFactory.getSession().beginTransaction(); projectDAO.save(project); transaction.commit(); request.setAttribute("project", project); request.setAttribute("userID", user.getUserUid()); request.setAttribute("userName", request.getParameter("name")); //不能使用equal的project的count进行判断,而是要在请求里写一个count request.setAttribute("count", project.getCount()); handlePage(project, request); request.getRequestDispatcher("../proj_jsp/proj_info.jsp").forward(request, response); return; } } HibernateSessionFactory.closeSession(); response.getWriter().write("page not found"); } //处理分页 private void handlePage(TProject project, HttpServletRequest request) { Set set = project.getTComments(); PageSeparator page = new PageSeparator(set.size(), edu.neu.sspp.Constant.comment_page_size); //如果页数小于或等于1,按不分页处理 if(page.getTotalPage() <= 1) { request.setAttribute("comments", set); return; } if(request.getParameter("page") == null) { try { page.setCurrentPage(1); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } else { try { page.setCurrentPage(Integer.parseInt(request.getParameter("page"))); } catch (NumberFormatException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } //此处的处理并不得当 Iterator iterator = set.iterator(); ArrayList comments = new ArrayList(); for(int i = page.getCurrentPageBegin();i < page.getCurrentPageEnd(); i++ ) comments.add(iterator.next()); request.setAttribute("page", page); request.setAttribute("comments", comments); } /** * The doPost method of the servlet.
* * 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.
* * @throws ServletException if an error occure */ public void init() throws ServletException { // Put your code here } }