Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jsp 如何在bean类中传递会话值_Jsp_Session_Servlets_Javabeans_Export To Excel - Fatal编程技术网

Jsp 如何在bean类中传递会话值

Jsp 如何在bean类中传递会话值,jsp,session,servlets,javabeans,export-to-excel,Jsp,Session,Servlets,Javabeans,Export To Excel,我有一个与bean类和会话相关的问题,我有一个 带有一些私有字段和一些setter和getter的简单类 方法,一个类与数据库交互,另一个类与数据库交互 控制器,我面临的问题是在主服务器上将用户值设置为null 任何人可以帮助我在这方面,我张贴完整的代码在这里 通过login.jsp我正在传递值,通过userlogged-in-jsp我正在尝试检索请任何人在这里帮助我 public class ConnectionManager { public static Connection getCo

我有一个与bean类和会话相关的问题,我有一个 带有一些私有字段和一些setter和getter的简单类 方法,一个类与数据库交互,另一个类与数据库交互 控制器,我面临的问题是在主服务器上将用户值设置为null 任何人可以帮助我在这方面,我张贴完整的代码在这里 通过login.jsp我正在传递值,通过userlogged-in-jsp我正在尝试检索请任何人在这里帮助我

public class ConnectionManager {

public static Connection getConnection(){
    Connection con=null;  
     try{

    Class.forName("com.mysql.jdbc.Driver");
con=(Connection) DriverManager.getConnection("jdbc:mysql://localhost/demo2","root","");


}catch(Exception  e){

}
return con; 
}} 
   public class LoginServlet extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response) 
        throws ServletException, java.io.IOException {

   try
  {        

   UserBean user = new UserBean();
   user.setUserName(request.getParameter("un"));
   user.setPassword(request.getParameter("pw"));

 user = UserDAO.login(user);

 if (user.isValid())
 {

    HttpSession session = request.getSession();    

     session.setAttribute("currentSessionUser",user); 

      response.sendRedirect("userLogged.jsp");    

  }

  else 
      response.sendRedirect("invalidLogin.jsp");
   } 


  catch (Throwable theException)         
  {
   System.out.println(theException); 
  }
   }
  }         
   public class UserDAO     
  {
  static Connection currentCon = null;
  static ResultSet rs = null;  
    public static UserBean login(UserBean bean) {

  //preparing some objects for connection 
  Statement stmt = null;    

  String username = bean.getUsername();    
  String password = bean.getPassword();   

  String searchQuery =
        "select * from users where username='"
                 + username
                 + "' AND password='"
                 + password
                 + "'";
        try 
  {

   currentCon = ConnectionManager.getConnection();
  stmt=currentCon.createStatement();
  rs = stmt.executeQuery(searchQuery);            
  boolean more = rs.next();

   // if user does not exist set the isValid variable to false
    if (!more) 
   {
     System.out.println("Sorry, you are not a registered user! Please sign up first");
     bean.setValid(false);
  } 

  //if user exists set the isValid variable to true
  else if (more) 
  {
     String firstName = rs.getString("FirstName");
     String lastName = rs.getString("LastName");


     bean.setFirstName(firstName);
     bean.setLastName(lastName);
     bean.setValid(true);
  }
   } 

   catch (Exception ex) 
   {
  System.out.println("Log In failed: An Exception has occurred! " + ex);
   } 


  finally 
   {
  if (rs != null)    {
     try {
        rs.close();
     } catch (Exception e) {}
        rs = null;
     }

  if (stmt != null) {
     try {
        stmt.close();
     } catch (Exception e) {}
        stmt = null;
     }

  if (currentCon != null) {
     try {
        currentCon.close();
     } catch (Exception e) {
     }

     currentCon = null;
   }
 }

 return bean;

 }    
 }
   public class UserBean {

  private String username;
  private String password;
  private String firstName;
  private String lastName;
  public boolean valid;


   public String getFirstName() {
   return firstName;
  }

   public void setFirstName(String newFirstName) {
   firstName = newFirstName;
  }


   public String getLastName() {
   return lastName;
      }

 public void setLastName(String newLastName) {
     lastName = newLastName;
      }


   public String getPassword() {
   return password;
   }

   public void setPassword(String newPassword) {
   password = newPassword;
  }


   public String getUsername() {
     return username;
      }

    public void setUserName(String newUsername) {
     username = newUsername;
      }


   public boolean isValid() {
     return valid;
  }

   public void setValid(boolean newValid) {
   valid = newValid;
   }    
  } 

  /*login.jsp*/

   form action="LoginServlet"

        Please enter your username         
        input type="text" name="un"        

        Please enter your password
        input type="text" name="pw"

        input type="submit" value="submit"           

    /form


 /*userLogged.jsp*/
  html


 Welcome Pname:<%=request.getAttribute("user")%>
  /html
