loginservlet.java~15~

来自「野生动物系统的JAVA源码, 野生动物系统的JAVA源码」· JAVA~15~ 代码 · 共 87 行

JAVA~15~
87
字号
package scout.sys_manage.login;

/**
 * <p>Title: control center</p>
 * <p>Description:when login,the operator inputs account an password into the textbox and submit.
 * the servlet get those informations,check by access the table of database.
 * the class jude it </p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Company: Chongqing Kemeida corporation</p>
 * @author zengBo
 * @version 1.0
 */

import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import scout.database.util.ConnDataBase;

public class LoginServlet extends HttpServlet {

    /**
     * @param conf
     * @throws ServletException
     */
    public void init(ServletConfig conf) throws ServletException {
        super.init(conf);
    }

    /**
     * @param request
     * @param response
     */
    public void doPost(HttpServletRequest request, HttpServletResponse response) {
        try {
            checkUser(request, response);
        } catch (Exception e) {}
    }

    /**
     * flag=2:success
     * flag=1:the account exist,but the password is error.
     * flag=0:the account does'not exist.
     * flag=-1:a mistake that we dont't expect in system has occured!
     * @param request
     * @param response
     */
    public void checkUser(HttpServletRequest request,
                          HttpServletResponse response) throws
            ServletException {
        int flag = 0;
        try {
            String account = request.getParameter("account");
            String password = request.getParameter("password");
            String sqlAcc = "select * from login_info where account='" +
                            account + "'";
            String sqlPas = "select * from login_info where account='" +
                            account +
                            "' and password='" + password + "'";
            ConnDataBase cdb = new ConnDataBase();
            Connection conn = cdb.getConnection();
            Statement stmt = conn.createStatement();
            ResultSet resPas = stmt.executeQuery(sqlPas);
            if (resPas.next()) {
                flag = 2;
                request.getSession().setAttribute("account", account);
                resPas.close();
            } else {
                ResultSet resAcc = stmt.executeQuery(sqlAcc);
                if (resAcc.next()) {
                    flag = 1;
                    request.getSession().setAttribute("account", account);
                    resAcc.close();

                }
            }
            stmt.close();
            conn.close();
            response.sendRedirect("sys_manage/login/result.jsp?flag=" + flag);
        } catch (Exception e) {
            System.out.print(e.getMessage());
            flag = -1;
        }

    }
}

⌨️ 快捷键说明

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