简单的jsp+servlet的web开发已经很少了~~但是开发流程对后来学框架还是挺有用的~~~
所以大概从简单的项目出发~~~简单的增删改查~~~~先好好理解好业务的流程~~~~
用的是简单的重复编程工作(重新封装->对象),所以大概就把源码跟步骤列一下好了~~~
连接类:JDBC_Connection.java
static{ try{ Class.forName(drivername); System.out.println("创建驱动成功!"); }catch(ClassNotFoundException e){ System.out.println("创建驱动失败!请检查驱动!"); e.printStackTrace(); } } public static Connection getConnection(){} public static void free(ResultSet rs,Connection conn,Statement stmt){}
在getConnection()方法中主要有这句(获得链接):
conn = (Connection)DriverManager.getConnection(url, username, password);
用户操作:
(1)增加用户
addUser.jsp
主要有这句:
<form action="AddUserServlet" method="post" >
AddUser.java
public void add(UserVo userVo){ Connection conn = null; PreparedStatement pstm = null; ResultSet rs = null; try{ conn = JDBC_Connection.getConnection(); String sql = "insert into user(id,name,age,tel,adress) value (?,?,?,?,?)"; pstm = conn.prepareStatement(sql); pstm.setInt(1,userVo.getId()); pstm.setString(2, userVo.getName()); pstm.setInt(3, userVo.getAge()); pstm.setString(4, userVo.getTel()); pstm.setString(5, userVo.getAdress()); pstm.executeUpdate(); System.out.println("添加成功,添加的内容如下:"); System.out.println("id:"+userVo.getId()+"\t name:"+userVo.getName()+"\t age:"+userVo.getAge()+ "\t tel:"+userVo.getTel()+"\t adress:"+userVo.getAdress()); }catch(Exception e){ e.printStackTrace(); }finally{ JDBC_Connection.free(rs, conn, pstm); } }
AddUserServlet.java
注意:doPost(HttpServletRequest request, HttpServletResponse response)throws...{}
int id = Integer.parseInt(request.getParameter("id")); String name = request.getParameter("name"); int age = Integer.parseInt(request.getParameter("age")); String tel = request.getParameter("tel"); String adress = request.getParameter("adress"); UserVo userVo = new UserVo(); userVo.setId(id); userVo.setName(name); userVo.setAge(age); userVo.setTel(tel); userVo.setAdress(adress); AddUser addUser = new AddUser();//声明一个AddUser对象 addUser.add(userVo);//调用AddUser对象的add()方法, request.getRequestDispatcher("addUsers.jsp").forward(request, response);//转发到指定页面
在web.xml下的配置:
<servlet> <servlet-name>AddUserServlet</servlet-name> <servlet-class>com.cn.add.AddUserServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>AddUserServlet</servlet-name> <url-pattern>/AddUserServlet</url-pattern> </servlet-mapping>
(2)查询所有用户
Query.java
List<UserVo> list = new ArrayList<UserVo>(); try{ conn = JDBC_Connection.getConnection(); stmt = conn.createStatement(); rs = stmt.executeQuery("select * from user"); while(rs.next()){ UserVo userVo = new UserVo(); userVo.setId(rs.getInt("id")); userVo.setName(rs.getString("name")); userVo.setAge(rs.getInt("age")); userVo.setTel(rs.getString("tel")); userVo.setAdress(rs.getString("adress")); list.add(userVo); } }
ShowAllUserServlet.java
List<UserVo> list = new ArrayList<UserVo>(); Query query = new Query(); list = query.showUser(); request.setAttribute("list", list); request.getRequestDispatcher("showAllUser.jsp").forward(request, response);
ShowAllUser.jsp
<c:forEach items = "${list }" var = "list"> <tr> <td>${list.id }</td> <td>${list.name }</td> <td>${list.age }</td> <td>${list.tel }</td> <td>${list.adress }</td> <td> <a href="QueryUserByIdServlet?id=${list.id }">修改</a> <a href="DeleteUserServlet?id=${list.id }">删除</a> </td> </tr> </c:forEach>
(3)查询一个用户
QueryById.java
pstmt = conn.prepareStatement("select * from user where id = ?"); pstmt.setInt(1, id); rs = pstmt.executeQuery(); return userVo;
QueryUserById.java
UserVo userVo = new UserVo(); QueryById queryById = new QueryById(); userVo = queryById.queryUserById(id); request.setAttribute("userVo", userVo); request.getRequestDispatcher("updateUser.jsp").forward(request, response);
UpdateUser.java
String sql = "update user set id = ?,name = ?,age = ?,tel = ?,address = ? where id = ?";
UpdateUserServlet.java
int id = Integer.parseInt(request.getParameter("id")); String name = request.getParameter("name"); int age = Integer.parseInt(request.getParameter("age")); String tel = request.getParameter("tel"); String adress = request.getParameter("adress"); UserVo userVo =new UserVo(); userVo.setId(id); userVo.setName(name); userVo.setAge(age); userVo.setTel(tel); userVo.setAdress(adress); UpdateUser updateUser = new UpdateUser(); updateUser.update(userVo); response.sendRedirect("ShowAllUserServlet");
(4)删除用户
DeleteUser.java
String sql = "delete from user where id = ?";
DeleteUserServelt.java
int id = Integer.parseInt(request.getParameter("id")); DeleteUser deleteUser = new DeleteUser(); deleteUser.deleteUser(id); response.sendRedirect("ShowAllUserServlet");
关于中文的配置,还可以利用过滤器Filter
Zh_Filter.java
public class Zh_Filter implements Filter { public void doFilter(ServletRequest request , ServletResponse response, FilterChain chain) throws IOException, ServletException{ request.setCharacterEncoding("gb2312"); response.setCharacterEncoding("gb2312"); chain.doFilter(request, response); } }
在web.xml中配置
<filter> <filter-name>filter</filter-name> <filter-class>com.cn.filter.Zh_Filter</filter-class> </filter> <filter-mapping> <filter-name>filter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
另外,小知识点:
(1)网上提示关于servlet的forword和redirect的区别:
forward是服务器请求资源,服务器直接访问目标地址的URL,把那个URL的响应内容读取过来,然后把这些内容再发给浏览器,浏览器根本不知道服务器发送的内容是从哪儿来的,所以它的地址栏中还是原来的地址。
redirect就是服务端根据逻辑,发送一个状态码,告诉浏览器重新去请求那个地址,一般来说浏览器会用刚才请求的所有参数重新请求,所以session,request参数都可以获取。
Forward:request中的数据不会丢失,sendRedirect:request请求中的数据会丢失
前者仅是容器中控制权的转向,在客户端浏览器地址栏中不会显示出转向后的地址;后者则是完全的跳转,浏览器将会得到跳转的地址,并重新发送请求链接。这样,从浏览器的地址栏中可以看到跳转后的链接地址。所以,前者更加高效,在前者可以满足需要时,尽量使用forward()方法,并且,这样也有助于隐藏实际的链接。在有些情况下,比如,需要跳转到一个其它服务器上的资源,则必须使用sendRedirect()方法
(2)
Request.getAttribute()是获取通过Request.setAttribute()中的数据
Request.getParameter()是获取表单请求过来的数据