公共类连接管理器{
公共静态连接getConnection(){
连接con=null;
试一试{
Class.forName(“com.mysql.jdbc.Driver”);
con=(连接)DriverManager.getConnection(“jdbc:mysql://localhost/demo2“,”根“,”);
}捕获(例外e){
}
返回con;
}} 
公共类LoginServlet扩展了HttpServlet{
public void doGet(HttpServletRequest请求、HttpServletResponse响应)
抛出ServletException,java.io.IOException{
尝试
{        
UserBean user=newuserbean();
user.setUserName(request.getParameter(“un”);
user.setPassword(request.getParameter(“pw”);
user=UserDAO.login(用户);
if(user.isValid())
{
HttpSession session=request.getSession();
setAttribute(“currentSessionUser”,用户);
sendRedirect(“userLogged.jsp”);
}
其他的
sendRedirect(“invalidLogin.jsp”);
} 
捕获(丢弃例外)
{
System.out.println(异常);
}
}
}         
公共类UserDAO
{
静态连接currentCon=null;
静态结果集rs=null;
公共静态UserBean登录(UserBean){
//准备一些要连接的对象
语句stmt=null;
字符串username=bean.getUsername();
字符串password=bean.getPassword();
字符串搜索查询=
“从用户名为“”的用户中选择*”
+用户名
+“'和密码='”
+密码
+ "'";
尝试
{
currentCon=ConnectionManager.getConnection();
stmt=currentCon.createStatement();
rs=stmt.executeQuery(搜索查询);
布尔更多=rs.next();
//如果用户不存在,请将isValid变量设置为false
如果(!更多)
{
System.out.println(“对不起,您不是注册用户!请先注册”);
setValid(false);
} 
//如果用户存在,请将isValid变量设置为true
否则,如果(更多)
{
String firstName=rs.getString(“firstName”);
String lastName=rs.getString(“lastName”);
setFirstName(firstName);
setLastName(lastName);
setValid(true);
}
} 
捕获(例外情况除外)
{
System.out.println(“登录失败:发生异常!”+ex);
} 
最后
{
如果(rs!=null){
试一试{
rs.close();
}捕获(例外e){}
rs=null;
}
如果(stmt!=null){
试一试{
stmt.close();
}捕获(例外e){}
stmt=null;
}
如果(currentCon!=null){
试一试{
currentCon.close();
}捕获(例外e){
}
currentCon=null;
}
}
返回豆;
}    
}
公共类用户bean{
私有字符串用户名;
私有字符串密码;
私有字符串名;
私有字符串lastName;
公共布尔有效;
公共字符串getFirstName(){
返回名字;
}
public void setFirstName(字符串newFirstName){
firstName=newFirstName;
}
公共字符串getLastName(){
返回姓氏;
}
public void setLastName(字符串newLastName){
lastName=newLastName;
}
公共字符串getPassword(){
返回密码;
}
public void setPassword(字符串newPassword){
密码=新密码;
}
公共字符串getUsername(){
返回用户名;
}
public void setUserName(字符串newUsername){
用户名=新用户名;
}
公共布尔值isValid(){
返回有效;
}
public void setValid(布尔值newValid){
valid=newValid;
}    
} 
/*login.jsp*/
form action=“LoginServlet”
请输入您的用户名
输入type=“text”name=“un”
请输入您的密码
输入type=“text”name=“pw”
输入type=“submit”value=“submit”
/形式
/*userLogged.jsp*/
html
欢迎光临Pname:
/html

实际上,我只是在搜索语法。我得到了它;这是

   <p>first name: ${currentSessionUser.firstName}</p>
名字:${currentSessionUser.firstName}


我找到了解决方案。。。已经感谢各位了,发布你们的解决方案怎么样?(或删除此问题)