login.java

来自「网络邮件系统」· Java 代码 · 共 79 行

JAVA
79
字号
/*
 * This product includes software developed by the
 * Apache Software Foundation (http://www.apache.org/).
 */
package ch05.controller;

import java.io.*;
import java.util.Hashtable;

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

import ch05.*;
import ch05.module.*;

/**
 * 针对登录页面的Servlet
 * @author ShenYK
 * @version 1.0
 */
public class Login extends HttpServlet
{
    public void doGet ( HttpServletRequest request, 
                        HttpServletResponse response )
        throws ServletException, IOException 
    {
        //设置提交表单的中文编码
        request.setCharacterEncoding("GBK");
        
        HttpSession mySession = request.getSession(true);
        
        //清空错误消息
        mySession.setAttribute("errMsg","");
        
        //设置session中的页面值域
        mySession.setAttribute(CommonConst.VIEWID_LOGIN, new Hashtable() );
        
        //是否进入默认页面
        if ( !request.getParameterNames().hasMoreElements() )
        {
            response.sendRedirect("/ch05/login.jsp");
            return;
        }
        
        //得到用户输入信息
        String sUsername = request.getParameter("username");
        String sPassword = request.getParameter("password");

        //如果用户是提交表单
        if ( sUsername != null && sUsername.length() > 0 )
        {
            //校验用户输入信息
            MLogin mLogin = new MLogin();
            
            boolean bCheckResult = mLogin.getUserInfo( mySession, sUsername, sPassword );
            if ( bCheckResult )
            {
                mySession.setAttribute("curPage","inbox");
                response.sendRedirect("/ch05/servlet/Inbox");
            }
            else
            {
                response.sendRedirect("/ch05/login.jsp");
            }
        }
        //如果用户非法进入这个页面
        else
        {
            response.sendRedirect("/ch05/login.jsp");
        }
    }
    
    public void doPost ( HttpServletRequest request, 
                           HttpServletResponse response )
        throws ServletException, IOException 
    {
        doGet( request, response );
    }
}

⌨️ 快捷键说明

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