MVC (jsp + servlet + javabean)

时间:2019-04-18
本文章向大家介绍MVC (jsp + servlet + javabean),主要包括MVC (jsp + servlet + javabean)使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

前端: 

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import = "java.util.ArrayList" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%!
int size;
ArrayList temp = new ArrayList();
%>
<%
ArrayList result = (ArrayList)request.getAttribute("result");
size = result.size();
%>
<table>
<tr>
<th>ID</th>
<th>品牌</th>
<th>价格</th>
<th>保质期</th>
</tr>
<%
for(int j = 0; j <size; j++){
temp.clear();
temp = (ArrayList)result.get(j); %>
<tr>
<th><a href = "change?id=<%=temp.get(0)%>"><%=temp.get(0)%></a></th>
<th><%= temp.get(1)%></th>
<th><%= temp.get(2)%></th>
<th><%= temp.get(3)%></th>
</tr>
<%}
%>
</table>
<a href = "add.jsp"> 增加 </a>

</body>
</html>

后端: 

@WebServlet("/show")
public class show extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
service service = new service();
ArrayList result = service.queryAll();
//将传递数据放入到request里面
request.setAttribute("result", result);
request.getRequestDispatcher( "show.jsp").forward(request,response);
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}

}

实体类:

package dao;

//专门用于数据的传递
public class entity {
private int id;
private String name;
private int price;
private int date;

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public int getDate() {
return date;
}
public void setDate(int date) {
this.date = date;
}
}