📄 incomeaddservlet.java
字号:
package com.yiboit.cfss.income;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.yiboit.cfss.db.CommonDAO;
import com.yiboit.cfss.dept.DeptDAO;
import com.yiboit.cfss.util.StringUtil;
public class IncomeAddServlet extends HttpServlet {
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String cmd = request.getParameter("cmd");
try {
if (cmd == null) {
DeptDAO deptDAO = new DeptDAO();
List deptList = deptDAO.getDeptList();
request.setAttribute("deptList", deptList);
request.getRequestDispatcher("incomeAdd.jsp").forward(request,
response);
} else {
String dept = request.getParameter("dept");
String income = request.getParameter("income");
String errmsg = checkInput(dept, income);
if (errmsg != null) {
request.setAttribute("errmsg", errmsg);
DeptDAO deptDAO = new DeptDAO();
List deptList = deptDAO.getDeptList();
request.setAttribute("deptList", deptList);
request.setAttribute("dept", dept);
request.setAttribute("income", income);
request.getRequestDispatcher("incomeAdd.jsp").forward(request, response);
return;
}
IncomeDAO dao = new IncomeDAO();
boolean result = dao.addIncome(Integer.parseInt(dept), Double
.parseDouble(income));
if (result) {
response.sendRedirect("IncomeListServlet");
} else {
response.sendRedirect(request.getContextPath()
+ "/error.jsp");
}
}
} catch (SQLException e) {
e.printStackTrace();
response.sendRedirect(request.getContextPath() + "/error.jsp");
}
}
private String checkInput(String dept, String income) throws SQLException {
String result = null;
String sql = "select dept_id from shop_dept where dept_id = " + dept;
if (dept == null || dept.length() == 0) {
result = "Please select Department.";
} else if (dept.length() != 3) {
result = "Department ID is invalid.";
} else if (!CommonDAO.isExist(sql)) {
result = "Department ID is not exist.";
} else if (income == null || income.length() == 0) {
result = "Income is required.";
} else if (income.length() > 10) {
result = "Income is too large.";
} else if (!StringUtil.isDouble(income)) {
result = "Income must be a number.";
} else {
sql = "select income_id from shop_income where dept_id = " + dept
+ " and to_char(business_date, 'yyyyMMdd') = to_char(sysdate, 'yyyyMMdd')";
if (CommonDAO.isExist(sql)) {
result = "Today\\'s record is exist. Cann\\'t add it.";
}
}
return result;
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to
* post.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -