码迷,mamicode.com
首页 > 其他好文 > 详细

servlet应用具体实例

时间:2015-06-08 13:05:33      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:

web,xml应用文件

1.<filter>参数

    <filter>
        <filter-name>encodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <!--多数据源支持监听  -->

java服务端:  每一个file对应一个具体的类,他会在web应用的servlet应用之响应

 1 /*** Eclipse Class Decompiler plugin, copyright (c) 2012 Chao Chen (cnfree2000@hotmail.com) ***/
 2 package org.springframework.web.filter;
 3 
 4 import java.io.IOException;
 5 import javax.servlet.FilterChain;
 6 import javax.servlet.ServletException;
 7 import javax.servlet.http.HttpServletRequest;
 8 import javax.servlet.http.HttpServletResponse;
 9 
10 public class CharacterEncodingFilter extends OncePerRequestFilter {
11     private String encoding;
12     private boolean forceEncoding = false;
13 
14     public void setEncoding(String encoding) {
15         this.encoding = encoding;
16     }
17 
18     public void setForceEncoding(boolean forceEncoding) {
19         this.forceEncoding = forceEncoding;
20     }
21 
22     protected void doFilterInternal(HttpServletRequest request,
23             HttpServletResponse response, FilterChain filterChain)
24             throws ServletException, IOException {
25         if ((this.encoding != null)
26                 && (((this.forceEncoding) || (request.getCharacterEncoding() == null)))) {
27             request.setCharacterEncoding(this.encoding);
28             if (this.forceEncoding) {
29                 response.setCharacterEncoding(this.encoding);
30             }
31         }
32         filterChain.doFilter(request, response);
33     }
34 }

案例二xml

<!--多数据源支持监听  -->
    <filter>
        <filter-name>dataSourceFilter</filter-name>
        <filter-class>cn.jasgroup.jasframework.dataaccess.filter.DataSourceFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>dataSourceFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

java服务器端

/*** Eclipse Class Decompiler plugin, copyright (c) 2012 Chao Chen (cnfree2000@hotmail.com) ***/
package cn.jasgroup.jasframework.dataaccess.filter;

import cn.jasgroup.jasframework.dataaccess.observer.DataSourceObserver;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

public class DataSourceFilter implements Filter {
    public void destroy() {
    }

    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) request;
        String datasource = req.getParameter("dataSourceName");
        if ((datasource != null) && (!("".equals(datasource)))) {
            DataSourceObserver.setDataSourceName(datasource);
        }
        filterChain.doFilter(request, response);
    }

    public void init(FilterConfig arg0) throws ServletException {
    }
}

 

respoonser实例

