mvc模式项目:
1。现在有个Animal表 里面有3个字段id(主键——编号),name(姓名),protect(监护人)
2。有2个类(AnimalDao.java)和(AnimalDto.java)
///AnimalDto.java类
package com.rw.mvc.service;
public class AnimalDto {
private Long id;
private String name;
private String protect;
public AnimalDto() {
super();
}
public AnimalDto(String name, String protect) {
super();
this.name = name;
this.protect = protect;
}
public AnimalDto(Long id, String name, String protect) {
super();
this.id = id;
this.name = name;
this.protect = protect;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getProtect() {
return protect;
}
public void setProtect(String protect) {
this.protect = protect;
}
}
、、、、、、、、、、、、、、、、、、、、、AnimalDao.java类
package com.rw.mvc.service;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;
public class AnimalDao {
private Connection con;
private PreparedStatement ps;
private ResultSet rs;
public void getCon() {
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
con = DriverManager
.getConnection(
"jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=study",
"sa", "1");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
public void closeAll() {
try {
if (rs != null)
rs.close();
if (ps != null)
ps.close();
if (con != null)
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public void Add(AnimalDto dto) {
getCon();
String sql="insert into animal(name,protect) values(?,?)";
try {
ps=con.prepareStatement(sql);
ps.setString(1, dto.getName());
ps.setString(2, dto.getProtect());
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}finally{
closeAll();
}
}
public void Del(Long id) {
getCon();
String sql="delete from animal where id=?";
try {
ps=con.prepareStatement(sql);
ps.setLong(1, id);
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}finally{
closeAll();
}
}
public void Update(AnimalDto dto) {
getCon();
String sql="update animal set name=?,protect=? where id=?";
try {
ps=con.prepareStatement(sql);
ps.setString(1, dto.getName());
ps.setString(2, dto.getProtect());
ps.setLong(3, dto.getId());
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}finally{
closeAll();
}
}
public List<AnimalDto> findAll() {
List<AnimalDto> list = new ArrayList<AnimalDto>();
getCon();
String sql="select * from animal";
try {
ps=con.prepareStatement(sql);
rs=ps.executeQuery();
while(rs.next()){
AnimalDto dto=new AnimalDto();
dto.setId(rs.getLong("id"));
dto.setName(rs.getString("name"));
dto.setProtect(rs.getString("protect"));
list.add(dto);
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
closeAll();
}
return list;
}
public AnimalDto findById(Long id) {
AnimalDto dto=null;
getCon();
String sql="select * from animal where id=?";
try {
ps=con.prepareStatement(sql);
ps.setLong(1, id);
rs=ps.executeQuery();
if(rs.next()){
dto=new AnimalDto();
dto.setId(rs.getLong("id"));
dto.setName(rs.getString("name"));
dto.setProtect(rs.getString("protect"));
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
closeAll();
}
return dto;
}
}
3。相应的Servlet类(增删改查等方法)
package com.rw.mvc.model;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.rw.mvc.service.AnimalDao;
import com.rw.mvc.service.AnimalDto;
public class AddServlet extends HttpServlet {
/**
* 添加方法
*/
private static final long serialVersionUID = 2908161854747753038L;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String name=request.getParameter("name");
String protect =request.getParameter("protect");
AnimalDto dto=new AnimalDto(name,protect);
AnimalDao dao=new AnimalDao();
dao.Add(dto);
request.getRequestDispatcher("List").forward(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
//______________________________________
package com.rw.mvc.model;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.rw.mvc.service.AnimalDao;
import com.rw.mvc.service.AnimalDto;
public class ListServlet extends HttpServlet {
/**
* 显示所有list方法
*/
private static final long serialVersionUID = -3664344732067462587L;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
AnimalDao dao=new AnimalDao();
List<AnimalDto> list=dao.findAll();
request.setAttribute("lists",list);
request.getRequestDispatcher("animal.jsp").forward(request,response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
--------------------------------------------------------------------
package com.rw.mvc.model;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.rw.mvc.service.AnimalDao;
public class DelServlet extends HttpServlet {
/**
* 删除方法
*/
private static final long serialVersionUID = -4810804261572904259L;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String[] ids=request.getParameterValues("ids");
AnimalDao dao=new AnimalDao();
for(int i=0;i<ids.length;i++){
dao.Del(Long.valueOf(ids[i]));
}
request.getRequestDispatcher("List").forward(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
----------------------------------------------------------
package com.rw.mvc.model;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.rw.mvc.service.AnimalDao;
import com.rw.mvc.service.AnimalDto;
public class UpdateServlet extends HttpServlet {
/**
* 修改方法s
*/
private static final long serialVersionUID = 901060387987323705L;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Long id=Long.valueOf(request.getParameter("id"));
String name=request.getParameter("name");
String protect =request.getParameter("protect");
AnimalDto dto=new AnimalDto(id,name,protect);
AnimalDao dao=new AnimalDao();
dao.Update(dto);
request.getRequestDispatcher("List").forward(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
package com.rw.mvc.model;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.rw.mvc.service.AnimalDao;
import com.rw.mvc.service.AnimalDto;
public class DetailServlet extends HttpServlet {
/**
* 根据条件查询
*/
private static final long serialVersionUID = 597820565749066991L;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Long id=Long.valueOf(request.getParameter("id"));
AnimalDao dao=new AnimalDao();
AnimalDto dto=dao.findById(id);
request.setAttribute("dtos", dto);
request.getRequestDispatcher("animal_edit.jsp").forward(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
------------------------------------------------------------------------------------
4。web.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<filter>
<filter-name>EncodingFilter</filter-name>
<filter-class>com.rw.mvc.model.EncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>gb2312</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>EncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>AddServlet</servlet-name>
<servlet-class>com.rw.mvc.model.AddServlet</servlet-class>
</servlet>
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>ListServlet</servlet-name>
<servlet-class>com.rw.mvc.model.ListServlet</servlet-class>
</servlet>
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>DelServlet</servlet-name>
<servlet-class>com.rw.mvc.model.DelServlet</servlet-class>
</servlet>
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>UpdateServlet</servlet-name>
<servlet-class>com.rw.mvc.model.UpdateServlet</servlet-class>
</servlet>
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>DetailServlet</servlet-name>
<servlet-class>com.rw.mvc.model.DetailServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AddServlet</servlet-name>
<url-pattern>/Add</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>ListServlet</servlet-name>
<url-pattern>/List</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>DelServlet</servlet-name>
<url-pattern>/Del</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>UpdateServlet</servlet-name>
<url-pattern>/Update</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>DetailServlet</servlet-name>
<url-pattern>/Detail</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
5。相应的jsp页面(添加页面,显示页面,修改页面)
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title></head>
<body>
<form name="form1" method="post" action="Add">
<table width="419" height="188" border="1" align="center" cellpadding="0" cellspacing="0">
<tr>
<td colspan="2" bgcolor="#CCCCCC"><strong>信息录入:</strong></td>
</tr>
<tr>
<td width="131">动物</td>
<td width="282"><label>
<input type="text" name="name" id="name" />
</label></td>
</tr>
<tr>
<td>等级</td>
<td><input type="text" name="protect" id="protect" /></td>
</tr>
<tr>
<td align="right"><label>
<input type="submit" name="button" id="button" value="提交" />
</label></td>
<td><input type="reset" name="button2" id="button2" value="重置" /></td>
</tr>
</table>
<p> </p>
<p> </p>
<p> </p>
</form>
</body>
</html>
---------------------
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<%@ page import="java.util.*,com.rw.mvc.service.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title><style type="text/css">
<!--
.STYLE1 {font-size: 16px}
-->
</style></head>
<body>
<form id="form1" name="form1" method="post" action="Del">
<table width="815" height="125" border="1" align="center" cellpadding="0" cellspacing="0">
<tr>
<td colspan="6" bgcolor="#CCCCCC"><strong><span class="STYLE1">信息列表<a href="animal_add.jsp">继续添加</a></span></strong></td>
</tr>
<tr>
<td><label>
<input type="checkbox" name="selAll" id="selAll" />
<input type="submit" name="button" id="button" value="删除" />
</label></td>
<td>动物</td>
<td>等级</td>
<td>详情</td>
</tr>
<%
List list=(List)request.getAttribute("lists");
Iterator itr=list.iterator();
while(itr.hasNext()){
AnimalDto dto=(AnimalDto)itr.next();
%>
<tr>
<td><label>
<input type="checkbox" name="ids" id="ids" value="<%=dto.getId()%>" />
</label></td>
<td><%=dto.getName()%></td>
<td><%=dto.getProtect()%></td>
<td background="index.jsp"><a href="/Animal/Detail?id=<%=dto.getId()%>">详情</a></td>
</tr>
<%
}
%>
</table>
<p> </p>
<p> </p>
<p> </p>
</form>
</body>
</html>
-------------------------------
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<%@ page import="java.util.*,com.rw.mvc.service.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>
<body>
<%
AnimalDto dto = (AnimalDto)request.getAttribute("dtos");
%>
<form id="form1" name="form1" method="post" action="Update">
<input type="hidden" name="id" value="<%=dto.getId()%>" />
<table width="419" height="188" border="1" align="center" cellpadding="0" cellspacing="0">
<tr>
<td colspan="2" bgcolor="#CCCCCC"><strong>信息编辑:</strong></td>
</tr>
<tr>
<td width="131">动物</td>
<td width="282"><label>
<input type="text" name="name" id="name" value="<%=dto.getName() %>"/>
</label></td>
</tr>
<tr>
<td>等级</td>
<td><input type="text" name="protect" id="protect" value="<%=dto.getProtect() %>"/></td>
</tr>
<tr>
<td align="right"><label>
<input type="submit" name="button" id="button" value="编辑" />
</label></td>
<td><input type="reset" name="button2" id="button2" value="重置" /></td>
</tr>
</table>
<p> </p>
<p> </p>
<p> </p>
</form>
</body>
</html>