MVC實例html
如今Javaweb開發第一階段學習步入實踐階段,在這兒將本身作的項目記下來,也方便積累經驗。java
下圖是處理流程:mysql
MySQL數據庫表以下所示:web
1.在web項目webContent中新建test.jspsql
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>數據庫
<a href="listAllStudentsServlet">List All Students</a>mvc
</body>
</html>jsp
2在Java-src中新建ListAllStudentServlet.java,並對其進行配置學習
package com.javaweb.mvc;ui
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ListAllStudentsServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
StudentDao studentDao = new StudentDao();
List<Student> students = studentDao.getAll();
request.setAttribute("students", students);
request.getRequestDispatcher("/students.jsp").forward(request, response);
}
}
3.繼續建立Student.java
package com.javaweb.mvc;
public class Student {
private Integer id;
private String studentName;
private String location;
private String telephone;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
public Student(Integer id, String studentName, String location, String telephone) {
super();
this.id = id;
this.studentName = studentName;
this.location = location;
this.telephone = telephone;
}
}
4.建立StudentDao.java
package com.javaweb.mvc;
import java.util.ArrayList;
import java.util.List;
import java.sql.*;
public class StudentDao {
public List<Student> getAll(){
List<Student> students = new ArrayList<Student>();
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/student?user=root&password=1234";
String user = "root";
String password = "1234";
String sql = "select * from stu";
conn = DriverManager.getConnection(url, user, password);
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
while(rs.next()){
int id = rs.getInt(1);
String studentName = rs.getString(2);
String location = rs.getString(3);
String telephone = rs.getString(4);
Student student = new Student(id, studentName, location, telephone);
students.add(student);
}
}catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if(rs != null)
rs.close();
if(ps != null)
ps.close();
if(conn != null)
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return students;
}
}
5.在webContent中新建students.jsp
<%-- 顯示學生信息 --%>
<%@page import="java.util.List"%>
<%@page import="com.javaweb.mvc.Student" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
List<Student> students = (List<Student>)request.getAttribute("students");
%>
<table>
<tr>
<th>id</th>
<th>studentName</th>
<th>location</th>
<th>telephone</th>
</tr>
<%
for(Student student:students){
%>
<tr>
<td><%= student.getId() %></td>
<td><%= student.getStudentName() %></td>
<td><%= student.getLocation() %></td>
<td><%= student.getTelephone() %></td>
</tr>
<%
}
%>
</table>
</body>
</html>
6.添加JDBC驅動
添加在lib文件夾,由於lib文件夾中存放支持web應用運行的JAR文件
7.接下來就能夠運行了
這是運行結果: