servlet的功能不可知这些,servlet的功能主要是完成数据的接收和处理,然后再将数据传回jsp页面,你的这种只是完成了页面的输出。其实你的这种完全可以用jsp实现(毕竟jsp其实就是一个特殊的servlet,只是Tomcat会将你写的jsp文件组装成一个servlet进行执行),好了,我把自己以前写的一个servlet给你,让你看看,servlet的功能。- package com.drp.web.fiscalcalculateperiod;
- import java.io.IOException;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import com.drp.model.FiscalCalculatePeriod;
- import com.drp.model.PageModel;
- import com.drp.web.AbstractServlet;
- @SuppressWarnings("serial")
- public class FiscalCalculatePeriodServlet extends AbstractServlet {
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- doPost(request,response);
- }
- public void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- request.setCharacterEncoding("GB18030");
-
- String command = request.getParameter("command");
- if("getFiscalCalculatePeriodList".equals(command)){
- getFiscalCalculatePeriodList(request,response);
- }else if("gotoAddFiscalCalculatePeriod".equals(command)){
- gotoAddFiscalCalculatePeriod(request,response);
- }else if("addFiscalCalculatePeriod".equals(command)){
- addFiscalCalculatePeriod(request,response);
- }else if("gotoModifyFiscalCalculatePeriod".equals(command)){
- gotoModifyFiscalCalculatePeriod(request,response);
- }else if("modifyFiscalCalculatePeriod".equals(command)){
- modifyFiscalCalculaterPeriod(request,response);
- }
- }
- private void modifyFiscalCalculaterPeriod(HttpServletRequest request,
- HttpServletResponse response) throws ServletException, IOException {
- FiscalCalculatePeriod fiscalCalculatePeriod = new FiscalCalculatePeriod();
- fiscalCalculatePeriod.setId(Integer.parseInt(request.getParameter("id")));
- fiscalCalculatePeriod.setCalculateYear(Integer.parseInt(request.getParameter("fiscalYear")));
- fiscalCalculatePeriod.setCalculateMonth(Integer.parseInt(request.getParameter("fiscalPeriod")));
- try {
- fiscalCalculatePeriod.setBeginDate(new SimpleDateFormat("yyyy-MM-dd").parse(request.getParameter("beginDate")));
- fiscalCalculatePeriod.setEndDate(new SimpleDateFormat("yyyy-MM-dd").parse(request.getParameter("endDate")));
- } catch (ParseException e) {
- e.printStackTrace();
- }
- fiscalCalculatePeriod.setPeriodStatus(request.getParameter("periodSts")== null?"N":"Y");
- FiscalCalculatePeriod fiscalCalculatePeriodResult = fiscalCalculatePeriodManager.modifyFiscalCalculatePeriod(fiscalCalculatePeriod);
- if(fiscalCalculatePeriodResult != null){
- request.setAttribute("fiscalCalculatePeriod", fiscalCalculatePeriodResult);
- request.getRequestDispatcher("../basedata/fiscal_year_period_modify.jsp?modifyFiscalCalculatePeriod=success").forward(request, response);
- }
-
- }
- private void gotoModifyFiscalCalculatePeriod(HttpServletRequest request,
- HttpServletResponse response) throws ServletException, IOException {
- String strId = request.getParameter("id");
- if(strId != null && !strId.equals("")){
- int id = Integer.parseInt(strId);
- FiscalCalculatePeriod fiscalCalculatePeriod = fiscalCalculatePeriodManager.loadFiscalCalculatePeriodById(id);
- if(fiscalCalculatePeriod != null){
- request.setAttribute("fiscalCalculatePeriod", fiscalCalculatePeriod);
- request.getRequestDispatcher("../basedata/fiscal_year_period_modify.jsp").forward(request, response);
- }
- }
- }
- private void getFiscalCalculatePeriodList(HttpServletRequest request,
- HttpServletResponse response) throws ServletException, IOException {
- int pageNo = 1;
- int pageSize = Integer.parseInt(this.getServletContext().getInitParameter("pageSize"));
- String strPageNo = request.getParameter("pageNo");
- if(strPageNo != null && !strPageNo.trim().equals("")){
- pageNo = Integer.parseInt(strPageNo);
- }
- PageModel<FiscalCalculatePeriod> pageModel = fiscalCalculatePeriodManager.getFiscalCalculatePeriodList(pageNo, pageSize);
- request.setAttribute("fiscalCalculatePeriodListPageModel", pageModel);
- request.getRequestDispatcher("../basedata/fiscal_year_period_maint.jsp").forward(request, response);
- }
- private void addFiscalCalculatePeriod(HttpServletRequest request,
- HttpServletResponse response) throws ServletException, IOException {
- FiscalCalculatePeriod fiscalCalculatePeriod = new FiscalCalculatePeriod();
- fiscalCalculatePeriod.setCalculateYear(Integer.parseInt(request.getParameter("fiscalYear")));
- fiscalCalculatePeriod.setCalculateMonth(Integer.parseInt(request.getParameter("fiscalPeriod")));
- try {
- fiscalCalculatePeriod.setBeginDate(new SimpleDateFormat("yyyy-MM-dd").parse(request.getParameter("beginDate")));
- fiscalCalculatePeriod.setEndDate(new SimpleDateFormat("yyyy-MM-dd").parse(request.getParameter("endDate")));
- } catch (ParseException e) {
- e.printStackTrace();
- }
- fiscalCalculatePeriod.setPeriodStatus(request.getParameter("periodSts")== null?"N":"Y");
- FiscalCalculatePeriod fiscalCalculatePeriodResult = fiscalCalculatePeriodManager.addFiscalCalculatePeriod(fiscalCalculatePeriod);
- if(fiscalCalculatePeriodResult != null){
- request.setAttribute("fiscalCalculatePeriod", fiscalCalculatePeriodResult);
- request.getRequestDispatcher("../basedata/fiscal_year_period_add.jsp?addfiscalCalculatePeriod=success").forward(request, response);
- }
- }
- private void gotoAddFiscalCalculatePeriod(HttpServletRequest request,
- HttpServletResponse response) throws ServletException, IOException {
- request.getRequestDispatcher("../basedata/fiscal_year_period_add.jsp").forward(request, response);
- }
- }
复制代码 |