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

📄 adminservlet.java

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

import com.tiandinet.HiMessage.HiMessageAdminFuncs;

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

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

public class AdminServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public AdminServlet() {
		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 {
		
		String action = request.getParameter("action");
		if (action == null) {
			action = "";
		}
		
		HttpSession sess = request.getSession(true);
		
		/**
		 * 1. log off
		 */
		if (action.equals("logoff")) {
			sess.invalidate();
			
			String indexServletURL = response.encodeRedirectURL("IndexServlet");
			response.sendRedirect(indexServletURL);
		}
		
		/**
		 * 2. setting
		 */
		else if (action.equals("setting")) {
			String adminLoginStatus = "";
			if (sess != null) {
				adminLoginStatus = (String)sess.getAttribute("loginStatus");
			}
			
			if (adminLoginStatus == null || !adminLoginStatus.equals("OK")) {
				sess.invalidate();
				RequestDispatcher rd = request.getRequestDispatcher("/adminLogin.jsp");
				rd.forward(request, response);
			}
			else {
				HiMessageFuncs hmf = new HiMessageFuncs();
				Conf conf = hmf.getConfiguration();
				
				request.setAttribute("conf", conf);
				RequestDispatcher rd = request.getRequestDispatcher("/adminSetting.jsp");
				rd.forward(request, response);
			}
		}
		
		/**
		 * 3. show login interface
		 */
		else {
			sess.invalidate();
			RequestDispatcher rd = request.getRequestDispatcher("/adminLogin.jsp");
			rd.forward(request, response);
		}
	}

	/**
	 * 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 {

		String action = request.getParameter("action");
		if (action == null) {
			action = "";
		}
		
		HttpSession sess = request.getSession(true);
		
		/**
		 * 1. log in
		 */
		if (action.equals("login")) {
			String username = request.getParameter("username");
			String password = request.getParameter("password");
			
			HiMessageAdminFuncs hmaf = new HiMessageAdminFuncs();
			
			if (hmaf.adminLogin(username, password)) {
				sess.setAttribute("loginStatus", "OK");

				String indexServletURL = response.encodeRedirectURL("IndexServlet");
				response.sendRedirect(indexServletURL);
			}
			else {
				sess.setAttribute("loginStatus", "Failed");
				request.setAttribute("errorInfo", "Wrong Username or Password!");
				RequestDispatcher rd = request.getRequestDispatcher("/adminLogin.jsp");
				rd.forward(request, response);
			}
		}
		
		/**
		 * 2. delete a message
		 */
		else if (action.equals("delMsg")) {
			String deleteMsgId = (String)request.getParameter("deleteMsgId");
			String adminLoginStatus = (String)sess.getAttribute("loginStatus");
			
			if (deleteMsgId != null 
				&& !deleteMsgId.equals("") 
				&& adminLoginStatus != null 
				&& adminLoginStatus.equals("OK")) {
				
				HiMessageAdminFuncs hmaf = new HiMessageAdminFuncs();
				
				String delMsgInfo = "";
				if (hmaf.deleteMessage(deleteMsgId)) {
					delMsgInfo = "Delete successfully!";
				}
				else {
					delMsgInfo = "Failed to delete!";
				}
				
				request.setAttribute("delMsgInfo", delMsgInfo);
				RequestDispatcher rd = request.getRequestDispatcher("/IndexServlet");
				rd.forward(request, response);
			}
			else {
				sess.invalidate();
				RequestDispatcher rd = request.getRequestDispatcher("/adminLogin.jsp");
				rd.forward(request, response);
			}
		}
		
		/**
		 * 3. updateConf
		 */
		else if (action.equals("updateConf")) {
			String title = request.getParameter("title");
			String pageshow = request.getParameter("pageshow");
			String show_ip = request.getParameter("show_ip");
			if (show_ip == null || !show_ip.equals("yes")) {
				show_ip = "0";
			}
			else {
				show_ip = "1";
			}
			
			HiMessageAdminFuncs hmaf = new HiMessageAdminFuncs();
			
			String info = "";
			if (hmaf.updateConfiguration(title, pageshow, show_ip)) {
				info = "alert('Update successfully!');window.location.href='" + response.encodeURL("AdminServlet?action=setting") + "';";
			}
			else {
				info = "alert('Failed to update!');window.location.href='" + response.encodeURL("AdminServlet?action=setting") + "';";
			}
			
			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("<script language='JavaScript'>");
			out.println(info);
			out.println("</script>");
			out.println("  </BODY>");
			out.println("</HTML>");
			out.flush();
			out.close();
		}
		
		/**
		 * 4. updatePassword
		 */
		else if (action.equals("updatePassword")) {
			String username = request.getParameter("username");
			String old_psw = request.getParameter("old_psw");
			String psw = request.getParameter("psw");
			
			HiMessageAdminFuncs hmaf = new HiMessageAdminFuncs();
			
			int result = hmaf.updatePassword(username, old_psw, psw);
			
			String info = "";
			if (result == 0) {
				info = "alert('Update successfully!');window.location.href='" + response.encodeURL("AdminServlet?action=setting") + "';";
			}
			else if (result == 1) {
				info = "alert('Wrong username or password!');window.location.href='" + response.encodeURL("AdminServlet?action=setting") + "';";
			}
			else {
				info = "alert('Failed to update!');window.location.href='" + response.encodeURL("AdminServlet?action=setting") + "';";
			}
			
			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("<script language='JavaScript'>");
			out.println(info);
			out.println("</script>");
			out.println("  </BODY>");
			out.println("</HTML>");
			out.flush();
			out.close();
		}
		
		/**
		 * 5. None action
		 */
		else {
			String indexServletURL = response.encodeRedirectURL("IndexServlet");
			response.sendRedirect(indexServletURL);
		}
	}

	/**
	 * 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 + -