与客户端对接用户信息的保存方式
在日常开发中,少不了与客户端进行通信,然而一些用户数据是怎么保存比较好的呢
1.保存在服务器端 传给客户端sessionid ,客户端每次之后每次请求都需要传入该sessionid,服务器验证
- //调用
- httpPost.setHeader("Cookie", "JSESSIONID=2F616197147E641684F30570028E4535");
- //处理
- public ActionForward clientmanage(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)throws Exception {response.setCharacterEncoding("utf-8");//保存xml传过来的参数Map<String,String> inputMap = new HashMap<String, String>();Map<String,Object> xmlMap=new HashMap<String, Object>();//标识String tflag = "";//返回xmlString xml = "";// StringBuffer sb = new StringBuffer();BufferedReader br = null;String xmlstr = request.getParameter("xml");//解析xmltry{SAXReader reader = new SAXReader();// BufferedReader reReader = request.getReader();//// String line=reReader.readLine();// while(line!=null){// sb.append(line);// line=reReader.readLine();// }// reReader.reset();//HTTP请求if(!StringUtils.isEmpty(xmlstr)){ByteArrayInputStream bos = new ByteArrayInputStream(xmlstr.getBytes());InputStreamReader ir = new InputStreamReader(bos);br = new BufferedReader(ir);}else{br = request.getReader();}Document document = reader.read(br); //读取XML文件Element root = document.getRootElement(); // 得到根节点for(Element e : (List<Element>)root.elements()){inputMap.put(e.getName(), e.getTextTrim());xmlMap.putAll(XmlParserUtil.parseXml(e,true));}System.out.println("get------"+inputMap);//客户手机类型String clientType = request.getParameter("client");inputMap.put("client",StringUtils.isEmpty(clientType) ? "" : clientType);//保存sessionidinputMap.put("sessionid", request.getSession().getId());//获取当前登录用户信息Users user = (Users) RequestUtil.getSessionInfo(request, "user", null);if(user!=null){inputMap.put("userid", user.getId().toString());xmlMap.put("userid", user.getId().toString());xmlMap.put("usercode", user.getUsercode());inputMap.put("usercode", user.getUsercode());}else{xml="<?xml version=\"1.0\" encoding=\"UTF-8\"?><XMLInfo><message><status>"+Constants.CLIENT_MANAGE_INFO_STATUS.TIMEOUT.value()+"</status><errorInfo>请您先登陆系统!</errorInfo></message></XMLInfo>";}tflag = inputMap.get("tflag");//根据标识调用相关方法if("login".equals(tflag)){//用户登录xml = ClientManage.login(request,oaClientService,drpRandomCheckService,oaRandomChcekService,inputMap);}else if("logincrm".equals(tflag)){//内部使用接口,不验证IMEI码xml = ClientManage.logincrm(request,oaClientService, inputMap);}else if(!"".equals(xml)){}else if("search".equals(tflag) ||"sort".equals(tflag)){//信息搜索 or 分类信息列表xml = ClientManage.search(request,oaClientService, inputMap);}else if("home".equals(tflag)){//主页部门列表xml = ClientManage.home(oaClientService, inputMap);}else if("cpwd".equals(tflag)){//密码修改xml = ClientManage.cpwd(request,oaClientService,inputMap);}else if("rimei".equals(tflag)){//IMEI申请xml = ClientManage.rimei(oaClientService,inputMap);}else if("mview".equals(tflag)){//信息查看if(!StringUtils.isEmpty(xmlstr)){xml = ClientViewmsgUtil.viewMsgCDATA(request,oaClientService,oaProveManageService, inputMap);} else{xml = ClientViewmsgUtil.viewMsg(request,oaClientService,oaProveManageService, inputMap);}}else if("enum".equals(tflag)){//枚举数据xml = ClientGetEnumListUtil.getMessageByEnum(oaClientService, inputMap);}else if("ctdeptpeople".equals(tflag)){//部门和人员信息列表获取接口xml = ClientManage.ctdeptpeople(oaClientService,inputMap);}else if("ctpeople".equals(tflag)){//人员信息获取接口xml = ClientManage.ctpeople(oaClientService,inputMap);}else if("ctpsearch".equals(tflag)){//人员信息获取接口xml = ClientManage.ctpsearch(oaClientService,inputMap);}else if("cturlist".equals(tflag)){//意见反馈信息列表获取接口xml = ClientManage.cturlist(oaClientService,inputMap);}else if("cturnew".equals(tflag)){//意见反馈新增接口xml = ClientManage.cturnew(oaClientService,inputMap);}else if("cturrepay".equals(tflag)){//意见反馈新增接口xml = ClientManage.cturrepay(oaClientService,inputMap);}else if("cturrinfo".equals(tflag)){//意见反馈回复信息列表接口xml = ClientManage.cturrinfo(oaClientService,inputMap);}else if("crlist".equals(tflag)){//信息参考信息列表获取接口xml = ClientManage.crlist(oaClientService,inputMap);}else if("crsinfo".equals(tflag)){//信息参考详细信息列表接口xml = ClientManage.crsinfo(oaClientService,inputMap);}else if("crvinfo".equals(tflag)){//信息参考内容查看接口xml = ClientManage.crvinfo(oaClientService,inputMap);}else if("qdept".equals(tflag)){//巡店部门信息列表获取接口-qdeptxml = ClientManage.qdept(oaClientShopVisitService, inputMap);}else if("qdsearch".equals(tflag)){ //巡店部门信息搜索接口-qdsearchxml = ClientManage.qdsearch(oaClientShopVisitService, inputMap);}else if("qdeptnew".equals(tflag)){//巡店信息提交接口-qdeptnewxml =ClientManage.qdeptnew(oaClientShopVisitService, inputMap);}else if("qrsendorget".equals(tflag)){//巡店问题结果提交和获取接口-qrsendorgetxml = ClientManage.qrsendorget(oaClientShopVisitService, inputMap);}else if("sdcollect".equals(tflag)){//销售数据销售查询汇总接口-sdcollectxml = ClientManage.sdcollect(oaUserSaleReportService, inputMap);}else if("sddetail".equals(tflag)){//销售数据销售查询明细接口-sddetailxml = ClientManage.sddetail(oaUserSaleReportService, inputMap);}else if("sdtop".equals(tflag)){//销售数据销售排行接口-sdtopxml = ClientManage.sdtop(oaUserSaleReportService, inputMap);}else if("strdcheck".equals(tflag)){//随机盘点单列表接口-strdcheckxml= ClientManage.strdcheck(drpRandomCheckService, inputMap);}else if("strdckitem".equals(tflag)){//随机盘点单列表商品明细获取接口-strdcheckxml= ClientManage.strdckitem(drpRandomCheckService, inputMap);}else if("mstrdckitem".equals(tflag)){//随机盘点单商品串码更新接口-mstrdckitemxml= ClientManage.mstrdckitem(drpRandomCheckService,oaRandomChcekService,xmlMap);}else if("mprove".equals(tflag)){//OA公文证明数据更新接口-mprovexml= ClientManage.mprove(oaProveManageService,inputMap);}else if("datamsgs".equals(tflag)){//OA数据分发列表-datamsgsxml= ClientManage.datamsgs(oaGatherDataService, inputMap);}else if("datamsgdetail".equals(tflag)){//OA数据分发详情 - datamsgdetailxml= ClientManage.datamsgdetail(oaGatherDataService, inputMap);}else if("mdatamsgdetail".equals(tflag)){//修改数据分发详情 - mdatamsgdetailxml= ClientManage.mdatamsgdetail(oaGatherDataService, xmlMap);}}catch(Exception e){e.printStackTrace();if(br != null){br.close();br = null;}logger.error("手机客户程序异常:"+e.getMessage());// logger.error("异常XML:"+sb.toString());logger.error("传递参数异常,导致解析xml失败!");xml="<?xml version=\"1.0\" encoding=\"UTF-8\"?><XMLInfo><message><status>1</status><errorInfo>传递参数异常,导致解析xml失败!</errorInfo></message></XMLInfo>";}//HTTP请求logger.info("output xml -------------"+xml);System.out.println("out------"+xml);if(!StringUtils.isEmpty(xmlstr)){response.setContentType("text/xml");response.setCharacterEncoding("utf-8");PrintWriter writer = response.getWriter();writer.write(xml);writer.close();return null;}//暂时不处理gzip压缩// GZIP压缩处理ByteArrayOutputStream out = new ByteArrayOutputStream();GZIPOutputStream gzip=new GZIPOutputStream(out);gzip.write(xml.getBytes("UTF-8"));gzip.close();byte[] outbyte=out.toByteArray();int length=out.toByteArray().length;response.setContentLength(length);OutputStream put=response.getOutputStream();put.write(outbyte);put.close();// PrintWriter pw = response.getWriter();// pw.print(xml);return null;}
2.存入客户端,这个可以缓存到客户端数据库 比如sqlite 登录状态的话用户可以手动清理 (android源码中有)
浙公网安备 33010602011771号