import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class UserInfoServlet
*/
@WebServlet("/UserInfoServlet")
public class UserInfoServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private List<UserInfo> userInfos = new ArrayList<UserInfo>();
/**
* @see HttpServlet#HttpServlet()
*/
public UserInfoServlet() {
super();
// TODO Auto-generated constructor stub
}
@Override
public void init(ServletConfig config) throws ServletException {
/* 通过Servlet初始化配置获取JDBC连接参数 */
String dbDriver = config.getInitParameter("DB_DRIVER");
String dbURL = config.getInitParameter("DB_URL");
String dbUser = config.getInitParameter("DB_USER");
String dbPassword = config.getInitParameter("DB_PASSWORD");
/* 通过JDBC从数据库userinfo表中获取用户信息 */
// 数据库连接对象引用
Connection connection = null;
// 声明对象引用
Statement statement = null;
// 结果集对象引用
ResultSet resultSet = null;
// 第一步:加载JDBC驱动程序
try {
Class.forName(dbDriver);
} catch (ClassNotFoundException e) {
System.out.println("找不到驱动程序类,加载驱动失败!");
e.printStackTrace();
}
// 第二步:创建数据库连接
try {
connection = DriverManager.getConnection(dbURL, dbUser, dbPassword);
} catch (SQLException e) {
System.out.println("数据库连接失败!");
e.printStackTrace();
}
// 第三步:创建执行SQL语句的声明对象
try {
statement = connection.createStatement();
} catch (SQLException e) {
System.out.println("获取SQL语句状态对象错误!");
e.printStackTrace();
}
// 第四步:执行SQL语句
try {
resultSet = statement.executeQuery("select * from userinfo");
} catch (SQLException e) {
System.out.println("执行SQL语句错误!");
e.printStackTrace();
}
// 第五步:处理执行结果
try {
while (resultSet.next()) {
// 获取用户信息
String id = resultSet.getString("userid");
String password = resultSet.getString("password");
String email = resultSet.getString("email");
String name = resultSet.getString("name");
String phone = resultSet.getString("phone");
// 将用户信息封装到UserInfo对象实例中
UserInfo userInfo = new UserInfo();
userInfo.setId(id);
userInfo.setPassword(password);
userInfo.setEmail(email);
userInfo.setName(name);
userInfo.setPhone(phone);
// 将UserInfo实例存放到容器中
userInfos.add(userInfo);
}
} catch (SQLException e) {
System.out.println("从结果集获取数据错误");
e.printStackTrace();
}
// 第六步:关闭JDBC所有对象
try {
if (resultSet != null) {
resultSet.close();
}
if (statement != null) {
statement.close();
}
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
super.init(config);
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();// 准备输出
out.println("<!DOCTYPE html>");
out.println("<html>"); // 输出HTML元素
out.println("<head lang=\"en\">"); // 输出HTML元素
out.println("<meta charset=\"UTF-8\">");
out.println("<title>显示用户信息</title>"); // 输出HTML元素
out.println("</head>"); // 输出HTML元素
out.println("<body>"); // 输出HTML元素
//填充用户信息
out.println("<table border=1>");
out.println("<tr>");
out.println("<th>用户ID</th>");
out.println("<th>用户名</th>");
out.println("<th>用户密码</th>");
out.println("<th>用户电话</th>");
out.println("<th>用户邮箱</th>");
out.println("</tr>");
for (UserInfo userInfo : userInfos) {
System.out.println(userInfo);
out.println("<tr>");
out.println("<td>"+userInfo.getId()+"</td>");
out.println("<td>"+userInfo.getName()+"</td>");
out.println("<td>"+userInfo.getPassword()+"</td>");
out.println("<td>"+userInfo.getPhone()+"</td>");
out.println("<td>"+userInfo.getEmail()+"</td>");
out.println("<tr/>");
}
out.println("</body>"); // 输出HTML元素
out.println("</html>"); // 输出HTML元素
out.close();
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
//配置文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" metadata-complete="true" version="3.0">
<display-name>JavaWebHomework</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>UserInfoServlet</servlet-name>
<servlet-class>com.lovo.servlet.UserInfoServlet</servlet-class>
<!-- 连接数据库驱动 -->
<init-param>
<param-name>DB_DRIVER</param-name>
<param-value>com.mysql.jdbc.Driver</param-value>
</init-param>
<!-- 连接数据库URL -->
<init-param>
<param-name>DB_URL</param-name>
<param-value>jdbc:mysql://localhost:3306/db_user?characterEncoding=utf-8</param-value>
</init-param>
<!-- 连接数据库用户名 -->
<init-param>
<param-name>DB_USER</param-name>
<param-value>root</param-value>
</init-param>
<!-- 连接数据库密码-->
<init-param>
<param-name>DB_PASSWORD</param-name>
<param-value></param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>UserInfoServlet</servlet-name>
<url-pattern>/UserInfoServlet.do</url-pattern>
</servlet-mapping>
</web-app>