MVC (jsp + servlet + javabean)

Posted 自信,自律, 积极

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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;
}
}

 

以上是关于MVC (jsp + servlet + javabean)的主要内容,如果未能解决你的问题,请参考以下文章

熟悉基于JSP和Servlet的Java Web开发,对Servlet和JSP的工作原理和生命周期有深入了解,熟练的使用JSTL和EL编写无脚本动态页面,有使用监听器过滤器等Web组件以及MVC架构

狂神Java Web MVC三层架构

JSP+JavaBean+Servlet技术(MVC模型)

基于servlet,jsp,来进行MVC思想的剖析 第一部分

Java EE模式和MVC

Head First Servlet&JSP MVC实战