本文共 1974 字,大约阅读时间需要 6 分钟。
//使用ajax输出从后台获取的信息
账号:姓名:...
protected void username(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { /*UserBean userBean = new UserBean();*/ List list = (List) request.getSession().getAttribute("info"); UserBean userBean = (UserBean) list.get(0); int u_id = userBean.getU_id(); UserBean userBean1 = userService.username(u_id); System.out.println(userBean1); JSONArray jsonArray = JSONArray.fromObject(userBean1); response.getWriter().print(jsonArray); }
- service UserService userService = new UserServiceImpl(); UserBean username(int u_id);
- serviceImpl UserDao userDao =new UserDaoImpl(); @Override public UserBean username(int u_id) { return userDao.username(u_id); }
- dao UserBean username(int u_id);
daoImpl
public UserBean username(int u_id){ //在数据库中查询信息 try{ UserBean userBean = new UserBean(); Connection conn = ConnUtil.getConnextion(); String changeSql = "select * from user WHERE u_id = ?"; PreparedStatement pstm = conn.prepareStatement(changeSql); pstm.setInt(1, u_id); ResultSet rs = pstm.executeQuery(); if(rs.next()) { userBean.setU_userid(rs.getString(2)); userBean.setU_username(rs.getString(3)); userBean.setU_password(rs.getString(4)); userBean.setU_sex(rs.getString(5)); userBean.setU_img(rs.getString(6)); return userBean; } }catch (Exception e){ System.out.println(e); } return null; }
登录成功后数据库的信息就就已经存在了session中,只需要在前端显示页面,即引入js接收即可:
如下所示:
<% List list = (List) request.getSession().getAttribute("info"); UserBean userBean = (UserBean) list.get(0); %>...用户名:
转载地址:http://gihna.baihongyu.com/