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 AccountServlet 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工厂
		
			AccountDao accountDao;
			try {
				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 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("</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("</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 '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>
<%@ 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>


你可能感兴趣的:(servlet 连接数据库的 增加用户,还有用户列表)