ajax技术在ssh中的应用[2]
package com.lxt008.service;
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 javax.servlet.http.HttpSession;
importorg.springframework.web.context.WebApplicationContext;
importorg.springframework.web.context.support.WebApplicationContextUtils;
importcom.lxt008.hibernate.po.PetInfo;
public class DynaUpdatePetProperty extendsHttpServlet {
PetInfo pi;
WebApplicationContext waCtx;
private static final long serialVersionUID =1L;
public DynaUpdatePetProperty() {
super();
}
public void destroy() {
super.destroy(); // Just puts"destroy" string in log
// Put your code here
}
public void doGet(HttpServletRequest request,HttpServletResponse response)
throwsServletException, IOException {
System.out.println("IndoGet()");
//必须获取Web应用上下文,直接new创建piDAO对象是不行的
//pdDAO=new PetInfoDAO();
try
{
waCtx=WebApplicationContextUtils.getWebApplicationContext(getServletContext());
System.out.println(waCtx.toString());
//pdDAO=(IPetInfoDAO)waCtx.getBean("pdDAO");
}
catch(Exception e)
{
e.printStackTrace();
}
request.setCharacterEncoding("UTF-8");
//String petId =request.getParameter("petId");
HttpSessionsession=request.getSession();
StringpetId=(String)session.getAttribute("petId");
String action =request.getParameter("action");
System.out.println("Pet ID:"+petId);
System.out.println("action:"+action);
//目的是增加事务功能
DynaUpdatePetPropertyServicedynaUpdatePetPropertyService=
(DynaUpdatePetPropertyService)waCtx.getBean("dynaUpdatePetPropertyService");
pi=dynaUpdatePetPropertyService.getSinglePet(petId);
System.out.println(pi.toString());
response.setContentType("text/xml; charset=UTF-8");
response.setHeader("Cache-Control","no-cache");
PrintWriter out = response.getWriter();
//产生XML响应
out.println("");
//对不同按钮进行处理
if(action.equals("ws"))
{
//System.out.println("In ws()");
//力量加30
pi.setPetStrength(newInteger(pi.getPetStrength().intValue()+30));
out.println(""+pi.getPetStrength()+"");
pi.setPetCute(newInteger(pi.getPetCute().intValue()));
out.println(""+pi.getPetCute()+"");
pi.setPetLove(newInteger(pi.getPetLove().intValue()));
out.println(""+pi.getPetLove()+"");
dynaUpdatePetPropertyService.save(pi);
}
else
if(action.equals("story"))
{
//System.out.println("In story()");
//力量减5
pi.setPetStrength(newInteger(pi.getPetStrength().intValue()-5));
out.println(""+pi.getPetStrength()+"");
//
pi.setPetCute(newInteger(pi.getPetCute().intValue()+1));
out.println(""+pi.getPetCute()+"");
//
pi.setPetLove(newInteger(pi.getPetLove().intValue()+3));
out.println(""+pi.getPetLove()+"");
dynaUpdatePetPropertyService.save(pi);
}
else
if(action.equals("game"))
{
//System.out.println("In game()");
//力量减5
pi.setPetStrength(newInteger(pi.getPetStrength().intValue()-5));
out.println(""+pi.getPetStrength()+"");
//
pi.setPetCute(newInteger(pi.getPetCute().intValue()+3));
out.println(""+pi.getPetCute()+"");
//
pi.setPetLove(newInteger(pi.getPetLove().intValue()+1));
out.println(""+pi.getPetLove()+"");
dynaUpdatePetPropertyService.save(pi);
}
out.println("");
out.close();
}
public void doPost(HttpServletRequest request,HttpServletResponse response)
throwsServletException, IOException {
doGet(request,response);
}
public void init() throws ServletException{
// Put your code here
}
}
