Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1002515
  • 博文数量: 152
  • 博客积分: 4937
  • 博客等级: 上校
  • 技术积分: 1662
  • 用 户 组: 普通用户
  • 注册时间: 2007-10-05 16:55
文章分类

全部博文(152)

文章存档

2013年(12)

2012年(6)

2011年(58)

2010年(43)

2009年(1)

2008年(15)

2007年(17)

我的朋友

分类: Java

2010-07-06 11:02:05

package com.newspager.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.newspager.dao.AddNumberDAO;
import com.newspager.dao.TempNumberDAO;
import com.newspager.dao.impl.AddNumberDAOImpl;
import com.newspager.dao.impl.TempNumberImpl;
import com.newspager.model.AddNumber;
import com.newspager.model.TempNumber;
public class AddOrDelNumberServlet extends HttpServlet {
    /**
  *
  */
 private static final long serialVersionUID = 1L;
 protected void processRequest (HttpServletRequest request,
            HttpServletResponse response, String method)
            throws ServletException, IOException {
  //System.out.println("servlet222");
                //把响应内容类型设置为 text/xml
                response.setContentType("text/xml");
                response.setHeader("Cache-Control","no-cache"); //HTTP 1.1
                response.setHeader("Pragma","no-cache"); //HTTP 1.0
                response.setDateHeader ("Expires", 0); //prevents caching at the proxy server
                int userId = 0;
                int pagerId = 0;
                int num = 0;
                String pweek;
                //得到用户参数值
                String uid = "";
                String pgid = "";
                String amount = "";
               
                 uid = request.getParameter("userId");
                 pgid = request.getParameter("pagerId");
                 amount = request.getParameter("amount");
                 pweek = request.getParameter("pweek");
               
                if(amount != null && amount.trim() != ""){
                 num = Integer.parseInt(amount);
                }
                if(uid != null && uid.trim() != ""){
                 userId = Integer.parseInt(uid);
                }
                if(pgid != null && pgid.trim() != ""){
                 pagerId = Integer.parseInt(pgid);
                }
               
                TempNumber tempNumber = new TempNumber();
                tempNumber.setAmount(num);
                tempNumber.setUserId(userId);
                tempNumber.setPagerId(pagerId);
               
               
                TempNumberDAO tnDAO = new TempNumberImpl();
                //写回浏览器
                PrintWriter out = response.getWriter();
                /**
                 * 如果接收到的参数num值等于0,就删除该报纸数量记录
                 * 否则,如果存在该报纸数量记录,就更新数量
                 * 否则,就添加一条报纸数量记录
                 *
                 */
                if(num == 0){
                 tnDAO.delete(tempNumber);
                }else if(tnDAO.isExistPager(userId,pagerId)){
                 tnDAO.update(tempNumber);
                }else{
                 tnDAO.add(tempNumber);
                }
                AddNumberDAO andao = new AddNumberDAOImpl();
                AddNumber an = new AddNumber();
                an.setUserId(userId);
                an.setPagerId(pagerId);
                an.setPweek(pweek);
                //修改完成后,判断该报纸是否有加减数功能
                //如果有就刷新页面
                if(andao.isExistAddNumber(an)){
                 out.write("window.location.href=window.location.href;");
                }
               
                out.close();
            }
            protected void doGet (HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
                //Get主函数调用processRequest,完成Get方法的参数接受,返回的过程
                processRequest(request, response, "GET");
            }
            protected void doPost (HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
                //POST主函数调用processRequest,完成POST方法的参数接受,返回的过程
                processRequest(request, response, "POST");
            }
}
阅读(521) | 评论(0) | 转发(0) |
0

上一篇:分页代码PageBean

下一篇:AddOrDelNumberServlet

给主人留下些什么吧!~~