java web http请求转发

简介: <p><span style="font-family:Helvetica,Tahoma,Arial,sans-serif; font-size:14px; line-height:21px">java web,如何获取request中的请求参数呢?</span></p> <p><span style="font-family:Helvetica,Tahoma,Arial,sans-se

java web,如何获取request中的请求参数呢?

/***
	 * Get request query string
	 * @param request
	 * @return   byte[]
	 */
	public byte[] getRequestStr(HttpServletRequest request){
		int contentLength = request.getContentLength();
		byte buffer[] = new byte[contentLength];
		for (int i = 0; i < contentLength;) {
			try {

				int readlen = request.getInputStream().read(buffer, i,
						contentLength - i);
				if (readlen == -1) {
					break;
				}
				i += readlen;
			} catch (IOException ioexception) {
				ioexception.printStackTrace();
			} finally {
				// logger.info("Json Request:" + requestPacket);
			}
		}
		return buffer;
	}

上述方法返回的是byte数组。

下面的方法直接返回字符串:

/***
	 * Get request query string
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	public String getRequestStr(HttpServletRequest request) throws UnsupportedEncodingException{
		byte buffer[]=getRequestBytes(request);
		String charEncoding=request.getCharacterEncoding();
		if(charEncoding==null){
			charEncoding="UTF-8";
		}
		return new String(buffer,charEncoding);
	}

应用 :上述方法一般用于在filter(javax.servlet.Filter)中获取请求参数,进行转发

java web中,重写response应答体(响应体),如何往应答体中写入指定内容呢?

/***
	 * Send http request
	 * 
	 * @param response
	 * @param bytes  :字节数组
	 * @param contentType  :if is null,default value is  "application/json"
	 * @param encoding   :  编码方式
	 * @throws IOException
	 */
	public static void sendRequestWriter(HttpServletResponse response, byte[] bytes,
			String contentType,String encoding) throws IOException {
		response.setContentLength(bytes.length);
		if (contentType == null) {
			contentType = "application/json";
		}
		response.setContentType(contentType);

		PrintWriter printer = response.getWriter();
		printer.println(new String(bytes,encoding));
		printer.flush();
		printer.close();
	}
	
	/***
	 * 
	 * @param response
	 * @param sendData   :<code>String</code>
	 * @param contentType
	 * @param encoding : such as GBK/utf-8
	 * @throws IOException
	 */
	public static void sendRequestWriter(HttpServletResponse response, String sendData,
			String contentType,String encoding) throws IOException {
//		response.setContentLength(sendData.getBytes(encoding).length);
		byte[]bytes=sendData.getBytes(encoding);
		sendRequestWriter(response, bytes, contentType, encoding);
	}

 以上方法都是使用PrintWriter来写入response的。

下面的方式是使用流的方式写入response:

/***
	 * test ok
	 * @param response
	 * @param bytes
	 * @param contentType
	 * @param encoding
	 * @throws IOException
	 */
	public static void sendRequestStream(HttpServletResponse response, byte[] bytes,
			String contentType) throws IOException {
		response.setContentLength(bytes.length);
		if (contentType == null) {
			contentType = "application/json";
		}
		response.setContentType(contentType);

		ServletOutputStream sos = response.getOutputStream();
		sos.write(bytes, 0, bytes.length);
		sos.flush();
		sos.close();
	}

 应用:用于在网关中进行请求转发和响应。





相关文章
|
10天前
|
SQL Java
20:基于EL与JSTL的产品管理页-Java Web
20:基于EL与JSTL的产品管理页-Java Web
21 5
|
4天前
|
前端开发 JavaScript Java
Java与Web开发的结合:JSP与Servlet
Java与Web开发的结合:JSP与Servlet
8 0
|
10天前
|
设计模式 前端开发 Java
19:Web开发模式与MVC设计模式-Java Web
19:Web开发模式与MVC设计模式-Java Web
20 4
|
10天前
|
设计模式 存储 前端开发
18:JavaBean简介及其在表单处理与DAO设计模式中的应用-Java Web
18:JavaBean简介及其在表单处理与DAO设计模式中的应用-Java Web
25 4
|
10天前
|
SQL Java 数据库连接
17:数据库连接池与Servlet整合-Java Web
17:数据库连接池与Servlet整合-Java Web
22 3
|
10天前
|
存储 前端开发 搜索推荐
13:Session机制实现用户登录与注销功能-Java Web
13:Session机制实现用户登录与注销功能-Java Web
25 3
|
10天前
|
XML 前端开发 Oracle
16:JSP简介、注释与Scriptlet、Page指令元素、Include操作、内置对象、四种属性-Java Web
16:JSP简介、注释与Scriptlet、Page指令元素、Include操作、内置对象、四种属性-Java Web
13 2
|
10天前
|
缓存 前端开发 Java
15:Servlet 3.0文件上传与下载-Java Web
15:Servlet 3.0文件上传与下载-Java Web
24 5
|
10天前
|
存储 缓存 前端开发
14:Servlet中的页面跳转-Java Web
14:Servlet中的页面跳转-Java Web
26 6
|
10天前
|
存储 前端开发 安全
13:会话跟踪技术Session的深度应用与实践-Java Web
13:会话跟踪技术Session的深度应用与实践-Java Web
25 3