我的代码如下:
package newpackage;//import java.beans.*;import java.sql.*;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;public class userBean {    public String userID;
    public String password;
    public String username;    public void setuserID(String _userID){
        this.userID=_userID;
    }
    public void setpassword(String _password){
        this.password=_password;
    }
    public String getusername(){
        return username;
    }
    public String getuserID(){
        return userID;
    }
    public String getpassword(){
        return password;
    }    public boolean checklogin(){
        boolean loginsuccess=false;
        Connection con=null;
        Statement stmt=null;
        ResultSet rs=null;        try{
            String url="jdbc:microsoft:sqlserver://localhost:1433;DataBaseName=pubs";
            Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
            con=DriverManager.getConnection(url,"sa","admin");
            String sql="select * from Users where userID='"+userID+"'";
            stmt=con.createStatement();
            rs=stmt.executeQuery(sql);
            while(rs.next()){
                if(password.equals(rs.getString("password")))
                {
                    loginsuccess=true;
                    username=rs.getString("username");
                    HttpSession session=servletRequest.getSession();   /*此处的servletRequest无法创建*/
                    /*session.setAttribute("username",username)*/
                }
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            try{
            con.close();
            }catch(Exception e1){
                e1.printStackTrace();
            }
            }
        return loginsuccess;    }}

解决方案 »

  1.   

    request.getSession().getAttribute("");
      

  2.   

    HttpServletRequest Request;
    request.getSession().setAttribute("username",username);
      

  3.   

    servletRequest这个从哪里来的呢
      

  4.   

    servletRequest是从那过来啊,首先的获得request对象啊,在获得session对象。
      

  5.   

    request.getSession()。在页面上默认有session对象,不过最好不要把页面和DB处理放在一起。
      

  6.   

    request.getSession()。在页面上默认有session对象
      

  7.   

    养成好习惯,需要继承时就继承,不要贪图不继承很帅,如果你是在用struts2
      

  8.   

    request.getSession().setAttribute("a",b);
      

  9.   

    request.getSession().setAttribut("name",name);
      

  10.   

    request.getSession().setAttribut("a",b);
      

  11.   

    好像应该是request.getSession();吧
      

  12.   

    先把request传到类中,再用request.getSession就可以了
      

  13.   

    可以将HttpServletRequest作为参数传递,不过不建议这么做,增加程序的偶合,如果要做的话可以public boolean checklogin(HttpServletRequest request){
    ...
    username=rs.getString("username");
    HttpSession session = request.getSession();.....
      

  14.   

    request.getSession()。在页面上默认有session对象
      

  15.   

    将request作为参数从外面传过来 public boolean checklogin(HttpServletRequest request) 然后在request.getSession()