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

📄 messageboard.java

📁 该留言板使用JSP/servlet+hibernate+MySQL制作而成!详细内容内容请看JAVADOC说明文档.
💻 JAVA
字号:
/*
 * MessageBoard.java
 *
 * Created on 2007年5月28日, 下午9:12
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

/**
 *
 * @author abc
 */
package message_board;

import java.io.IOException;

import javax.naming.NamingException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.http.HttpSession;

public class MessageBoard extends HttpServlet
{
  
    private static final String DEFAULT_PAGE    = "request_error.html";

    private static final String ACTION_TOKEN    = "ACTION";

    private static final String MESSAGE_ACTIONS   = "MESSAGE";
    private static final String ADMIN   = "ADMIN";
    private static final String ADMIN_LOGIN   = "ADMIN_LOGIN";
    private static final String ADMIN_LOGOUT   = "ADMIN_LOGOUT";
    

    private JDBCHelper     _jdbcHelper     = null;
    
    private String user;
    private String passwd;
    
    HttpSession session = null ;
                    
  
    public void init(ServletConfig config) throws ServletException
    {
        super.init(config);
        user = config.getInitParameter("username");
        passwd = config.getInitParameter("password");

       try
        {
            _jdbcHelper = new JDBCHelper();
        }
        catch (NamingException unexpected)
        {
            throw new ServletException("Cannot locate required database : " + unexpected);
        }

    }

    

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

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {
        String action = findAction(request);

        ProcessingCommand command = null;
        if (action != null)
        {

            if (action.startsWith(MESSAGE_ACTIONS))
            {
                command = new MessageProcessingCommand(_jdbcHelper);
            }
            else if (action.startsWith(ADMIN))
            {
                if (action.equals(ADMIN_LOGIN))
                {
                    String username = request.getParameter("username");
                    String password = request.getParameter("password");
                    session = request.getSession();
                    boolean isNew = session.isNew();
                    if (!isNew)
                    {
                        session.removeAttribute("username");//先清除老的Session里的某个字段值
                        session.removeAttribute("password");
                        session = request.getSession(true);
                    }
                    session.setAttribute("username", username);
                    session.setAttribute("password", password);
                     if( user.equals(username)&& passwd.equals(password))
                    {   
                        command = new MessageProcessingCommand(_jdbcHelper);
                    }
                }
                else if (action.equals(ADMIN_LOGOUT))
                {
                        session.removeAttribute("username");
                        session.removeAttribute("password");
                        command = new MessageProcessingCommand(_jdbcHelper);
                        command.execute(request, response, MESSAGE_ACTIONS);
                        return;
                }
                else
                {
                    String username = (String) session.getAttribute("username");
                    String password = (String) session.getAttribute("password");
                    if( user.equals(username)&& passwd.equals(password))
                    {   
                        command = new MessageProcessingCommand(_jdbcHelper);
                    }
                }  
                                
            }
            if (command != null)
            {
                command.execute(request, response, action);
                return;
            }
         }
        else if (action == null)
        {
            command = new MessageProcessingCommand(_jdbcHelper);
            command.execute(request, response, MESSAGE_ACTIONS);
            return;
                
        }
        RequestDispatcher dispatch = request.getRequestDispatcher(DEFAULT_PAGE);
        dispatch.forward(request, response);
    }

    private String findAction(HttpServletRequest request)
    {
        return request.getParameter(ACTION_TOKEN);
    }
}

⌨️ 快捷键说明

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