`
knight_black_bob
  • 浏览: 821487 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

servlet 消息转发

阅读更多

 

 

 

 

 

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee" xmlns:jsp="http://java.sun.com/xml/ns/javaee/jsp"
	xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
	version="3.0">
	<display-name></display-name>
	 
	 
	<servlet>   
    <servlet-name>reportServlet</servlet-name>   
    <servlet-class>servlet.ReportServlet</servlet-class>   
</servlet>   
<servlet-mapping>   
    <servlet-name>reportServlet</servlet-name>    
    <url-pattern>/report</url-pattern>   
</servlet-mapping>

 
	 
</web-app>

 

 

 

 

 

 

 

import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
 
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

 
@SuppressWarnings("serial")
public class ReportServlet extends HttpServlet{

	static final String  DOMAINURL="http://localhost:80/xxx/xxx/xxx";
	
	@Override  
	 protected void doGet(HttpServletRequest req, HttpServletResponse resp)   
	            throws ServletException, IOException {  
    	super.doPost(req, resp);
	 }   
	    

	 @Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException { 
		 
		   InputStream is = null;
		   ByteArrayOutputStream baos =null;
			try
			{
				is = req.getInputStream();
				baos = new ByteArrayOutputStream();
				int readBytes = 0;
				byte[] bs = new byte[1024];
				while ((readBytes = is.read(bs)) > 0)
					baos.write(bs, 0, readBytes);
			}	catch (IOException e)
			{ 
				e.printStackTrace();
				resp.getWriter().write("{\"errorCode\":401}");
			}
			finally
			{
				if (is != null)
					try
					{
						is.close();
					}
					catch (IOException e)
					{
						e.printStackTrace();
						resp.getWriter().write("{\"errorCode\":401}");
					}
			}
		 
		  
			InputStream in = null;
			HttpURLConnection conn = null;
			String setCookie = null;
			try
			{
				System.out.println("SendTo: " + DOMAINURL);
				URL send_url = new URL(DOMAINURL);

				conn = (HttpURLConnection) send_url.openConnection();
				conn.setConnectTimeout(10000);
				conn.setDoOutput(true);
				conn.setUseCaches(false);
				conn.setRequestProperty("Content-type", "application/x-www-form-urlencoded;charset=UTF-8");
				conn.setRequestMethod("POST");
				conn.connect();

				OutputStream raw = conn.getOutputStream();
				OutputStream buf = new BufferedOutputStream(raw);
				OutputStreamWriter out = new OutputStreamWriter(buf, "UTF-8");
				
				out.write(baos.toString());
				out.flush();
				out.close();

				in = conn.getInputStream();
				setCookie = conn.getHeaderField("Set-Cookie");
				// 采用byte流读取

				ByteArrayOutputStream baos2 = new ByteArrayOutputStream();
				byte[] bytes = new byte[1];
				while (in.read(bytes) != -1)
				{
					baos2.write(bytes);
				}
				in.close();
				String ouputData = new String(baos2.toByteArray());
				System.out.println("ouputData = " + ouputData);

				resp.getWriter().write(ouputData); 
			}
			catch (Exception e)
			{ 
				e.printStackTrace();
				resp.getWriter().write("{\"errorCode\":401}");
			}
			finally
			{
				try
				{
					if (in != null)
						in.close();
					if (conn != null)
						conn.disconnect();
				}
				catch (IOException e)
				{
					e.printStackTrace();
					resp.getWriter().write("{\"errorCode\":401}");
				}
			}
		 
		 
		 
		 /*ServletContext application = getServletContext(); 
         RequestDispatcher dispatcher = application.getRequestDispatcher(DOMAINURL); 
         dispatcher.forward(req,resp);*/
	}
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

捐助开发者

在兴趣的驱动下,写一个免费的东西,有欣喜,也还有汗水,希望你喜欢我的作品,同时也能支持一下。 当然,有钱捧个钱场(右上角的爱心标志,支持支付宝和PayPal捐助),没钱捧个人场,谢谢各位。



 
 
 谢谢您的赞助,我会做的更好!

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics