Servlet优化1 实现将各种请求由一个组件进行统一调度后响应
-
截取各种post/get请求,在一个组件种进行统一调配 -
servlet组件
@WebServlet("/customers.do")
public class CustomersServlet extends ViewBaseServlet {
private ListIpm listIpm = new ListIpm();
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("UTF-8");
String operate = req.getParameter("operate");
if (StringUtil.isEmpty(operate)) {
operate = "index";
}
Method[] methods = this.getClass().getDeclaredMethods();
for (Method m : methods) {
String methodName = m.getName();
if (operate.equals(methodName)) {
try {
m.invoke(this, req, resp);
break;
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
throw new RuntimeException("operate值非法");
}
}
protected void index(HttpServletRequest req, HttpServletResponse resp) {
Connection conn = null;
try {
conn = JdbcUtils.getConnection();
ListIpm ipm = new ListIpm();
HttpSession session = req.getSession();
int pageOn = 1;
String oper = req.getParameter("oper");
String key = null;
if (StringUtil.isNotEmpty(oper) && "search".equals(oper)) {
pageOn = 1;
key = req.getParameter("keyword");
if (StringUtil.isEmpty(key)) {
key = "";
}
session.setAttribute("k",key);
}else {
String pageStr = req.getParameter("page");
if (StringUtil.isNotEmpty(pageStr)) {
pageOn = Integer.parseInt(pageStr);
}
Object keyObj = session.getAttribute("k");
if (keyObj == null) {
key = "";
}else {
key = (String) keyObj;
}
}
session.setAttribute("pageOn",pageOn);
List<Customers> custList = ipm.getList(conn,"%"+ key +"%",pageOn);
session.setAttribute("cl",custList);
long count = 0L;
count = ipm.getCount(conn,"%"+key+"%");
int max = (int) (count+2-1)/2;
session.setAttribute("max",max);
super.processTemplate("index",req,resp);
} catch (Exception e) {
e.printStackTrace();
} finally {
JdbcUtils.closeResource(conn,null);
}
}
private void add(HttpServletRequest req, HttpServletResponse resp) {
Connection conn = null;
try {
String name = req.getParameter("name");
String email = req.getParameter("email");
String birth = req.getParameter("birth");
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date date = format.parse(birth);
conn = JdbcUtils.getConnection();
listIpm.addCustomer(conn,name,email,date);
resp.sendRedirect("customers.do");
} catch (Exception e) {
e.printStackTrace();
} finally {
JdbcUtils.closeResource(conn,null);
}
}
private void delete(HttpServletRequest req, HttpServletResponse resp) {
String idStr = req.getParameter("sid");
if (StringUtil.isNotEmpty(idStr)) {
Connection conn = null;
try {
int id = Integer.parseInt(idStr);
conn = JdbcUtils.getConnection();
listIpm.delById(conn,id);
resp.sendRedirect("customers.do");
} catch (Exception e) {
e.printStackTrace();
} finally {
JdbcUtils.closeResource(conn,null);
}
}
}
private void edit(HttpServletRequest req, HttpServletResponse resp) {
Connection conn = null;
try {
conn = JdbcUtils.getConnection();
String idStr = req.getParameter("cid");
if (StringUtil.isNotEmpty(idStr)) {
int id = Integer.parseInt(idStr);
System.out.println(id);
Customers customer = listIpm.getCustomerById(conn, id);
System.out.println(customer);
req.setAttribute("cu" ,customer);
super.processTemplate("edit",req,resp);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
JdbcUtils.closeResource(conn,null);
}
}
private void update(HttpServletRequest req, HttpServletResponse resp) {
Connection conn = null;
try {
String idStr = req.getParameter("id");
int id = Integer.parseInt(idStr);
String name = req.getParameter("name");
String email = req.getParameter("email");
String birth = req.getParameter("birth");
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date date = format.parse(birth);
conn = JdbcUtils.getConnection();
listIpm.updateById(conn,name,email,date,id);
resp.sendRedirect("customers.do");
} catch (Exception e) {
e.printStackTrace();
} finally {
JdbcUtils.closeResource(conn,null);
}
}
}
-
html文件 index.html <!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<html>
<head>
<meta charset="UTF-8">
<link rel="stylesheet" th:href="@{CSS/index.css}">
<script language="JavaScript" th:src="@{js/index.js}" ></script>
</head>
<body>
<div id="div_contain">
<div id="div_fruit_list">
<p class="center f32">欢迎使用客户后台管理系统</p>
<div style="float: left; margin-left: 20%;width: 60%" >
<form th:action="@{/customers.do}" method="post">
<input type="hidden" name="operate" value="index">
<input type="hidden" name="oper" value="search">
请输入查询关键字<input type="text" name="keyword"/>
<input type="submit" value="查询" />
</form>
</div>
<div style=" width:60% ; margin-left: 20%; float: right">
<a th:href="@{/add.html}" style=" margin-bottom: 10px"> 新添加客户 </a>
</div>
<table id="tbl_fruit">
<tr>
<th>cust_id</th>
<th>cust_name</th>
<th>cust_email</th>
<th>cust_birth</th>
</tr>
<tr th:if="${#lists.isEmpty(session.cl)}">
<td colspan="4">无客户名单!</td>
</tr>
<tr th:unless="${#lists.isEmpty(session.cl)}"
th:each="cust:${session.cl}">
<td>
<a th:text="${cust.id}"
th:href="@{/customers.do(cid=${cust.id},operate='edit')}">1</a>
</td>
<td th:text="${cust.name}">太白金星</td>
<td th:text="${cust.email}"
th:onclick="|delCust(${cust.id})|">tbstart@163.com</td>
<td th:text="${cust.birth}">1000-1-1</td>
</tr>
</table>
<div style="width:60% ; margin-left: 20%">
<input th:type="button" th:value="首页" th:onclick="|pageControl(1)|"
th:disabled="${session.pageOn==1}"/>
<input th:type="button" th:value="上一页"
th:onclick="|pageControl(${session.pageOn-1})|"
th:disabled="${session.pageOn==1}"/>
<input th:type="button" th:value="下一页"
th:onclick="|pageControl(${session.pageOn+1})|"
th:disabled="${session.pageOn==session.max}"/>
<input th:type="button" th:value="尾页"
th:onclick="|pageControl(${session.max})|"
th:disabled="${session.pageOn==session.max}"/>
</div>
</div>
</div>
</body>
</html>
add.html <!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<base href="http://localhost:8080/optimization_1/" >
<link rel="stylesheet" href="CSS/edit.css" >
</head>
<body>
<div id="div_contain">
<div id="div_fruit_list">
<p class="center f32">修改/编辑库存信息</p>
<form action="customers.do" method="post">
<input type="hidden" name="operate" value="add" />
<table id="tbl_fruit">
<tr>
<th>cust_name</th>
<td><input type="text" name="name" /></td>
</tr>
<tr>
<th>cust_email</th>
<td><input type="text" name="email" /></td>
</tr>
<tr>
<th>cust_birth</th>
<td><input type="text" name="birth" /></td>
</tr>
<tr>
<th colspan="2">
<input type="submit" value="添加" />
</th>
</tr>
</table>
</form>
</div>
</div>
</body>
</html>
edit.html <!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<html>
<head>
<meta charset="UTF-8">
<link th:rel="stylesheet" th:href="@{CSS/index.css}">
</head>
<body>
<div id="div_contain">
<div id="div_fruit_list">
<p class="center f32">修改/编辑库存信息</p>
<form th:action="@{/customers.do}" method="post">
<input type="hidden" name="operate" value="update"/>
<table id="tbl_fruit" th:object="${cu}">
<tr>
<th>cust_id</th>
<td><input type="text" name="id" th:value="*{id}"/></td>
</tr>
<tr>
<th>cust_name</th>
<td><input type="text" name="name" th:value="*{name}"/></td>
</tr>
<tr>
<th>cust_email</th>
<td><input type="text" name="email" th:value="*{email}"/></td>
</tr>
<tr>
<th>cust_birth</th>
<td><input type="text" name="birth" th:value="*{birth}"/></td>
</tr>
<tr>
<th colspan="2">
<input th:type="submit" th:value="提交" />
</th>
</tr>
</table>
</form>
</div>
</div>
</body>
</html>
-
js文件 function delCust(s) {
if (confirm('是否确认删除')) {
<!-- 在url上添加operate的值,对应到delete方法 -->
window.location.href = 'customers.do?sid=' + s +'&operate=delete';
}
}
function pageControl(page) {
if (confirm("确认")) {
<!-- 在url上添加operate的值,对应到index方法 -->
window.location.href = 'customers.do?page=' + page +'&operate=index';
}
}
|