/**
     * 查询任意圈定区域所在工区信息
     * 
     * @param request
     * @param response
     * @return
     * @throws IOException
     */
    @RequestMapping("/queryWorkareaInfo")
    @ResponseBody
    public void queryWorkareaInfo(HttpServletRequest request, HttpServletResponse response) throws IOException {
        WorkareaInfoBo winfoBo = new WorkareaInfoBo();
        String workareaName = request.getParameter("workareaName");

        String wellIds = request.getParameter("wellIds");
        String wellNames = request.getParameter("wellNames");
        try {
            wellNames = URLDecoder.decode(wellNames, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        // 生产层位
        String cengwei = request.getParameter("cengwei");

        String date_start = request.getParameter("date_start");
        String date_end = request.getParameter("date_end");
        if (StringUtils.isNotBlank(cengwei)) {
            try {
                cengwei = URLDecoder.decode(cengwei, "UTF-8");
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            if (!cengwei.equals("所有层位")) {
                String cengwei_wellname = "";
                String[] wellarr = wellNames.split(";");
                wellNames="";
                for (String val : wellarr) {
                    cengwei_wellname += "‘" + val + "‘,";
                }
                cengwei_wellname += "@";
                cengwei_wellname = cengwei_wellname.replace(",@", "");

//                cengwei = getChildren(cengwei);

//                wellNames = "";
//                String hql_per = "from Perforationdata where perforationposition in(‘" + cengwei + "‘) and wellnum in("
//                        + cengwei_wellname + ")";
                String hql_per = "select distinct(jh) from YCSY_NEWHORIZON where cw in(‘" + cengwei + "‘) and jh in("
                        + cengwei_wellname + ")";

                List<NewHorizon> list = workareaInfoService.queryPerforation(hql_per);
                for (Iterator iterator = list.iterator(); iterator.hasNext();) {
                    NewHorizon perforationdata = (NewHorizon) iterator.next();
                    wellNames += perforationdata.getJh() + ";";
                }
            }
        }
        // 生产层位end
        /*
         * try { wellNames = new
         * String(wellNames.getBytes("ISO-8859-1"),"UTF-8"); } catch
         * (UnsupportedEncodingException e1) { // TODO Auto-generated catch
         * block e1.printStackTrace(); }
         */

        Map<String, Object> map = new HashMap<String, Object>();
        PageRequest pageRequest = this.getPage(request);
        System.out.println(pageRequest + "=pageRequest");
        // 根据工区名称查询所有井名称
        if (!"".equals(workareaName) && null != workareaName) {
            String str = "from Wellinfo where workArea = ‘" + workareaName + "‘";
            List<Wellinfo> wellInfoList = workareaInfoService.queryWellinfoList(pageRequest, str);
            if (wellInfoList.size() > 0) {
                String wellNum = "";
                for (int i = 0; i < wellInfoList.size(); i++) {
                    wellNum = wellInfoList.get(i).getWellName();
                    wellIds += wellNum + ";";
                }
            }
        }
        String hql = joinHql(wellNames, workareaName);
        String cyhql = "";
        String zshql = "";
        if (StringUtils.isNotBlank(workareaName)) {
            cyhql = "from Wellinfo where wellType <> ‘注水井‘ and workArea = ‘" + workareaName + "‘";
            zshql = "from Wellinfo where wellType like ‘%注水%‘ and workArea = ‘" + workareaName + "‘";
        } else if (StringUtils.isNotBlank(wellIds)) {
            cyhql = joinCyWellHql(wellIds);
            zshql = joinZsWellHql(wellIds);
        } else if (StringUtils.isNotBlank(wellNames)) {
            cyhql = joinCyWellHqlName(wellNames);
            zshql = joinZsWellHqlName(wellNames);
        }
        WorkareaInfoBo wibo = workareaInfoService.queryWorkareaInfo(pageRequest, hql + "@" + cyhql + "@" + zshql,
                date_start, date_end, wellNames,cengwei);
        String sql = "select tmcl cl ,qkzymj mj from ycab03 where qkmc  = ‘东仁沟区‘and gxsj=(select max(gxsj) gxsj from  ycab03 where qkmc  = ‘东仁沟区‘)";
        List<AreaSeartH> ah = dataReportService.queryAreaAndChuliangServer(sql,AreaSeartH.class);
        JSONObject obj = new JSONObject();
        DecimalFormat df=new DecimalFormat("0.00");
        if(ah.size()>0){
            double yujichuliang;
            double suoxuanmianji;
                obj.put("dzreserves", ah.get(0).getCl());
                obj.put("workarea", ah.get(0).getMj());
            if(StringUtils.isNotEmpty(ah.get(0).getCl())&&StringUtils.isNotEmpty(wibo.getOilWellNum())){
                yujichuliang = Double.parseDouble(ah.get(0).getCl())/1137*Double.parseDouble(wibo.getOilWellNum());
                    obj.put("guessGeologicla",df.format(yujichuliang));                
            }else{
                obj.put("guessGeologicla", "0.00");
            }
            if(StringUtils.isNotEmpty(ah.get(0).getMj())&&StringUtils.isNotEmpty(wibo.getOilWellNum())){
                suoxuanmianji =  Double.parseDouble(ah.get(0).getMj())/1137*Double.parseDouble(wibo.getOilWellNum());
                    obj.put("calculatearea",df.format(suoxuanmianji));
            }else{
                obj.put("calculatearea",  "0.00");
            }
        }else{
            obj.put("dzreserves", "0.00");
            obj.put("workarea", "0.00");
        }
        obj.put("endDate", wibo.getEndDate());
            obj.put("injectWaterNum",wibo.getInjectWaterNum());
            obj.put("productOilNum",wibo.getProductOilNum());
        obj.put("oilWellNum",wibo.getOilWellNum());
        obj.put("startDate", wibo.getStartDate());
        obj.put("waterWellNum", wibo.getWaterWellNum());
        obj.put("wellNames", wibo.getWellNames());
                obj.put("yeliang",wibo.getYeliang());
        
                obj.put("currcyou", wibo.getCurrcyou());
                obj.put("currzshui",wibo.getCurrzshui());
                obj.put("curryeliang", wibo.getCurryeliang());
        response.getWriter().write(obj.toString());
        response.getWriter().flush();
        response.getWriter().close();
    }

 

servlet应用具体实例

标签:

原文地址:http://www.cnblogs.com/tian830937/p/4560581.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!