实现用户信息显示功能
这功能仅仅作为用户信息显示作用,内容比较少。
用户信息显示JSP页面,userview.jsp
pageEncoding="UTF-8"%>
html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
用户个人信息界面个人信息 | |
用户ID | ${st.userId } |
用户名 | ${st.userName } |
用户账号 | ${st.userAccount } |
用户信息显示Servlet,ViewUserServlet.javapackage servlet;
import java.io.IOException;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import dao.UserDao;
import entity.User;
/**
* 用户个人信息Servlet
*/
@WebServlet("/ViewUserServlet")
public class ViewUserServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setContentType("text/html;charset=UTF-8");
//获取用户ID
int userId = Integer.parseInt(req.getParameter("userId"));
UserDao ud = new UserDao();
User user = ud.selectOneUserInfo(userId);//调取用户个人信息
ArrayList list = new ArrayList();
list.add(user);
req.setAttribute("list", list);
//显示用户个人信息
String path = resp.encodeURL("userview.jsp");
req.getRequestDispatcher(path).forward(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
super.doPost(req, resp);
}
}
数据库操作--查询用户个人信息方法/**
* 按账号获取单个用户对象
* @param userAccount 用户账号
* @return User对象
*/
public User selectOneUserInfo(String userAccount) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
User user = null;
try {
//查询数据库比对用户信息
conn = DBUtils.getConnection();
String sql = "SELECT * FROM users WHERE user_account=?";
ps = conn.prepareStatement(sql);
//为?赋值
ps.setString(1, userAccount);
rs = ps.executeQuery();
//提取用户信息
while(rs.next()){
user = new User();
user.setUserId(rs.getInt("user_id"));
user.setUserName(rs.getString("user_name"));
user.setUserAccount(rs.getString("user_account"));
user.setUserPassword(rs.getString("user_password"));
user.setUserDisplay(rs.getString("user_display"));
user.setUserDeleteTime(rs.getString("user_deleteTime"));
}
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
//释放资源
DBUtils.closeAll(null, ps, conn);
}
return user;
}
返回用户列表JSP页面Servlet,UserReturnServlet.javapackage servlet;
import java.io.IOException;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import dao.UserDao;
import entity.User;
/**
* 返回用户管理界面
*/
@WebServlet("/UserReturnServlet")
public class UserReturnServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
super.doGet(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setContentType("text/html;charset=UTF-8");
UserDao ud = new UserDao();
ArrayList list = ud.selectNotDeleteList();
req.setAttribute("list", list);
//返回用户管理界面
String path = resp.encodeURL("userlist.jsp");
req.getRequestDispatcher(path).forward(req, resp);
}
}
OK,用户信息显示功能也完毕了。尽管JSP页面做出来的界面丑爆了,但还是要忍受的,毕竟使用的技术有限。如果是使用css和各种框架做出来的web页面,必须是美美哒,但也会花很多的时间啦。