servlet 增删改查

package web;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.AccountDao;
import dao.common.DaoFactory;
import entity.Account;

@WebServlet(urlPatterns="/add")
public class AccountAddServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2547680690878139285L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String name=req.getParameter("name");
		String age=req.getParameter("age");
		String address=req.getParameter("address");
		//创建JavaBean对象
		Account account=new Account();
		account.setName(name);
		account.setAge(Integer.parseInt(age));
		account.setAddress(address);
		//Dao工厂
		
			try {
				AccountDao accountDao = DaoFactory.getInstance("accountDao", AccountDao.class);
				accountDao.save(account);
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			resp.sendRedirect("list.do");
	}


}
package web;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.AccountDao;
import dao.common.DaoFactory;
import entity.Account;

@WebServlet(urlPatterns="/delete.do")
public class AccountDeleteServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String id=req.getParameter("id");
		
		try {
			AccountDao accountDao=DaoFactory.getInstance("accountDao", AccountDao.class);
			accountDao.delete(Integer.parseInt(id));
			resp.sendRedirect("list.do");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
package web;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.AccountDao;
import dao.common.DaoFactory;
import entity.Account;

@WebServlet(urlPatterns="/edit.do")
public class AccountEditServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7060829902603581735L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		String id=req.getParameter("id");
		
		try {
			AccountDao accountDao=DaoFactory.getInstance("accountDao", AccountDao.class);
			Account account=accountDao.get(Integer.parseInt(id));
			resp.setContentType("text/html;charset=UTF-8");
			PrintWriter out=resp.getWriter();
			out.println("<html>");
				out.println("<head>");
					out.println("<title>"+"修改用户页面"+"</title>");
				out.println("</head>");
				out.println("<body>");
					out.println("<form action='update.do' method='post'>");
						out.println("<table>");
						
							out.println("<tr>");
							out.println("<td><input type='hidden' name='id' value="+account.getId()+"></td>");
							out.println("</tr>");
							
							out.println("<tr>");
								out.println("<td>"+"用户名:"+"</td>");
								out.println("<td>"+"<input type='text' name='name' value='"+account.getName()+"'>"+"</td>");
							out.println("</tr>");
							
							out.println("<tr>");
								out.println("<td>"+"年龄:"+"</td>");
								out.println("<td>"+"<input type='text' name='age' value='"+account.getAge()+"'>"+"</td>");
							out.println("</tr>");
							
							out.println("<tr>");
								out.println("<td>"+"地址:"+"</td>");
								out.println("<td>"+"<input type='text' name='address' value='"+account.getAddress()+"'>"+"</td>");
							out.println("</tr>");
							
							out.println("<tr>");
								out.println("<td colspan='2'>");
								out.println("<input type='submit' value='编辑完成,提交'>&nbsp;&nbsp;");
		    					out.println("<input type='reset' value='重置'>");
		    					out.println("</td>");
		    				out.println("</tr>");
		    				
						out.println("</table>");
					out.println("</form>");
				out.println("</body>");
			out.println("</html>");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	

}
package web;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.AccountDao;
import dao.common.DaoFactory;
import entity.Account;

@WebServlet(urlPatterns="/update.do")
public class AccountUpdateServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = -4718336942993114247L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		String id=req.getParameter("id");
		String name=req.getParameter("name");
		String age=req.getParameter("age");
		String address=req.getParameter("address");
		try {
			AccountDao accountDao=DaoFactory.getInstance("accountDao", AccountDao.class);
			Account account=accountDao.get(Integer.parseInt(id));
			account.setName(name);
			account.setAge(Integer.parseInt(age));
			account.setAddress(address);
			
			accountDao.edit(account);
			resp.sendRedirect("list.do");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

}
package web;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.AccountDao;
import dao.common.DaoFactory;
import entity.Account;

@WebServlet(urlPatterns="/list.do")
public class AccountListServet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 8442871448207725751L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		try{
			
			AccountDao accountDao=DaoFactory.getInstance("accountDao", AccountDao.class);
			List<Account> list=accountDao.findAll();
			
			resp.setContentType("text/html;charset=UTF-8");
			PrintWriter out=resp.getWriter();
			
			out.println("<html>");
				out.println("<head>");
				out.println("</head>");
				out.println("<body>");
					out.println("<h3>"+"用户列表"+"</h3>");
					out.println("<table>");
						out.println("<tr>");
							out.println("<th>"+"ID"+"</th>");
							out.println("<th>"+"用户名"+"</th>");
							out.println("<th>"+"年龄"+"</th>");
							out.println("<th>"+"地址"+"</th>");
							out.println("<th>"+"操作"+"</th>");
						out.println("</tr>");
						for(Account a:list){
							out.println("<tr>");
								out.println("<td>"+a.getId()+"</td>");
								out.println("<td>"+a.getName()+"</td>");
								out.println("<td>"+a.getAge()+"</td>");
								out.println("<td>"+a.getAddress()+"</td>");
								out.println("<th><a href='edit.do?id="+a.getId()+"&name="+a.getName()+"'>修改</a>&nbsp;&nbsp;<a href='delete.do?id="+a.getId()+"'>删除</a></th>");
							out.println("</tr>");
						}
					out.println("</table>");
				out.println("</body>");
			out.println("</html>");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

}
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'register.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    <form align="center" action="add" method="post">
    	<table>
    	
    		<tr>
    			<td>用户名:</td>
    			<td><input type="text" name="name"></td>
    		</tr>
    		
    		<tr>
    			<td>年龄:</td>
    			<td><input type="text" name="age"></td>
    		</tr>
    		
    		<tr>
    			<td>地址:</td>
    			<td><input type="text" name="address"></td>
    		</tr>
    		
    		<tr>
    			<td colspan="2">
    				<input type="submit" value="添加">&nbsp;&nbsp;
    				<input type="reset" value="重置">
    			</td>
    		</tr>
    	</table>
    </form>
  </body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    <a href="add.jsp">添加用户</a><br>
    <a href="list.do">查看用户列表</a>
  </body>
</html>


你可能感兴趣的:(servlet 增删改查)