servlet 消息转发

简介:           reportServlet servlet.ReportServlet reportServlet /report               import java.

 

 

 

 

 

<?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捐助),没钱捧个人场,谢谢各位。



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

 

 

 

目录
相关文章
|
4月前
|
前端开发 Java 程序员
Servlet/过滤器/拦截器/监听器:
Servlet接口在Sun公司有两个默认的实现类:HttpServlet、GenericServlet。Servlet是一种运行服务器端的Java应用程序,具有独立于平台和协议的特性,并且可以动态的生成web页面,它工作在客户端请求与服务器响应的中间层。
|
4月前
|
Java 数据库 容器
Servlet3.0中支持的异步处理
Servlet3.0中支持的异步处理
31 1
|
8月前
|
存储 Java 数据安全/隐私保护
servlet过滤器与监听器
前言 Servlet 过滤器和监听器是 Java Web 应用程序中常见的两种组件,它们提供了各种扩展 Web 应用程序功能的方式。 总的来说,过滤器和监听器都可以通过 Java Web 应用程序的配置文件或注解进行使用,方便灵活,并可以很好地实现框架与业务逻辑的分离,提高代码可维护性和扩展性。 过滤器(Filter) 1. 参数验证和转换:可以拦截用户提交的数据,并对数据格式进行验证、修正或转换。 2. 访问控制和认证:可以拦截请求并检查用户是否有访问特定资源的权限。 3. 日志记录:可以拦截请求并输出相应的日志信息,用于系统运行时的监测与故障排除。 4. 资源压缩和解密: 可以拦截响应并对
49 0
QGS
|
11月前
|
存储 JavaScript 前端开发
浅谈SpringMVC之request请求转发传递数据
SpringMVC默认的参数类型 SpringMVC底层是Servlet HttpServletRequest、HttpServletResponse、HttpSession、Model、Map、ModelMap
QGS
127 0
|
XML 前端开发 Java
关于服务连接器(Servlet)你了解多少?
Servlet是JavaWeb最为核心的内容,它是Java提供的一门动态web资源开发技术。 使用Servlet就可以实现,根据不同的登录用户在页面上动态显示不同内容。 Servlet是JavaEE规范之一,其实就是一个接口,将来我们需要定义Servlet类实现Servlet接口,并由web服务器运行Servlet
56 0
关于服务连接器(Servlet)你了解多少?
Servlet学习(四):请求的分发处理
Servlet学习(四):请求的分发处理
|
JavaScript
Servlet学习——监听器
Servlet学习——监听器
131 0
|
XML 设计模式 前端开发
Servlet、过滤器、监听器、拦截器
Servlet是javaEE规范中的一种,javaEE中的规范很多除了Servlet还有很多我们熟悉的JSP、JDBC、RMI、XML、EJB、JTS等等。
125 0
Servlet、过滤器、监听器、拦截器
|
存储 缓存 监控
servlet中的监听器接口
servlet中的监听器接口
78 0
servlet中的监听器接口