1.首先建立一个请求页面
<body>
<a href="listAllStudents">list the student information </a>
</body>
很简单,就是一个超链接,输出一个表中的信息
2.建立servlet,接受请求并转发
通过调用Dao接收返回从数据库中查询数据包装成的模型组合二成的list,把list转发给页面,在页面中读取
public class LlistAllStudentsServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
studentdao studentDao = new studentdao();
List<students> students = studentDao.getStudentDao();
request.setAttribute("student",students);
request.getRequestDispatcher("/student.jsp").forward(request, response);
}
3.模型层,建立DAO和数据模型
暂时在Dao中连接数据库和查询数据,返回一个List
public class studentdao {
public List getStudentDao(){
List<students> students = new ArrayList<students>();
Connection conn = null;
PreparedStatement preparedstatement = null;
ResultSet resultset = null;
String sql = "select id, name,grade,location from tb_stu";
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/student";
String user = "root";
String password = "123456";
try {
Class.forName(driver);
conn = DriverManager.getConnection(url,user,password);
preparedstatement = conn.prepareStatement(sql);
resultset = preparedstatement.executeQuery();
while(resultset.next()){
int id = resultset.getInt(1);
String name = resultset.getString(2);
int grade = resultset.getInt(3);
String location = resultset.getString(4);
students stu = new students(id,name,grade,location);
students.add(stu);
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(resultset != null){
try {
resultset.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(preparedstatement != null){
try {
preparedstatement.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(conn != null){
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return students;
}
}
student模型
package jsp;
public class students {
private int id;
private String name;
private int grade;
private String location;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getGrade() {
return grade;
}
public void setGrade(int grade) {
this.grade = grade;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public students(int id,String name,int grade,String location){
this.id = id;
this.name = name;
this.grade = grade;
this.location = location;
}
public students(){
}
}
4.在页面中得到转发过来的数据,并一条条显示在表格中
需要注意的是要在页面中加载students模型类,语句为
<%@ page import="jsp.students" %>
<body>
<%
List<students> studs = (List<students>)request.getAttribute("student");
%>
<table>
<tr>
<th>ID</th>
<th>NAME</th>
<th>GRADE</th>
<th>LOCATION</th>
</tr>
<tr>
<%
for(students student:studs){
%>
<tr>
<td><%=student.getId() %></td>
<td><%=student.getName() %></td>
<td><%=student.getGrade() %></td>
<td><%=student.getLocation() %></td>
</tr>
<%
}
%>
</tr>
</table>
</body>
由此就可以在页面中显示