如何在Spring中注入servlet配置组件?

来源:互联网 发布:济南网络广播电视台app 编辑:程序博客网 时间:2024/05/16 15:30

在servlet中可用此取得publicservice业务服务对象:

public class RetrievedataproxyBean extends HttpServlet {   

 public void doGet(HttpServletRequest req, HttpServletResponse res)
   throws ServletException, IOException {

  PrintWriter out = res.getWriter();
  
  WebApplicationContext   ctx=WebApplicationContextUtils.getRequiredWebApplicationContext(this.getServletContext());
  PublicService publicservice=(PublicService)ctx.getBean("publicservice"); 
  String datatype = (String) req.getParameter("dt");  
  // 根据请求的参数,来决定返回数据内容  --需要请求的是抽奖模块
  if (datatype.equalsIgnoreCase("drawmeasures")) {
   String start = (String) req.getParameter("start");
   String limit = (String) req.getParameter("limit");
   try {
    int index = Integer.parseInt(start);
    int pageSize = Integer.parseInt(limit);
    System.out.println("run here");
    //
    List<Drawmeasureindex>  lst=new ArrayList<Drawmeasureindex>();
    lst=publicservice.getAllDrawmeasure();   //完成业务需求
    int j=1;
    String json = "{totalProperty:100,root:[";
    System.out.println(json);
    for(Iterator itr=lst.iterator();itr.hasNext();)
    {
     Drawmeasureindex objtmp=(Drawmeasureindex)itr.next();
     json += "{id:" + j + ",name:'" + objtmp.getDrawmeasurename() + "',descn:'" +objtmp.getDrawmeasurename() + "'},";
    }
    json=json.substring(0, json.length()-1);//去除最后一个分号
    
    json += "]}";
    
    
   
    /*for (int i = index; i < pageSize + index; i++) {
     json += "{id:" + i + ",name:'name" + i + "',descn:'descn" + i + "'}";
     if (i != pageSize + index - 1) {
      json += ",";
     }
    }
    json += "]}";*/    
    System.out.println(json);
    out.write(json);     
   } catch (Exception ex) {
   } } 
 }

}

 

在web.xml中:

 <servlet>
  <servlet-name>retrievedataproxyservlet</servlet-name>
  <servlet-class>com.gisco.adsys.view.bean.RetrievedataproxyBean</servlet-class>
 </servlet>
  <servlet-mapping>
  <servlet-name>retrievedataproxyservlet</servlet-name>
  <url-pattern>/servlet/retrievedata</url-pattern>
 </servlet-mapping>

 

本文转载自:http://www.cnblogs.com/orchidsure/archive/2009/05/18/1459702.html