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

📄 indexservlet.java

📁 运用hibernate技术 实现的留言板
💻 JAVA
字号:
package com.tiandinet.HiMessage;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.RequestDispatcher;

import java.util.Hashtable;
import java.util.Enumeration;

public class IndexServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public IndexServlet() {
		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 {
		
		HiMessageFuncs hmf = new HiMessageFuncs();
		int pageTotal = 0;
		int page = 1;
		Conf conf = null;
		Hashtable messages = null;
		Hashtable replyMsgs = null;
		int messageTotal = 0;
		
		try {
			conf = hmf.getConfiguration();
	
			messageTotal = hmf.getMessageTotal();
			
			String pageStr = request.getParameter("page");

			if (pageStr != null && !pageStr.equals("")) {
				page = Integer.parseInt(pageStr);
			}
			
			pageTotal = hmf.getPageTotal(messageTotal, conf.getPageshow().intValue());
			
			if (page < 1) {
				page  = 1;
			}
			else if (page > pageTotal) {
				page = pageTotal;
			}
			
			messages = hmf.getMessages(page);
			replyMsgs = hmf.getReplyMsgs();

		
			// get url and queryString
			String url = request.getRequestURL().toString();
			Enumeration em = request.getParameterNames();
			String queryString = "";
			String parameterName = "";
			while (em.hasMoreElements()) {
				parameterName = (String)em.nextElement();
				queryString += "&" + parameterName + "=" + request.getParameter(parameterName);
			}
			if (queryString.length() > 1) {
				queryString = queryString.substring(1);
			}
			
			String pageLink = hmf.pageLink(url, queryString, pageTotal, page);
			
			String adminLogin = "";
			HttpSession sess = request.getSession(false);
			if (sess != null) {
				String loginStatus = (String)sess.getAttribute("loginStatus");
				if (loginStatus != null && loginStatus.equals("OK")) {
					adminLogin = loginStatus;
				}
				else {
					adminLogin = "";
				}
			}
			
			// administrator login
			request.setAttribute("adminLogin", adminLogin);
			
			request.setAttribute("HiMessageConf", conf);
			request.setAttribute("HiMessageMessages", messages);
			request.setAttribute("replyMsgs", replyMsgs);
			request.setAttribute("messageTotal", Integer.valueOf(messageTotal));
			request.setAttribute("pageTotal", Integer.valueOf(pageTotal));
			request.setAttribute("pageLink", pageLink);
			
			//response.sendRedirect(request.getContextPath() + "/index.jsp");
			RequestDispatcher rd= request.getRequestDispatcher("/messageList.jsp"); 
			rd.forward(request, response);
		}
		catch (NullPointerException npe) {
			PrintWriter out = response.getWriter();
			
			out.println("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">");
			out.println("<html xmlns=\"http://www.w3.org/1999/xhtml\">");
			out.println("	<HEAD>");
			out.println("		<TITLE>HiMessage - TiandiNet.com</TITLE>");
			out.println("		<meta http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\" />");
			out.println("	</HEAD>");
			out.println("  <BODY>");	
			out.println("<font color=\"#FF0000\" family=\"Tahoma\">Fatal Error:</font> Can not connect the Database!");
			out.println("  </BODY>");
			out.println("</HTML>");
			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 {
		this.doGet(request, response);
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occure
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}

⌨️ 快捷键说明

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