第一步:在edu.fzu.openfund.domain包下新建类Client。
public class Client {
private String IDCardNO; // 客户身份证号
private String clientName; // 客户姓名
private String clientSex; // 客户性别
private String clientPhone; // 客户联系电话
private String clientAddress; // 客户地址
private String clientEmail; // 客户电邮
private String clientHobby; // 客户爱好
private Date clientCreateDate; // 客户创建时间
public String getIDCardNO()
{
return IDCardNO;
}
public void setIDCardNO(String cardNO)
{
IDCardNO = cardNO;
}
public String getClientName()
{
return clientName;
}
public void setClientName(String clientName)
{
this.clientName = clientName;
}
public String getClientSex()
{
return clientSex;
}
public void setClientSex(String clientSex)
{
this.clientSex = clientSex;
}
public String getClientPhone()
{
return clientPhone;
}
public void setClientPhone(String clientPhone)
{
this.clientPhone = clientPhone;
}
public String getClientAddress()
{
return clientAddress;
}
public void setClientAddress(String clientAddress)
{
this.clientAddress = clientAddress;
}
public String getClientEmail()
{
return clientEmail;
}
public void setClientEmail(String clientEmail)
{
this.clientEmail = clientEmail;
}
public String getClientHobby()
{
return clientHobby;
}
public void setClientHobby(String clientHobby)
{
this.clientHobby = clientHobby;
}
public Date getClientCreateDate()
{
return clientCreateDate;
}
public void setClientCreateDate(Date clientCreateDate)
{
this.clientCreateDate = clientCreateDate;
}
}
第二步:在edu.fzu.openfund.controller包下新建Servlet,类名ClientMgrServlet。点击Next,设置Mapping URL为/client。
第三步:编写doPost()和doGet()方法的代码。
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
int mid = Integer.parseInt(request.getParameter("mid"));
// get inputClient.jsp
if (mid == 0) {
request.getRequestDispatcher("/Client/inputClient.jsp").forward(
request, response);
}
else if (mid == 1) {
Client client = new Client();
client.setIDCardNO(request.getParameter("idcard_no"));
client.setClientName(request.getParameter("client_name"));
if(request.getParameter("client_sex")!=null)
client.setClientSex("client_sex");
if(request.getParameter("client_phone")!=null)
client.setClientPhone("client_phone");
if(request.getParameter("client_address")!=null)
client.setClientAddress("client_address");
if(request.getParameter("client_email")!=null)
client.setClientEmail("client_email");
if(request.getParameter("client_hobby")!=null)
client.setClientHobby("client_hobby");
client.setClientCreateDate(new Date());
request.getRequestDispatcher("/Client/insertClientRes.jsp?rid=0").forward(request, response);
}
}
第四步:修改web.xml文档。
<servlet-mapping>
<servlet-name>ClientMgrServlet</servlet-name>
<url-pattern>/clientMgr</url-pattern>
</servlet-mapping>
第五步:在WebRoot下建立目录Client,在该目录新建inputClient.jsp。
<%@ page language="java" import="java.util.*,edu.fzu.openfund.domain.*"
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>新增客户</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="css/bootstrap.css">
</head>
<body>
<form action="clientMgr?mid=1" method="post"
class="form-horizontal">
<div class="control-group">
<legend>
<span class="controls"> 新增客户 </span>
</legend>
</div>
<div class="control-group">
<label class="control-label" for="idcardno">
客户身份证号:
</label>
<div class="controls">
<input type="text" name="idcard_no" maxlength="18" id="idcardno" />
</div>
</div>
<div class="control-group">
<label class="control-label" for="clientname">
客户姓名:
</label>
<div class="controls">
<input type="text" name="client_name" maxlength="30"
id="clientname" />
</div>
</div>
<div class="control-group">
<label class="control-label" for="clientsex">
客户性别:
</label>
<div class="controls">
<select name="client_sex" id="clientsex" class="input-medium">
<option value="">
==请选择==
</option>
<option value="M">
男
</option>
<option value="F">
女
</option>
</select>
</div>
</div>
<div class="control-group">
<label class="control-label" for="clientphone">
客户手机号码:
</label>
<div class="controls">
<input type="text" name="client_phone" maxlength="20"
id="clientphone" />
</div>
</div>
<div class="control-group">
<label class="control-label" for="clientaddress">
客户住宅地址:
</label>
<div class="controls">
<input type="text" name="client_address" maxlength="20"
id="clientaddress" />
</div>
</div>
<div class="control-group">
<label class="control-label" for="clientemail">
客户电子邮箱:
</label>
<div class="controls">
<input type="text" name="client_email" maxlength="50"
id="clientemail" />
</div>
</div>
<div class="control-group">
<label class="control-label" for="clienthobby">
客户兴趣爱好:
</label>
<div class="controls">
<input type="text" name="client_hobby" maxlength="50"
id="clienthobby" />
</div>
</div>
<div id="submiter" class="control-group">
<div class="controls">
<input type="submit" value=" 添 加 " class="btn btn-primary" />
</div>
</div>
</form>
</body>
</html>
第六步:在
WebRoot
下建立目录
Client
,在该目录新建
inputClientRes.jsp。
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>新增客户</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">
-->
<link rel="stylesheet" type="text/css" href="css/bootstrap.css">
</head>
<body>
<%
request.setCharacterEncoding("UTF-8");
String rid = request.getParameter("rid");
if (rid.equals("0")){
%>
<script>
alert("成功添加!");
window.location.assign("clientMgr?mid=0");
</script>
<%
}
else{
%>
<script>
alert("添加失败!未知原因,请通知管理员");
history.go(-1);
</script>
<%
}
%>
</body>
</html>