//index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!-- 转发 -->
<jsp:forward page="/emp.do?p=getAllEmps"></jsp:forward>
//这是regist.jsp
<script type="text/javascript" src="js/My97DatePicker/WdatePicker.js"></script>
<script type="text/javascript" src="js/My97DatePicker/WdatePicker.js"></script>
<!-- 这是时刻表的控件的增加 -->
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@page import="com.dao.EmpDAO"%>
<%@page import="com.entity.Emp"%>
<%@page import="com.entity.Dep"%>
<form action="emp.do?p=regist" method="post">
姓名:<input type=text name="empname"> <br>
性别:<input type=radio name="empsex" value=1>男
<input type=radio name="empsex" value=0>女 <br>
爱好:<input type=checkbox value="吃" name="emploves">吃
<input type=checkbox value="喝" name="emploves">喝
<input type=checkbox value="玩" name="emploves">玩
<input type=checkbox value="乐" name="emploves">乐<br>
生日:<input type=text name="empage" class="Wdate" οnfοcus="new WdatePicker(this)" > <br>
部门:<select name="depid">
</select>
</form>
//这是show.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@page import="com.entity.Emp"%>
<a href="emp.do?p=getAllDep2RegistView">注册员工</a> <br>
<table border="1" align="center" width="80%">
<tr>
<th>编号</th>
<th>姓名</th>
<th>性别</th>
<th>部门</th>
<th>操作</th>
</tr>
<%
List<Emp> emps = (List)request.getAttribute("emps");
for (Emp e:emps) {
%>
<tr align="center">
<td><%= e.getEmpid() %></td>
<td><%= e.getEmpname() %></td>
<td><%= e.getEmpsex()==1 ?"男":"女" %></td>
<td><%= e.getDepname() %></td>
<td>
<a οnclick="return confirm('真的要删除吗?')" href="emp.do?p=del&id=<%=e.getEmpid() %>">删除</a> |
<a href="emp.do?p=updateView&id=<%=e.getEmpid() %>">修改</a>
</td>
</tr>
<% } %>
</table>
//这是update.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@page import="com.entity.Dep"%>
<%@page import="com.entity.Emp"%>
<%
//取出在servlet保存的数据
List<Dep> deps = (List)request.getAttribute("deps");
Emp emp = (Emp) request.getAttribute("emp");
%>
<script>
onload = function()
{
var sex = <%=emp.getEmpsex()%>;
var depid = <%=emp.getDepid() %>;
/*
if (sex==1)
document.getElementsByName("empsex")[0].checked=true;
else
document.getElementsByName("empsex")[1].checked=true;
*/
document.getElementsByName("empsex")[1-sex].checked=true; //显示性别
document.getElementById("depid").value = depid;
}
</script>
<form action="emp.do?p=update" method="post">
编号:<input type=text name=empid value=<%=emp.getEmpid() %> readonly="readonly"> <br>
姓名:<input type=text name=empname value="<%=emp.getEmpname() %>"> <br>
性别:<input type=radio name="empsex" value=1>男
<input type=radio name="empsex" value=0>女 <br>
部门:<select name="depid" id="depid">
<% for (Dep d:deps) { %>
<option value="<%=d.getDepid() %>"><%=d.getDepname() %></option>
<% } %>
</select> <br>
<input type=submit value="修改">
</form>
//这是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">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>servletLife</servlet-name>
<servlet-class>com.servlets.ServletLife</servlet-class>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>servletLife</servlet-name>
<url-pattern>/test.do</url-pattern>
</servlet-mapping>
<!-- 员工servlet -->
<servlet>
<servlet-name>EmpServlet</servlet-name>
<servlet-class>com.servlets.EmpServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>EmpServlet</servlet-name>
<url-pattern>/emp.do</url-pattern>
</servlet-mapping>
</web-app>
//这是servlet的代码
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import com.dao.EmpDAO;
import com.entity.Emp;
public class EmpServlet implements Servlet {
EmpDAO empDAO = new EmpDAO();
public void destroy() {
// TODO Auto-generated method stub
}
public ServletConfig getServletConfig() {
// TODO Auto-generated method stub
return null;
}
public String getServletInfo() {
// TODO Auto-generated method stub
return null;
}
public void init(ServletConfig config) throws ServletException {
// TODO Auto-generated method stub
}
public void service(ServletRequest req, ServletResponse res)
throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
String p = req.getParameter("p");
if (p.equals("getAllDep2RegistView")) {
getAllDep2RegistView(req, res);
} else if (p.equals("regist")) {
regist(req, res);
} else if (p.equals("getAllEmps")) {
getAllEmps(req, res);
} else if (p.equals("del")) {
delEmp(req, res);
} else if (p.equals("updateView")) {
updateView(req, res);
}
else if (p.equals("update")){
update(req, res);
}
}
//保存修改
public void update(ServletRequest request, ServletResponse res)
throws ServletException, IOException {
//编号
int empid = Integer.parseInt(request.getParameter("empid"));
//姓名
String empname = request.getParameter("empname");
//性别
int empsex= Integer.parseInt(request.getParameter("empsex"));
//部门
int depid =Integer.parseInt(request.getParameter("depid"));
//封装成Emp
Emp emp = new Emp();
emp.setEmpid(empid);
emp.setEmpname(empname);
emp.setDepid(depid);
emp.setEmpsex(empsex);
//修改
int n = empDAO.update(emp);
if (n>0)
getAllEmps(request, res);
else{
// 客户显示中文
res.setContentType("text/html;charset=utf-8");
// 得到输出流
PrintWriter out = res.getWriter();
// 输出
out.write("<script>alert('修改失败');</script>");
}
}
//根据员工id查找员工,并查找所有部门,保存起来,转到修改界面
public void updateView(ServletRequest request, ServletResponse res)
throws ServletException, IOException {
int id = Integer.parseInt(request.getParameter("id"));
//根据id查找对象
Emp emp = empDAO.findById(id);
//查找所有部门
List deps = empDAO.getAllDep();
//保存
request.setAttribute("emp", emp);
request.setAttribute("deps", deps);
//转到修改界面
request.getRequestDispatcher("/update.jsp").forward(request, res);
}
// 删除方法
public void delEmp(ServletRequest request, ServletResponse res)
throws ServletException, IOException {
int id = Integer.parseInt(request.getParameter("id"));
int n = empDAO.delEmp(id);
if (n > 0)
getAllEmps(request, res);
else {
// 客户显示中文
res.setContentType("text/html;charset=utf-8");
// 得到输出流
PrintWriter out = res.getWriter();
// 输出
out.write("<script>alert('删除失败');</script>");
}
}
// 取所有员工
public void getAllEmps(ServletRequest request, ServletResponse res)
throws ServletException, IOException {
// 取所有员工
List emps = empDAO.getAllEmps();
request.setAttribute("emps", emps);
request.getRequestDispatcher("/show.jsp").forward(request, res);
}
// 查部门转转到注册页面
public void getAllDep2RegistView(ServletRequest request, ServletResponse res)
throws ServletException, IOException {
// 调用业务
List deps = empDAO.getAllDep();
// 保存部门
request.setAttribute("deps", deps);
// 转到注册页
request.getRequestDispatcher("/regist.jsp").forward(request, res);
}
// 注册
public void regist(ServletRequest req, ServletResponse res)
throws ServletException, IOException {
// 姓名
String empname = req.getParameter("empname");
// 性别
String empsex = req.getParameter("empsex");
// 爱好
String[] emploves = req.getParameterValues("emploves");
// 生日
String empage = req.getParameter("empage");
// 部门
String depid = req.getParameter("depid");
// 封装到Emp
Emp emp = new Emp();
emp.setDepid(Integer.parseInt(depid));
emp.setEmpage(java.sql.Date.valueOf(empage));
// 爱好
StringBuffer sb = new StringBuffer();
if (emploves != null) {
for (String str : emploves) {
sb.append(str).append(";");
}
emp.setEmploves(sb.toString());
}
emp.setEmpname(empname);
emp.setEmpsex(Integer.parseInt(empsex));
int n = empDAO.regist(emp);
// 客户显示中文
res.setContentType("text/html;charset=utf-8");
// 得到输出对象,准备客户端写内容
PrintWriter out = res.getWriter();
if (n > 0) {
// 输出
out
.write("<script>alert('注册成功');location='emp.do?p=getAllEmps'</script>");
} else {
out.write("<script>alert('注册失败');</script>");
}
}
}
//这是实体
public class Dep {
private int depid;
private String depname;
public int getDepid() {
return depid;
}
public void setDepid(int depid) {
this.depid = depid;
}
public String getDepname() {
return depname;
}
public void setDepname(String depname) {
this.depname = depname;
}
}
import java.sql.Date;
public class Emp extends Dep {
private int empid;
private String empname;
private int empsex;
private String emploves;
private Date empage;
public int getEmpid() {
return empid;
}
public void setEmpid(int empid) {
this.empid = empid;
}
public String getEmpname() {
return empname;
}
public void setEmpname(String empname) {
this.empname = empname;
}
public int getEmpsex() {
return empsex;
}
public void setEmpsex(int empsex) {
this.empsex = empsex;
}
public String getEmploves() {
return emploves;
}
public void setEmploves(String emploves) {
this.emploves = emploves;
}
public Date getEmpage() {
return empage;
}
public void setEmpage(Date empage) {
this.empage = empage;
}
}
//这是dao
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.entity.Dep;
import com.entity.Emp;
public class EmpDAO {
// 封装连接数据库
public Connection getCon() {
Connection con = null;
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con = DriverManager.getConnection(
"jdbc:sqlserver://127.0.0.1:1433;DatabaseName=test", "sa",
"sasa");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return con;
}
// 修改员工
public int update(Emp emp) {
Connection con = getCon();
String sql = "update emp set empname=?,empsex=?,depid=? where empid=?";
int n = 0;
try {
PreparedStatement ps = con.prepareStatement(sql);
ps.setObject(1, emp.getEmpname());
ps.setObject(2, emp.getEmpsex());
ps.setObject(3, emp.getDepid());
ps.setObject(4, emp.getEmpid());
n = ps.executeUpdate();
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return n;
}
// 取所有部门
public List getAllDep() {
Connection con = getCon();
List list = new ArrayList();
String sql = "select * from dep";
try {
PreparedStatement ps = con.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
int depid = rs.getInt("depid");
String depname = rs.getString("depname");
Dep dep = new Dep();
dep.setDepid(depid);
dep.setDepname(depname);
list.add(dep);
}
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
// 保存注册
public int regist(Emp emp) {
Connection con = getCon();
String sql = "insert into emp(empname,empsex,emploves,empage,depid) values(?,?,?,?,?)";
int n = 0;
try {
PreparedStatement ps = con.prepareStatement(sql);
ps.setObject(1, emp.getEmpname());
ps.setObject(2, emp.getEmpsex());
ps.setObject(3, emp.getEmploves());
ps.setObject(4, emp.getEmpage());
ps.setObject(5, emp.getDepid());
n = ps.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return n;
}
// 删除员工
public int delEmp(int id) {
Connection con = getCon();
String sql = "delete from emp where empid=?";
int n = 0;
try {
PreparedStatement ps = con.prepareStatement(sql);
ps.setObject(1, id);
n = ps.executeUpdate();
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return n;
}
// 取所有员工
public List getAllEmps() {
List list = new ArrayList();
Connection con = getCon();
String sql = "select * from emp join dep on emp.depid=dep.depid";
try {
PreparedStatement ps = con.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
Emp emp = new Emp();
int empid = rs.getInt("empid");
String empname = rs.getString("empname");
int empsex = rs.getInt("empsex");
String depname = rs.getString("depname");
emp.setDepname(depname);
emp.setEmpname(empname);
emp.setEmpid(empid);
emp.setEmpsex(empsex);
list.add(emp);
}
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
// 根据id查找员工
public Emp findById(int id) {
Connection con = getCon();
String sql = "select * from emp where empid=?";
try {
PreparedStatement ps = con.prepareStatement(sql);
ps.setObject(1, id);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
Emp emp = new Emp();
int empid = rs.getInt("empid");
String empname = rs.getString("empname");
int empsex = rs.getInt("empsex");
int depid = rs.getInt("depid");
emp.setDepid(depid);
emp.setEmpname(empname);
emp.setEmpid(empid);
emp.setEmpsex(empsex);
return emp;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
}
//这是servlet的一个生命周期
import java.io.IOException;
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class ServletLife implements Servlet {
public ServletLife(){
System.out.println("实例化");
}
public void destroy() {
// TODO Auto-generated method stub
System.out.println("死亡");
}
public ServletConfig getServletConfig() {
// TODO Auto-generated method stub
return null;
}
public String getServletInfo() {
// TODO Auto-generated method stub
return null;
}
public void init(ServletConfig arg0) throws ServletException {
// TODO Auto-generated method stub
System.out.println("初始化");
}
public void service(ServletRequest arg0, ServletResponse arg1)
throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println("服务");
}
}