session的传值,个人信息的修改

session的传值,个人信息的修改

首先要在登录的时候把用户信息传到session里面

else {
			//权限验证session
			HttpSession session=request.getSession();
			session.setAttribute("currentUser", currentUser);
			response.sendRedirect("teacher.jsp");
		}

在所需要的servlet获得当前的session

try{
con=dbUtil.getCon();
User currentUser=(User) request.getSession().getAttribute(“currentUser”);
String userName=currentUser.getUserName();
int id=currentUser.getId();
}//这是获取的session信息

要把值传递给Dao包,也就是要在Servlet里面调用dao的放法,例如

try{
con=dbUtil.getCon();
User currentUser=(User) request.getSession().getAttribute(“currentUser”);
String userName=currentUser.getUserName();
int id=currentUser.getId();
JSONArray jsonArray= JsonUtil.formatRsToJsonArray(mySessionDao.mySession(con, pageBean,currentUser));
JSONObject result=new JSONObject();
int total=mySessionDao.mySessionCount(con);
result.put(“rows”, jsonArray);
result.put(“total”, total);
ResponseUtil.write(response,result );
}
这个是我的dao包

public class MySessionDao {
public ResultSet mySession(Connection con,PageBean pageBean,User user)throws Exception{
int id=user.getId();
String userName=user.getUserName();
StringBuffer sb=new StringBuffer(“select * from s_user where id=”+id);
if(pageBean!=null) {
sb.append(" limit “+pageBean.getStart()+”,"+pageBean.getRows());
}
PreparedStatement pstmt=con.prepareStatement(sb.toString().replaceFirst(“and”, “where”));
return pstmt.executeQuery();
}
这样就能获得当前用户的个人信息了

猜你喜欢

转载自blog.csdn.net/qq_41357948/article/details/89367594
今日推荐