servlet应用具体实例

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 ([email protected]) ***/
 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 ([email protected]) ***/
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();
    }
时间: 2024-11-14 12:01:58

servlet应用具体实例的相关文章

JSP+servlet简单登录实例

一个简单的jsp+servlet实例,实现简单的登录 转载▼ http://blog.sina.com.cn/s/blog_5c5bc9070100z7wb.html     开发环境myeclipse+tomcat6 1.先创建web project,项目名为RegisterSystem, 2.在WebRoot 目录下创建login.jsp文件,只需修改body中的内容,如下所示: <body>     <form action="login">     us

【转】怎样理解Servlet的单实例多线程

转自 http://blog.chinaunix.net/uid-7374279-id-3687149.html 首先明确:Servlet是单实例的,即对于同一种业务请求只有一个是实例.不同的业务请求可以通过分发来产生多个实例.其次:单实例的原因我想是因为单实例足可以处理某一个请求,就像ibatis的Querydao.UpdateDao一样都是单实例的.再次:为什么单实例足可以处理某一个请求,因为Servlet是单实例多线程的.http://hiyachen.cublog.cn  [email 

每一个Servlet只有一个实例,多个线程

每一个Servlet只有一个实例,多个线程: Servlet: package com.stono.servlet.synchronize; import javax.servlet.http.HttpServlet; public class TestServletC extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(javax.servlet.http.Htt

Tomcat+Servlet登录页面实例

概念   Tomcat 服务器是一个免费的开放源代码的Web 应用服务器,属于轻量级应用服务器,在中小型系统和并发访问用户不是很多的场合下被普遍使用,是开发和调试JSP 程序的首选.对于一个初学者来说,可以这样认为,当在一台机器上配置好Apache 服务器,可利用它响应HTML(标准通用标记语言下的一个应用)页面的访问请求.实际上Tomcat 部分是Apache 服务器的扩展,但它是独立运行的,所以当你运行tomcat 时,它实际上作为一个与Apache 独立的进程单独运行的.   Servle

Servlet的HelloWorld实例

以下内容引用自http://wiki.jikexueyuan.com/project/servlet/first-example.html: Servlets是Java类,服务于HTTP请求并实现了javax.servlet.Servlet接口.Web应用程序开发人员通常编写扩展javax.servlet.http.HttpServlet的Servlets,它是一个实现了Servlet接口的抽象类并且是为处理HTTP请求专门设计的. 一.Hello World的示例代码 下面是Servlet编写

EL表达式获取Servlet的值实例

E L(Expression Language) 目的:为了使JSP写起来更加简单.它提供了在 JSP 中简化表达式的方法 pageScope requestScope sessionScope applicationScope的权限问题. 1.Servlet的代码:  public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException

Javabean+servlet+JSP(html)实例应用

大家都知道Javabean+servlet+JSP是最简单的MVC模式.的确,在一个小型的项目中,这个模式完全够用. 它优雅并且简洁.加上jQueryui的完美展示效果,让这个模式看起来非常合适.当然这其中必不可少的是ajax和json格式的应用. 1.首先从数据库(sqlserver)中获取结果集并封装到Javabean中.在此之前要定义你所需要的bean类. /* * 查询Branch */ public ArrayList<Branch> selBranch(Connection con

对Servlet单实例多线程的理解。

0 Servlet如何处理多个请求访问? Servlet容器默认是采用单实例多线程的方式处理多个请求的: Servlet是单实例多线程运行方式,所以对象变量线程不安全,局部变量线程安全的. 1.当web服务器启动的时候(或客户端发送请求到服务器时),Servlet就被加载并实例化(只存在一个Servlet实例): 2.容器初始化化Servlet主要就是读取配置文件(例如tomcat,可以通过servlet.xml的<Connector>设置线程池中线程数目,初始化线程池通过web.xml,初始

以登录实例简介Servlet使用

1.简介 Java Servlet 是运行在 Web 服务器或应用服务器上的程序,使用 Servlet,您可以收集来自网页表单的用户输入,呈现来自数据库或者其他源的记录,还可以动态创建网页. 作用:Web 浏览器的请求和 HTTP 服务器上的数据库或应用程序之间的中间层. 简单说就是接受客户端传递的输入然后进行处理(链接数据库等)然后在将相应的结果(html等)在返回给客户端显示相当于MWC中的Ccontroller层. Servlet程序的基本运行过程时序图:下面通过一个登陆实例来简单说明下.