当前位置: 代码迷 >> Web前端 >> HttpServlet Web 通信
  详细解决方案

HttpServlet Web 通信

热度:872   发布时间:2012-09-02 21:00:34.0
HttpServlet Web 通讯
//httpservlet  Web 请求


public class PointPayBO {

   
    public int payPoint(NewUserInfo userInfo) {
        int retVal = ResourceDef.RESULT_FAILED;
       
        String servletUrl = "http://192.168.0.1:8443/eitp/servlet/PointPayServlet"
            + "?barID=1&serviceid=50001"
            + "&param=";// 请求的url
       
        String param = "type=1";
        PostMethod httppost = null;
        HttpClient httpclient = new HttpClient();
        try {
           
            httpclient.getParams().setParameter("http.socket.timeout",new Integer(60 * 1000));   //设置超时1分钟
            httppost = new PostMethod(servletUrl + URLEncoder.encode(param));
            httpclient.executeMethod(httppost);
            int result = httppost.getStatusCode();
           
           
            if (result == 200) {
                byte[] by = httppost.getResponseBody();
               
                String retStr = new String(by);
               
                String[] retStrinfo = retStr.split("\\|", -1);
               
                retVal = Integer.parseInt(retStrinfo[0]);
            }
        } catch (IOException e) {
            logger.error(" UpdateMemberInfo  getCenterValue() ...  ", e);
           
            retVal = ResourceDef.RESULT_FAILED;
        } finally{
            httppost.releaseConnection();
            httpclient = null;
        }
       
        return retVal;
    }
}


//收到请求信息
public class PointPayServlet extends HttpServlet {

   
    public PointPayServlet() {
        super();
    }

   
    public void destroy() {
        super.destroy(); // Just puts "destroy" string in log
        // Put your code here
    }

   
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }

   
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setCharacterEncoding("GBK");

        PrintWriter out = response.getWriter();

        String param = request.getParameter("param");
       
        int serviceid = Tools.str2int(request.getParameter("serviceid"));

        if(param == null || "".equals(param.trim())){
            out.println(ResourceDef.RESULT_FAILED);
        }else{
            String barID = request.getParameter("barID");
            try {
                if (serviceid == ResourceDef.BAR_SERVICEID_POINTPAY) {
                    out.println(new PointPayBean().pay(param, barID));
                }

            }
            catch (Exception e) {
                logger.error("error", e);
                out.println(ResourceDef.RESULT_FAILED);
            }
        }

        out.flush();
        out.close();
    }

        public void init() throws ServletException {
        // Put your code here
    }

}



public class PointPayBean {
   
  
        public String pay(String param, String barID) {
        int retVal = ResourceDef.RESULT_FAILED;
        String msg = "";
       
        String str[] = CommonUtils.splitStr(param);
       
        boolean falg = true;
        boolean isServerError = false;
        String message = null;
        if(str == null){
            retVal = ResourceDef.RESULT_FAILED;
            msg = "请求数据为空, 请检查输入数据!";
        }
        else{
                  }

        return retVal + CommunicationResourceDef.SPILT + msg;




web.xml配置

<servlet>
<servlet-name>PointPayServlet</servlet-name>
<servlet-class>
com.hintsoft.pubwin.netcafe.communication.bar.PointPayServlet
</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>PointPayServlet</servlet-name>
<url-pattern>/servlet/PointPayServlet</url-pattern>
</servlet-mapping>
      
  相关解决方案