package com.neu.entity;
import java.util.Date;
public class Profile {
private Integer id;
private String name;
private Date birthday;
private String gender;
private String career;
private String address;
private String mobile;
public Profile() {
super();
}
public Profile(Integer id, String name, Date birthday, String gender, String career, String address, String mobile) {
super();
this.id = id;
this.name = name;
this.birthday = birthday;
this.gender = gender;
this.career = career;
this.address = address;
this.mobile = mobile;
}
@Override
public String toString() {
return "Profile [id=" + id + ", name=" + name + ", birthday=" + birthday + ", gender=" + gender + ", career="
+ career + ", address=" + address + ", mobile=" + mobile + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((address == null) ? 0 : address.hashCode());
result = prime * result + ((birthday == null) ? 0 : birthday.hashCode());
result = prime * result + ((career == null) ? 0 : career.hashCode());
result = prime * result + ((gender == null) ? 0 : gender.hashCode());
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result + ((mobile == null) ? 0 : mobile.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Profile other = (Profile) obj;
if (address == null) {
if (other.address != null)
return false;
} else if (!address.equals(other.address))
return false;
if (birthday == null) {
if (other.birthday != null)
return false;
} else if (!birthday.equals(other.birthday))
return false;
if (career == null) {
if (other.career != null)
return false;
} else if (!career.equals(other.career))
return false;
if (gender == null) {
if (other.gender != null)
return false;
} else if (!gender.equals(other.gender))
return false;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
if (mobile == null) {
if (other.mobile != null)
return false;
} else if (!mobile.equals(other.mobile))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getCareer() {
return career;
}
public void setCareer(String career) {
this.career = career;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
}
package com.neu.dao;
import java.util.List;
import com.neu.entity.Profile;
public interface ProfileDao {
public List<Profile> getAll() throws Exception;
public Profile get(int id) throws Exception;
public void update(Profile profile) throws Exception;
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="">
<table border="1">
<tr>
<td>编号</td>
<td name="id" readonly="readyonly">${profile.id}</td>
</tr>
<tr>
<td>姓名</td>
<td name="name" >${profile.name}</td>
</tr>
<tr>
<td>生日</td>
<td name="birthday">${profile.birthday}</td>
</tr>
<tr>
<td>性别</td>
<td name="gender">${profile.gender}</td>
</tr>
<tr>
<td>职业</td>
<td name="career">${profile.career}</td>
</tr>
<tr>
<td>住所</td>
<td name="address">${profile.address}</td>
</tr>
<tr>
<td>电话</td>
<td name="mobile">${profile.mobile}</td>
</tr>
<tr>
<td>
<a href="${pageContext.request.contextPath }/ListServlet">返回</a>
</td>
</tr>
</table>
</form>
</body>
</html>
package com.neu.service;
import java.util.List;
import com.neu.entity.Profile;
public interface ProfileService {
public List<Profile> getAll() throws Exception;
public Profile get(int id) throws Exception;
public void update(Profile profile) throws Exception;
}
package com.neu.service;
import java.util.List;
import com.neu.dao.ProfileDao;
import com.neu.dao.ProfileDaoImpl;
import com.neu.entity.Profile;
public class ProfileServiceImpl implements ProfileService {
ProfileDao profileDao=new ProfileDaoImpl();
@Override
public List<Profile> getAll() throws Exception {
List<Profile> list = profileDao.getAll();
return list;
}
@Override
public Profile get(int id) throws Exception {
Profile profile = profileDao.get(id);
return profile;
}
@Override
public void update(Profile profile) throws Exception {
profileDao.update(profile);
}
}
package com.neu.servlet;
import java.io.IOException;
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 com.neu.entity.Profile;
import com.neu.service.ProfileService;
import com.neu.service.ProfileServiceImpl;
@WebServlet("/ListServlet")
public class ListServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ProfileService profileService=new ProfileServiceImpl();
try {
List<Profile> profilelist = profileService.getAll();
request.setAttribute("profilelist", profilelist);
request.getRequestDispatcher("/WEB-INF/jsp/profile/list.jsp").forward(request, response);
} catch (Exception e) {
e.printStackTrace();
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<table border="1" width="800">
<tr bgcolor="yellow">
<td>编号</td>
<td>姓名</td>
<td>生日</td>
<td>性别</td>
<td>职业</td>
<td>住所</td>
<td>电话</td>
<td></td>
</tr>
<c:forEach items="${ profilelist }" var="profile">
<tr>
<td>${ profile.id}</td>
<td>${ profile.name}</td>
<td> <fmt:formatDate value="${profile.birthday}" pattern="yyyy-MM-dd"/></td>
<td>${ profile.gender}</td>
<td>${ profile.career}</td>
<td>${ profile.address}</td>
<td>${ profile.mobile}</td>
<td>
<a href="${pageContext.request.contextPath}/UpdateServlet?id=${profile.id}">修改</a>
<a href="${pageContext.request.contextPath }/DetailServlet?id=${profile.id}">明细</a>
</td>
</tr>
</c:forEach>
</table>
</body>
</html>
package com.neu.servlet;
import java.io.IOException;
import java.util.Date;
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 com.neu.entity.Profile;
import com.neu.service.ProfileService;
import com.neu.service.ProfileServiceImpl;
@WebServlet("/UpdateServlet")
public class UpdateServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
Integer id=Integer.parseInt(request.getParameter("id"));
ProfileService profileService=new ProfileServiceImpl();
try {
Profile profile = profileService.get(id);
request.setAttribute("profile", profile);
request.getRequestDispatcher("/WEB-INF/jsp/profile/update.jsp").forward(request, response);
} catch (Exception e) {
e.printStackTrace();
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/UpdateServlet2">
<table border="1">
<tr>
<td>编号</td>
<td readonly="readyonly"><input type="text" name="id" value="${profile.id}"></td>
</tr>
<tr>
<td>姓名</td>
<td ><input type="text" name="name" value="${profile.name}"></td>
</tr>
<tr>
<td>生日</td>
<td ><input type="text" name="birthday" value="${profile.birthday}"></td>
</tr>
<tr>
<td>性别</td>
<td ><input type="text" name="gender" value="${profile.gender}"></td>
</tr>
<tr>
<td>职业</td>
<td ><input type="text" name="career" value="${profile.career}"></td>
</tr>
<tr>
<td>住所</td>
<td ><input type="text" name="address" value="${profile.address}"></td>
</tr>
<tr>
<td>电话</td>
<td ><input type="text" name="mobile" value="${profile.mobile}"></td>
</tr>
<tr>
<td>
<input type="submit" value="修改">
<a href="${pageContext.request.contextPath }/ListServlet">返回</a>
</td>
</tr>
</table>
</form>
</body>
</html>
package com.neu.servlet;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
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 com.neu.entity.Profile;
import com.neu.service.ProfileService;
import com.neu.service.ProfileServiceImpl;
@WebServlet("/UpdateServlet2")
public class UpdateServlet2 extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
int id =Integer.parseInt(request.getParameter("id")) ;
String name =request.getParameter("name");
SimpleDateFormat f=new SimpleDateFormat("yyy-MM-dd");
Date birthday=new Date();
try {
birthday = f.parse(request.getParameter("birthday"));
} catch (ParseException e1) {
System.out.println("生日错");
e1.printStackTrace();
}
String gender = request.getParameter("gender");
String career=request.getParameter("career");
String address=request.getParameter("address");
String mobile=request.getParameter("mobile");
Profile profile=new Profile(id,name,birthday,gender,career,address,mobile);
ProfileService profileService=new ProfileServiceImpl();
try {
profileService.update(profile);
request.getRequestDispatcher("/ListServlet").forward(request, response);
} catch (Exception e) {
e.printStackTrace();
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="">
<table border="1">
<tr>
<td>编号</td>
<td name="id" readonly="readyonly">${profile.id}</td>
</tr>
<tr>
<td>姓名</td>
<td name="name" >${profile.name}</td>
</tr>
<tr>
<td>生日</td>
<td name="birthday">${profile.birthday}</td>
</tr>
<tr>
<td>性别</td>
<td name="gender">${profile.gender}</td>
</tr>
<tr>
<td>职业</td>
<td name="career">${profile.career}</td>
</tr>
<tr>
<td>住所</td>
<td name="address">${profile.address}</td>
</tr>
<tr>
<td>电话</td>
<td name="mobile">${profile.mobile}</td>
</tr>
<tr>
<td>
<a href="${pageContext.request.contextPath }/ListServlet">返回</a>
</td>
</tr>
</table>
</form>
</body>
</html>