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


package edu.neu.sspp.servlet; 
 
import java.io.IOException; 
import java.util.List; 
 
import javax.servlet.ServletException; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
 
import edu.neu.sspp.StudentHandler; 
import edu.neu.sspp.hibernate.TTeacher; 
import edu.neu.sspp.hibernate.TTeacherDAO; 
import edu.neu.sspp.hibernate.TUser; 
import edu.neu.sspp.hibernate.TUserDAO; 
 
public class Project extends HttpServlet { 
 
	/** 
	 * Constructor of the object. 
	 */ 
	public Project() { 
		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 { //通过URL获取用户名 String userName = request.getParameter("name"); TUserDAO userDAO = new TUserDAO(); List userList = userDAO.findByUserName(userName); if(!userList.isEmpty()) { TUser user = (TUser)userList.get(0); request.setAttribute("userName", userName); //在jsp页面中将userID与session中的id比较,防止用户篡改cookie或者请求 request.setAttribute("userID", user.getUserUid()); request.setAttribute("nick", user.getNick()); request.setAttribute("count", user.getCount()); request.setAttribute("projects", user.getTProjects()); //针对老师的代码 if(request.getSession().getAttribute("login") != null && request.getSession().getAttribute("login").equals("teacher")) { TTeacherDAO teacherDAO = new TTeacherDAO(); TTeacher teacher = teacherDAO.findById((String)request.getSession().getAttribute("id")); StudentHandler sh = new StudentHandler(teacher.getStudents()); request.setAttribute("added", sh.studentAdded(userName)); } //HibernateSessionFactory.closeSession(); 不能关,涉及懒读 request.getRequestDispatcher("../proj_jsp/project.jsp").forward(request, response); return; } else { //should be better response.getWriter().write("page not found"); } } /** * 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 } }