从HttpServletRequest获取POST数据的代码
我们经常需要从HttpServletRequest对象获取POST请求的数据,下面给出简练的代码共大家参考
StringBuffer jb = new StringBuffer(); String line = null; try { BufferedReader reader = request.getReader(); while ((line = reader.readLine()) != null) jb.append(line); } catch (Exception e) { /*report an error*/ }
注意返回时应常会遇到中文乱码问题,记得加上下面的两个语句,设置response对象的字符集,
response.setCharacterEncoding("GBK");
response.setContentType("application/xml; charset=GBK");