码码码码农 2017-05-10 10:20 采纳率: 33.3%
浏览 1572

我在代码加了request请求值,java里request为什么一直为空?大神们!!!!

DBUtil.java

 package a;

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

public class DBUtil {
    boolean bInited = false;
    private HttpSession request;

    // 加载驱动
    public void initJDBC() throws ClassNotFoundException {
        // 加载MYSQL JDBC驱动程序
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        bInited = true;
        System.out.println("Success loading sql Driver!");
    }

    public Connection getConnection() throws ClassNotFoundException,
            SQLException {
        if (!bInited) {
            initJDBC();
        }
        // 连接URL为 jdbc:mysql//服务器地址/数据库名
        // 后面的2个参数分别是登陆用户名和密码
        String url = "jdbc:sqlserver://localhost:1433;databaseName=classcircle";
        String userName = "sa";
        String password = "123456789123456";
        Connection conn = DriverManager.getConnection(url, userName, password);
        return conn;
    }

    public boolean loginSuccess(String userName, String password) {
        boolean returnValue = false;
        String sql = "SELECT * FROM headteacher";
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;

        try {
            conn = getConnection();
            stmt = conn.createStatement();
            rs = stmt.executeQuery(sql);
            while (rs.next()) {
                String userNameInDB = rs.getString("username");
                String passwordInDB = rs.getString("password");
                if (userNameInDB.equals(userName)
                        && passwordInDB.equals(password)) {
                    returnValue = true;
                    String name = rs.getString("name");
                    request.setAttribute("name", name);//**我就是加了这段代码**
                    System.out.println(name);

                    break;

                }
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return returnValue;

    }

    public boolean loginSuccess2(String userName, String password) {
        boolean returnValue = false;
        String sql = "SELECT * FROM teacher";
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;

        try {
            conn = getConnection();
            stmt = conn.createStatement();
            rs = stmt.executeQuery(sql);
            while (rs.next()) {
                String userNameInDB = rs.getString("username");
                String passwordInDB = rs.getString("password");
                if (userNameInDB.equals(userName)
                        && passwordInDB.equals(password)) {
                    returnValue = true;
                    break;
                }
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return returnValue;

    }

    public boolean loginSuccess3(String userName, String password) {
        boolean returnValue = false;
        String sql = "SELECT * FROM parents";
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;

        try {
            conn = getConnection();
            stmt = conn.createStatement();
            rs = stmt.executeQuery(sql);
            while (rs.next()) {
                String userNameInDB = rs.getString("username");
                String passwordInDB = rs.getString("password");
                if (userNameInDB.equals(userName)
                        && passwordInDB.equals(password)) {
                    returnValue = true;
                    break;
                }
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return returnValue;

    }

    public boolean loginSuccess4(String userName, String password) {
        boolean returnValue = false;
        String sql = "SELECT * FROM student";
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;

        try {
            conn = getConnection();
            stmt = conn.createStatement();
            rs = stmt.executeQuery(sql);
            while (rs.next()) {
                String userNameInDB = rs.getString("username");
                String passwordInDB = rs.getString("password");
                if (userNameInDB.equals(userName)
                        && passwordInDB.equals(password)) {
                    returnValue = true;
                    break;
                }
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return returnValue;

    }
}

servelet

package a;

import java.io.IOException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.JOptionPane;

public class loginServlet implements javax.servlet.Servlet {
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String userName = request.getParameter("username");// 取得用户名
        String password = request.getParameter("password");// 取得密码
        String radio = request.getParameter("radio");//单选框
        System.out.println(radio);
        DBUtil db = new DBUtil();// 构建数据库对象
        if ("1".equals(radio)) {
            boolean canLogin = db.loginSuccess(userName, password);
            if (canLogin) {// 根据登陆情况,跳转页面
                System.out.println("登陆成功!");
                response.sendRedirect("zhuyeheader.jsp");

            } else {
                JOptionPane.showMessageDialog(null, "账号或密码错误");
                response.sendRedirect("denglu.jsp");
            }
        }
        else if ("2".equals(radio)) {
            boolean canLogin = db.loginSuccess2(userName, password);
            if (canLogin) {// 根据登陆情况,跳转页面
                System.out.println("登陆成功!");
                response.sendRedirect("zhuyeteacher.jsp");
            } else {
                JOptionPane.showMessageDialog(null, "账号或密码错误");
                response.sendRedirect("denglu.jsp");
            }
        }
        else if ("3".equals(radio)) {
            boolean canLogin = db.loginSuccess3(userName, password);
            if (canLogin) {// 根据登陆情况,跳转页面
                System.out.println("登陆成功!");
                response.sendRedirect("zhuyepparents.jsp");
            } else {
                JOptionPane.showMessageDialog(null, "账号或密码错误");
                response.sendRedirect("denglu.jsp");
            }
        }
        else{
            boolean canLogin = db.loginSuccess4(userName, password);
            if (canLogin) {// 根据登陆情况,跳转页面
                System.out.println("登陆成功!");
                response.sendRedirect("zhuyestudent.jsp");
            } else {
                JOptionPane.showMessageDialog(null, "账号或密码错误");
                response.sendRedirect("denglu.jsp");
            }

        }
    }


    public void destroy() {
    }

    public ServletConfig getServletConfig() {
        return null;
    }

    public String getServletInfo() {
        return null;
    }

    public void init(ServletConfig arg0) throws ServletException {
    }

    public void service(ServletRequest request, ServletResponse response)
            throws ServletException, IOException {
        HttpServletRequest rq = (HttpServletRequest) request;
        HttpServletResponse rs = (HttpServletResponse) response;
        doPost(rq, rs);

    }![图片说明](https://img-ask.csdn.net/upload/201705/10/1494411561_422889.png)
}

  • 写回答

2条回答 默认 最新

  • Jan@昂 2017-05-10 10:36
    关注

    DBUtil 类第二行的request你定义错了,不是HttpSession

    评论

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题