javaWebDemo
一:设计数据库
1)新建数据库(我这里用的是MySQL)
/*
Navicat MySQL Data Transfer
Source Server : localhost_3306
Source Server Version : 50022
Source Host : localhost:3306
Source Database : a
Target Server Type : MYSQL
Target Server Version : 50022
File Encoding : 65001
Date: 2017-09-28 09:19:33
*/
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`userid` int(11) NOT NULL,
`name` varchar(20) default NULL,
`sex` varchar(10) default NULL,
`age` int(11) default NULL,
PRIMARY KEY (`userid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES ('1', 'torry', '女', '21');
INSERT INTO `user` VALUES ('2', 'tom', '男', '21');
INSERT INTO `user` VALUES ('3', '李元芳', '女', '20');
二:mvc模式
1)数据库连接
1 package com.qiu.demo.dbc; 2 3 import javax.sql.DataSource; 4 5 import org.apache.commons.dbcp.BasicDataSource; 6 7 public class DataSourceHolder { 8 //基本数据源 9 BasicDataSource ds = new BasicDataSource(); 10 //构造方法加载数据库驱动 11 public DataSourceHolder(){ 12 ds.setDriverClassName("com.mysql.jdbc.Driver"); 13 ds.setUrl("jdbc:mysql://localhost:3306/a?useUnicode=true&characterEnxoding=gb2312"); 14 ds.setUsername("root"); 15 ds.setPassword("root"); 16 } 17 //内部单例类,实例化DataSourceHolder的对象 18 private static class SingletonHolder{ 19 private static DataSourceHolder instance = new DataSourceHolder(); 20 } 21 //获得内部类中的实例化好的外部类对象 22 public static DataSourceHolder getInstance(){ 23 return SingletonHolder.instance; 24 } 25 //获得数据源对象 26 public DataSource getDataSource(){ 27 return ds; 28 } 29 }
2)实体类
1 package com.qiu.demo.vo; 2 3 public class UserModel { 4 private int userId; 5 private String name; 6 private String sex; 7 private int age; 8 //----------------- 9 public int getUserId() { 10 return userId; 11 } 12 public void setUserId(int userId) { 13 this.userId = userId; 14 } 15 public String getName() { 16 return name; 17 } 18 public void setName(String name) { 19 this.name = name; 20 } 21 public String getSex() { 22 return sex; 23 } 24 public void setSex(String sex) { 25 this.sex = sex; 26 } 27 public int getAge() { 28 return age; 29 } 30 public void setAge(int age) { 31 this.age = age; 32 } 33 //--------------------- 34 @Override 35 public int hashCode() { 36 // TODO Auto-generated method stub 37 final int prime=31; 38 int result = 1; 39 result = prime*result+userId; 40 return result; 41 } 42 @Override 43 public boolean equals(Object obj) { 44 // TODO Auto-generated method stub 45 if(this == obj){ 46 return true; 47 } 48 if(obj==null){ 49 return false; 50 } 51 if(this.getClass() != obj.getClass()){ 52 return false; 53 } 54 final UserModel other = (UserModel)obj; 55 if(userId != other.userId){ 56 return false; 57 } 58 return true; 59 } 60 }
3)Dao层
1 package com.qiu.demo.dao; 2 3 import java.util.List; 4 5 import com.qiu.demo.vo.UserModel; 6 7 public interface UserDao { 8 //查询所有,返回集合 9 public List<UserModel> findAll(); 10 //删除 11 public void delete(int userId); 12 //添加 13 public void add(UserModel usermodel); 14 //根据id查询信息 15 public UserModel findById(int userId); 16 //修改信息 17 public void edit(UserModel usermodel); 18 //条件组合查询 19 public List<UserModel> findgroup(UserModel usermodel); 20 }
4)Dao实现
1 package com.qiu.demo.dao.impl; 2 3 import java.sql.Connection; 4 import java.sql.PreparedStatement; 5 import java.sql.ResultSet; 6 import java.sql.SQLException; 7 import java.util.ArrayList; 8 import java.util.List; 9 10 import com.qiu.demo.dao.UserDao; 11 import com.qiu.demo.dbc.DataSourceHolder; 12 import com.qiu.demo.vo.UserModel; 13 14 public class UserDaoImpl implements UserDao { 15 16 @Override 17 public List<UserModel> findAll() { 18 Connection conn = null; 19 List<UserModel> userList = new ArrayList<UserModel>(); 20 try{ 21 conn = DataSourceHolder.getInstance().getDataSource().getConnection(); 22 String sql ="SELECT userId,name,sex,age FROM user"; 23 PreparedStatement ps =conn.prepareStatement(sql); 24 ResultSet res = ps.executeQuery(); 25 while(res.next()){ 26 UserModel user = this.rs2Model(res); 27 userList.add(user); 28 } 29 res.close(); 30 ps.close(); 31 }catch(Exception e){ 32 e.printStackTrace(); 33 }finally{ 34 try { 35 conn.close(); 36 } catch (SQLException e) { 37 // TODO Auto-generated catch block 38 e.printStackTrace(); 39 } 40 } 41 return userList; 42 } 43 //----------------------------查询多数的方法封装 44 private UserModel rs2Model(ResultSet res)throws Exception{ 45 UserModel user = new UserModel(); 46 user.setUserId(res.getInt("userId")); 47 user.setName(res.getString("name")); 48 user.setSex(res.getString("sex")); 49 user.setAge(res.getInt("age")); 50 return user; 51 } 52 //------------------------------删除 53 @Override 54 public void delete(int userId) { 55 Connection conn = null; 56 57 try{ 58 conn = DataSourceHolder.getInstance().getDataSource().getConnection(); 59 String sql ="DELETE FROM user WHERE userId=?"; 60 PreparedStatement ps =conn.prepareStatement(sql); 61 ps.setInt(1, userId); 62 ps.execute(); 63 ps.close(); 64 }catch(Exception e){ 65 e.printStackTrace(); 66 }finally{ 67 try { 68 conn.close(); 69 } catch (SQLException e) { 70 // TODO Auto-generated catch block 71 e.printStackTrace(); 72 } 73 } 74 75 } 76 //-------------------------------添加 77 @Override 78 public void add(UserModel usermodel) { 79 Connection conn = null; 80 try{ 81 conn = DataSourceHolder.getInstance().getDataSource().getConnection(); 82 String sql ="INSERT INTO user (userId,name,sex,age) VALUES (?,?,?,?) "; 83 PreparedStatement ps = conn.prepareStatement(sql); 84 int count =1; 85 ps.setInt(count++, usermodel.getUserId()); 86 ps.setString(count++, usermodel.getName()); 87 ps.setString(count++, usermodel.getSex()); 88 ps.setInt(count++, usermodel.getAge()); 89 ps.executeUpdate(); 90 ps.close(); 91 }catch(Exception e){ 92 e.printStackTrace(); 93 }finally{ 94 try { 95 conn.close(); 96 } catch (SQLException e) { 97 // TODO Auto-generated catch block 98 e.printStackTrace(); 99 } 100 } 101 102 } 103 //----------------------按id查询 104 @Override 105 public UserModel findById(int userId) { 106 Connection conn = null; 107 UserModel user = new UserModel(); 108 try{ 109 conn = DataSourceHolder.getInstance().getDataSource().getConnection(); 110 String sql ="SELECT userId,name,sex,age FROM user WHERE userId=?"; 111 PreparedStatement ps =conn.prepareStatement(sql); 112 ps.setInt(1, userId); 113 ResultSet res = ps.executeQuery(); 114 if(res.next()){ 115 user = this.rs2Model(res); 116 } 117 res.close(); 118 ps.close(); 119 }catch(Exception e){ 120 e.printStackTrace(); 121 }finally{ 122 try { 123 conn.close(); 124 } catch (SQLException e) { 125 // TODO Auto-generated catch block 126 e.printStackTrace(); 127 } 128 } 129 return user; 130 } 131 //------------------修改信息 132 @Override 133 public void edit(UserModel usermodel) { 134 Connection conn = null; 135 try{ 136 conn = DataSourceHolder.getInstance().getDataSource().getConnection(); 137 String sql ="UPDATE user SET name=?,sex=?,age=? WHERE userId=?"; 138 PreparedStatement ps =conn.prepareStatement(sql); 139 int count =1; 140 ps.setString(count++,usermodel.getName()); 141 ps.setString(count++,usermodel.getSex()); 142 ps.setInt(count++, usermodel.getAge()); 143 ps.setInt(count++, usermodel.getUserId()); 144 ps.executeUpdate(); 145 ps.close(); 146 }catch(Exception e){ 147 e.printStackTrace(); 148 }finally{ 149 try { 150 conn.close(); 151 } catch (SQLException e) { 152 // TODO Auto-generated catch block 153 e.printStackTrace(); 154 } 155 } 156 157 } 158 //------------------组合查询优化版 159 @Override 160 public List<UserModel> findgroup(UserModel usermodel) { 161 Connection conn = null; 162 List<UserModel> userList = new ArrayList<UserModel>(); 163 try{ 164 conn = DataSourceHolder.getInstance().getDataSource().getConnection(); 165 String sql ="SELECT userId,name,sex,age FROM user WHERE 1=1 "+this.generateWhere(usermodel); 166 PreparedStatement ps =conn.prepareStatement(sql); 167 this.priparedPs(usermodel, ps); 168 System.out.println(sql); 169 ResultSet res = ps.executeQuery(); 170 while(res.next()){ 171 UserModel user = this.rs2Model(res); 172 userList.add(user); 173 } 174 res.close(); 175 ps.close(); 176 }catch(Exception e){ 177 e.printStackTrace(); 178 }finally{ 179 try { 180 conn.close(); 181 } catch (SQLException e) { 182 // TODO Auto-generated catch block 183 e.printStackTrace(); 184 } 185 } 186 return userList; 187 188 } 189 //----------------------------条件组合用到的方法 190 private String generateWhere(UserModel usermodel){ 191 StringBuffer buf = new StringBuffer(); 192 if(usermodel.getUserId()> 0){ 193 buf.append(" AND userId= ? "); 194 } 195 if(usermodel.getName()!=null && usermodel.getName().trim().length()>0){ 196 buf.append(" AND name LIKE ? "); 197 198 } 199 if(usermodel.getSex()!=null && usermodel.getSex().trim().length()>0){ 200 buf.append(" AND sex= ? ") ; 201 202 } 203 if(usermodel.getAge()>0){ 204 buf.append(" AND age = ? "); 205 } 206 return buf.toString(); 207 } 208 private void priparedPs(UserModel usermodel,PreparedStatement ps)throws Exception{ 209 int count=1; 210 if(usermodel.getUserId()>0){ 211 ps.setInt(count++, usermodel.getUserId()); 212 } 213 if(usermodel.getName()!=null && usermodel.getName().trim().length()>0){ 214 ps.setString(count++, "%"+usermodel.getName()+"%"); 215 } 216 if(usermodel.getSex()!=null && usermodel.getSex().trim().length()>0){ 217 ps.setString(count++, usermodel.getSex()); 218 } 219 if(usermodel.getAge()>0){ 220 ps.setInt(count++, usermodel.getAge()); 221 } 222 } 223 224 }
5)代理工厂
1 package com.qiu.demo.dao.factory; 2 3 import java.lang.reflect.InvocationHandler; 4 import java.lang.reflect.Method; 5 import java.lang.reflect.Proxy; 6 7 import com.qiu.demo.dao.UserDao; 8 import com.qiu.demo.dao.impl.UserDaoImpl; 9 10 public class UserDaoProxyFactory implements InvocationHandler { 11 //实现InvocationHandler 12 //位于的包是java.lang.reflect.InvocationHandler 13 private UserDao us; 14 public UserDaoProxyFactory(UserDao us){ 15 this.us = us; 16 } 17 public UserDao getUserServiceProxy(){ 18 //生成动态代理 19 UserDao usProxy =(UserDao)Proxy.newProxyInstance(UserDao.class.getClassLoader(),UserDaoImpl.class.getInterfaces(),this ); 20 return usProxy; 21 } 22 @Override 23 public Object invoke(Object proxy, Method method, Object[] args) 24 throws Throwable { 25 //进行事务处理操作 26 Object invoke = method.invoke(us, args); 27 return invoke; 28 } 29 30 }
6)Servlet
1 package com.qiu.demo.servlet; 2 3 import java.io.IOException; 4 import java.util.ArrayList; 5 import java.util.List; 6 7 import javax.servlet.ServletException; 8 import javax.servlet.http.HttpServlet; 9 import javax.servlet.http.HttpServletRequest; 10 import javax.servlet.http.HttpServletResponse; 11 12 import com.qiu.demo.dao.UserDao; 13 import com.qiu.demo.dao.factory.UserDaoProxyFactory; 14 import com.qiu.demo.dao.impl.UserDaoImpl; 15 import com.qiu.demo.vo.UserModel; 16 17 /** 18 * 查询所有信息到list页面 19 */ 20 public class ListServlet extends HttpServlet { 21 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 22 // 查询所有信息 23 request.setCharacterEncoding("gb2312"); 24 List<UserModel> userList = new ArrayList<UserModel>(); 25 //----------------- 26 UserDao us= new UserDaoImpl(); 27 UserDaoProxyFactory factory = new UserDaoProxyFactory(us); 28 UserDao usProxy = factory.getUserServiceProxy(); 29 userList=usProxy.findAll(); 30 //-----------------存入作用域 31 request.setAttribute("userList", userList); 32 request.getRequestDispatcher("list.jsp").forward(request, response); 33 34 } 35 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 36 this.doGet(request, response); 37 } 38 39 }
1 package com.qiu.demo.servlet; 2 3 import java.io.IOException; 4 import java.util.ArrayList; 5 import java.util.List; 6 7 import javax.servlet.ServletException; 8 import javax.servlet.annotation.WebServlet; 9 import javax.servlet.http.HttpServlet; 10 import javax.servlet.http.HttpServletRequest; 11 import javax.servlet.http.HttpServletResponse; 12 13 import com.qiu.demo.dao.UserDao; 14 import com.qiu.demo.dao.factory.UserDaoProxyFactory; 15 import com.qiu.demo.dao.impl.UserDaoImpl; 16 import com.qiu.demo.vo.UserModel; 17 18 /** 19 * 条件组合查询 20 */ 21 @WebServlet("/UserFindServlet") 22 public class UserFindServlet extends HttpServlet { 23 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 24 request.setCharacterEncoding("gb2312"); 25 System.out.println("qqqqqqqqqqqqqqqq"); 26 List<UserModel> userList = new ArrayList<UserModel>(); 27 //------------------------------- 28 int userId = Integer.parseInt(request.getParameter("userId")); 29 System.out.println(userId); 30 String name = request.getParameter("name"); 31 System.out.println(name); 32 String sex = request.getParameter("sex"); 33 System.out.println(sex); 34 int age =Integer.parseInt(request.getParameter("age")); 35 //------------封装数据 36 UserModel usermodel = new UserModel(); 37 usermodel.setUserId(userId); 38 usermodel.setName(name); 39 usermodel.setSex(sex); 40 usermodel.setAge(age); 41 //-------------查询操作 42 UserDao us= new UserDaoImpl(); 43 UserDaoProxyFactory factory = new UserDaoProxyFactory(us); 44 UserDao usProxy = factory.getUserServiceProxy(); 45 userList = usProxy.findgroup(usermodel); 46 //-------------转发到list.jsp 47 request.setAttribute("userList", userList); 48 request.getRequestDispatcher("list.jsp").forward(request, response); 49 } 50 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 51 this.doGet(request, response); 52 } 53 54 }
1 package com.qiu.demo.servlet; 2 3 import java.io.IOException; 4 import javax.servlet.ServletException; 5 import javax.servlet.annotation.WebServlet; 6 import javax.servlet.http.HttpServlet; 7 import javax.servlet.http.HttpServletRequest; 8 import javax.servlet.http.HttpServletResponse; 9 10 /** 11 * to添加页面 12 */ 13 @WebServlet("/UserToAddServlet") 14 public class UserToAddServlet extends HttpServlet { 15 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 16 request.setCharacterEncoding("gb2312"); 17 request.getRequestDispatcher("userAdd.jsp").forward(request, response); 18 } 19 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 20 this.doGet(request, response); 21 } 22 23 }
1 package com.qiu.demo.servlet; 2 3 import java.io.IOException; 4 import javax.servlet.ServletException; 5 import javax.servlet.annotation.WebServlet; 6 import javax.servlet.http.HttpServlet; 7 import javax.servlet.http.HttpServletRequest; 8 import javax.servlet.http.HttpServletResponse; 9 10 import com.qiu.demo.dao.UserDao; 11 import com.qiu.demo.dao.factory.UserDaoProxyFactory; 12 import com.qiu.demo.dao.impl.UserDaoImpl; 13 import com.qiu.demo.vo.UserModel; 14 15 /** 16 * 添加操作 17 */ 18 public class UserAddServlet extends HttpServlet { 19 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 20 // TODO Auto-generated method stub 21 request.setCharacterEncoding("gb2312"); 22 int userId = Integer.parseInt(request.getParameter("userId")); 23 String name = request.getParameter("name"); 24 String sex = request.getParameter("sex"); 25 int age =Integer.parseInt(request.getParameter("age")); 26 //------------封装数据 27 UserModel usermodel = new UserModel(); 28 usermodel.setUserId(userId); 29 usermodel.setName(name); 30 usermodel.setSex(sex); 31 usermodel.setAge(age); 32 //-------------添加操作 33 UserDao us= new UserDaoImpl(); 34 UserDaoProxyFactory factory = new UserDaoProxyFactory(us); 35 UserDao usProxy = factory.getUserServiceProxy(); 36 usProxy.add(usermodel); 37 //-------------重定向到ListServlet 38 response.sendRedirect("listServlet"); 39 } 40 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 41 this.doGet(request, response); 42 } 43 44 }
1 package com.qiu.demo.servlet; 2 3 import java.io.IOException; 4 import javax.servlet.ServletException; 5 import javax.servlet.annotation.WebServlet; 6 import javax.servlet.http.HttpServlet; 7 import javax.servlet.http.HttpServletRequest; 8 import javax.servlet.http.HttpServletResponse; 9 10 import com.qiu.demo.dao.UserDao; 11 import com.qiu.demo.dao.factory.UserDaoProxyFactory; 12 import com.qiu.demo.dao.impl.UserDaoImpl; 13 import com.qiu.demo.vo.UserModel; 14 15 /** 16 * Servlet implementation class UserToEditServlet 17 */ 18 @WebServlet("/UserToEditServlet") 19 public class UserToEditServlet extends HttpServlet { 20 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 21 request.setCharacterEncoding("gb2312"); 22 UserModel user = new UserModel(); 23 //获取id 24 int userId = Integer.parseInt(request.getParameter("userId")); 25 //查询出信息 26 UserDao us= new UserDaoImpl(); 27 UserDaoProxyFactory factory = new UserDaoProxyFactory(us); 28 UserDao usProxy = factory.getUserServiceProxy(); 29 user=usProxy.findById(userId); 30 //-----------------存入作用域 31 request.setAttribute("user", user); 32 request.getRequestDispatcher("edit.jsp").forward(request, response); 33 } 34 35 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 36 this.doGet(request, response); 37 } 38 39 }
1 package com.qiu.demo.servlet; 2 3 import java.io.IOException; 4 import javax.servlet.ServletException; 5 import javax.servlet.annotation.WebServlet; 6 import javax.servlet.http.HttpServlet; 7 import javax.servlet.http.HttpServletRequest; 8 import javax.servlet.http.HttpServletResponse; 9 10 import com.qiu.demo.dao.UserDao; 11 import com.qiu.demo.dao.factory.UserDaoProxyFactory; 12 import com.qiu.demo.dao.impl.UserDaoImpl; 13 import com.qiu.demo.vo.UserModel; 14 15 /** 16 * 修改操作 17 */ 18 @WebServlet("/UserEditServlet") 19 public class UserEditServlet extends HttpServlet { 20 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 21 request.setCharacterEncoding("gb2312"); 22 int userId = Integer.parseInt(request.getParameter("userId")); 23 String name = request.getParameter("name"); 24 String sex = request.getParameter("sex"); 25 int age =Integer.parseInt(request.getParameter("age")); 26 //------------封装数据 27 UserModel usermodel = new UserModel(); 28 usermodel.setUserId(userId); 29 usermodel.setName(name); 30 usermodel.setSex(sex); 31 usermodel.setAge(age); 32 //-------------修改操作 33 UserDao us= new UserDaoImpl(); 34 UserDaoProxyFactory factory = new UserDaoProxyFactory(us); 35 UserDao usProxy = factory.getUserServiceProxy(); 36 usProxy.edit(usermodel); 37 //-------------重定向到ListServlet 38 response.sendRedirect("listServlet"); 39 } 40 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 41 this.doGet(request, response); 42 } 43 44 }
1 package com.qiu.demo.servlet; 2 3 import java.io.IOException; 4 import javax.servlet.ServletException; 5 import javax.servlet.annotation.WebServlet; 6 import javax.servlet.http.HttpServlet; 7 import javax.servlet.http.HttpServletRequest; 8 import javax.servlet.http.HttpServletResponse; 9 10 import com.qiu.demo.dao.UserDao; 11 import com.qiu.demo.dao.factory.UserDaoProxyFactory; 12 import com.qiu.demo.dao.impl.UserDaoImpl; 13 14 /** 15 * 删除操作 16 */ 17 public class UserDelete extends HttpServlet { 18 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 19 request.setCharacterEncoding("gb2312"); 20 int userId = Integer.parseInt(request.getParameter("userId")); 21 //-----------------------------删除 22 UserDao us= new UserDaoImpl(); 23 UserDaoProxyFactory factory = new UserDaoProxyFactory(us); 24 UserDao usProxy = factory.getUserServiceProxy(); 25 usProxy.delete(userId); 26 //-----------------------------重定向到listServlet 27 response.sendRedirect("listServlet"); 28 29 } 30 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 31 this.doGet(request, response); 32 } 33 34 }
7)同步配置web.xml文件
这里的配置是:每当6)有一个Servlet就相应在web.xml中写对应的配置(!!!切记)
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> 3 <display-name>jdbcdemospring</display-name> 4 <welcome-file-list> 5 <welcome-file>index.html</welcome-file> 6 <welcome-file>index.htm</welcome-file> 7 <welcome-file>index.jsp</welcome-file> 8 <welcome-file>default.html</welcome-file> 9 <welcome-file>default.htm</welcome-file> 10 <welcome-file>default.jsp</welcome-file> 11 </welcome-file-list> 12 <servlet> 13 <servlet-name>listServlet</servlet-name> 14 <servlet-class>com.qiu.demo.servlet.ListServlet</servlet-class> 15 </servlet> 16 <servlet-mapping> 17 <servlet-name>listServlet</servlet-name> 18 <url-pattern>/listServlet</url-pattern> 19 </servlet-mapping> 20 <servlet> 21 <servlet-name>userDelete</servlet-name> 22 <servlet-class>com.qiu.demo.servlet.UserDelete</servlet-class> 23 </servlet> 24 <servlet-mapping> 25 <servlet-name>userDelete</servlet-name> 26 <url-pattern>/userDelete</url-pattern> 27 </servlet-mapping> 28 <servlet> 29 <servlet-name>userToAddServlet</servlet-name> 30 <servlet-class>com.qiu.demo.servlet.UserToAddServlet</servlet-class> 31 </servlet> 32 <servlet-mapping> 33 <servlet-name>userToAddServlet</servlet-name> 34 <url-pattern>/userToAddServlet</url-pattern> 35 </servlet-mapping> 36 <servlet> 37 <servlet-name>userAddServlet</servlet-name> 38 <servlet-class>com.qiu.demo.servlet.UserAddServlet</servlet-class> 39 </servlet> 40 <servlet-mapping> 41 <servlet-name>userAddServlet</servlet-name> 42 <url-pattern>/userAddServlet</url-pattern> 43 </servlet-mapping> 44 <servlet> 45 <servlet-name>userToEditServlet</servlet-name> 46 <servlet-class>com.qiu.demo.servlet.UserToEditServlet</servlet-class> 47 </servlet> 48 <servlet-mapping> 49 <servlet-name>userToEditServlet</servlet-name> 50 <url-pattern>/userToEditServlet</url-pattern> 51 </servlet-mapping> 52 <servlet> 53 <servlet-name>userEditServlet</servlet-name> 54 <servlet-class>com.qiu.demo.servlet.UserEditServlet</servlet-class> 55 </servlet> 56 <servlet-mapping> 57 <servlet-name>userEditServlet</servlet-name> 58 <url-pattern>/userEditServlet</url-pattern> 59 </servlet-mapping> 60 61 62 <servlet> 63 <servlet-name>userFindServlet</servlet-name> 64 <servlet-class>com.qiu.demo.servlet.UserFindServlet</servlet-class> 65 </servlet> 66 <servlet-mapping> 67 <servlet-name>userFindServlet</servlet-name> 68 <url-pattern>/userFindServlet</url-pattern> 69 </servlet-mapping> 70 </web-app>
8)页面
*这里按照个人习惯,我是每次用到一个小功能就写一个相应的页面
1 <%@ page language="java" contentType="text/html; charset=gb2312" 2 pageEncoding="gb2312"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> 7 <title>主页做跳转</title> 8 </head> 9 <body> 10 <% 11 response.sendRedirect("listServlet"); 12 %> 13 </body> 14 </html>
1 <%@ page language="java" contentType="text/html; charset=gb2312" 2 pageEncoding="gb2312"%> 3 <%@ page import="java.util.*" %> 4 <%@ page import="com.qiu.demo.vo.UserModel" %> 5 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 6 <html> 7 <head> 8 <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> 9 <title>信息显示页面</title> 10 </head> 11 <body> 12 <form action="" method="post"> 13 <table> 14 <tr> 15 <td>编号:</td> 16 <td>姓名:</td> 17 <td>性别:</td> 18 <td>年龄:</td> 19 <td>操作:</td> 20 </tr> 21 <% 22 request.setCharacterEncoding("gb2312"); 23 %> 24 <% 25 List<UserModel> userList = (List<UserModel>)request.getAttribute("userList"); 26 if(userList!=null){ 27 Iterator<UserModel> iter = userList.iterator(); 28 while(iter.hasNext()){ 29 UserModel user = iter.next(); 30 %> 31 <tr> 32 <td><%=user.getUserId() %> </td> 33 <td><%=user.getName() %> </td> 34 <td><%=user.getSex() %> </td> 35 <td><%=user.getAge() %> </td> 36 <td><a href ="userToEditServlet?userId=<%=user.getUserId()%>">修改</a>|<a href ="javascript:if(confirm('你确定要删除吗?'))window.location.href='userDelete?userId=<%=user.getUserId()%>'">删除</a> </td> 37 </tr> 38 <% 39 } 40 } 41 %> 42 </table> 43 <a href="userToAddServlet">添加用户</a> 44 <a href ="userFind.jsp">查询用户</a> 45 46 </form> 47 </body> 48 </html>
1 <%@ page language="java" contentType="text/html; charset=gb2312" 2 pageEncoding="gb2312"%> 3 <%@ page import="com.qiu.demo.vo.UserModel"%> 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 5 <html> 6 <head> 7 <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> 8 <title>修改页面</title> 9 </head> 10 <body> 11 <% 12 request.setCharacterEncoding("gb2312"); 13 %> 14 <% 15 UserModel user = (UserModel)request.getAttribute("user"); 16 if(user!=null){ 17 18 %> 19 <form action="userEditServlet" method="post"> 20 编号:<input type="text" name="userId" value="<%=user.getUserId() %>" readonly="readonly"/><br/> 21 姓名:<input type="text" name="name" value="<%=user.getName() %>"/><br/> 22 性别:<select name="sex"> 23 <option value="<%=user.getSex() %>">男</option> 24 <option>女</option> 25 </select><br/> 26 年龄:<input type="text" name="age" value="<%=user.getAge() %>"><br/> 27 <input type="submit" value="编辑"/> 28 </form> 29 <% 30 } 31 %> 32 33 </body> 34 </html>
1 <%@ page language="java" contentType="text/html; charset=gb2312" 2 pageEncoding="gb2312"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> 7 <title>添加页面</title> 8 </head> 9 <body> 10 <form action="userAddServlet" method="post"> 11 编号:<input type="text" name="userId" value="0"/><br/> 12 姓名:<input type="text" name="name" /><br/> 13 性别:<select name="sex"> 14 <option>男</option> 15 <option>女</option> 16 </select><br/> 17 年龄:<input type="text" name="age" value="0"><br/> 18 <input type="submit" value="添加"/> 19 </form> 20 </body> 21 </html>
1 <%@ page language="java" contentType="text/html; charset=gb2312" 2 pageEncoding="gb2312"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> 7 <title>条件查询页面</title> 8 </head> 9 <body> 10 <form action="userFindServlet" method="post"> 11 编号:<input type="text" name="userId" value="0"/><br/> 12 姓名:<input type="text" name="name"/><br/> 13 性别:<select name="sex"> 14 <option value=""></option> 15 <option>男</option> 16 <option>女</option> 17 </select><br/> 18 年龄:<input type="text" name="age" value="0"><br/> 19 <input type="submit" value="查询"/> 20 </form> 21 </body> 22 </html>
三:发布运行
这里我用的是Tomcat v7.0,后面就不用多说了,什么调bug是程序员必备技能(纸上得来终觉浅,绝知此事要躬行)。
四:感悟
每个人写代码习惯不同,建议整理思路后再下手。
五:我只是一只菜鸟,欢迎大神纠正(在此谢过)。