zoukankan      html  css  js  c++  java
  • servlet解决浏览器的跨域

    概述:

    在做一些web请求时,会涉及到很麻烦的浏览器跨域问题,因此,一般都会通过后台处理再给前台。本文讲述通过servlet解决浏览器的跨域问题。


    实现:

    在工程下新建一个servlet,实现代码如下:

    package lzugis.web.agent;
    
    import java.io.BufferedInputStream;
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.net.URL;
    import java.net.URLConnection;
    
    import javax.servlet.ServletException;
    import javax.servlet.ServletOutputStream;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    /**
     * Servlet implementation class WebAgent
     */
    @WebServlet(name="WebAgent",urlPatterns="/web/agent")
    public class WebAgent extends HttpServlet {
    	private static final long serialVersionUID = 1L;
    	private String debug = "false";
    
        /**
         * Default constructor. 
         */
        public WebAgent() {
            // TODO Auto-generated constructor stub
        }
    
    	/**
    	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
    	 */
    	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		// TODO Auto-generated method stub
    		String encoding = getInitParameter("encoding");
    	    if ((encoding != null) && (!"".equals(encoding))) {
    	      request.setCharacterEncoding(encoding);
    	    }
    	    String url = request.getParameter("url");
    	    if (!"".equals(url)) {
    	      responseFile(request, response, url);
    	      return;
    	    }
    	}
    
    	/**
    	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
    	 */
    	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		// TODO Auto-generated method stub
    		String encoding = getInitParameter("encoding");
    	    if ((encoding != null) && (!"".equals(encoding))) {
    	      request.setCharacterEncoding(encoding);
    	    }
    	    String url = request.getParameter("url");
    	    if (!"".equals(url)) {
    	      responseFile(request, response, url);
    	      return;
    	    }
    
    	    if ("true".equals(this.debug)) {
    	    	String data = readXMLFromRequestBody(request);
    	    	System.out.println(data);
    	    }
    	}
    	
    	public void responseFile(HttpServletRequest req, HttpServletResponse resp, String url) {
    	    try {
    	      URL u = new URL(url);
    
    	      URLConnection urlc = u.openConnection();
    	      urlc.setConnectTimeout(3000);
    	      urlc.setReadTimeout(100000);
    
    	      resp.setContentType(urlc.getContentType());
    
    	      ServletOutputStream out = null;
    	      BufferedInputStream input = null;
    	      byte[] data = new byte[1024];
    	      int length = -1;
    	      input = new BufferedInputStream(urlc.getInputStream());
    	      out = resp.getOutputStream();
    	      while ((length = input.read(data)) != -1) {
    	        out.write(data, 0, length);
    	      }
    	      input.close();
    	      out.flush();
    	      out.close();
    	    }
    	    catch (IOException localIOException)
    	    {
    	    }
    	  }
    
    	  private String readXMLFromRequestBody(HttpServletRequest request) {
    	    StringBuffer strBuf = new StringBuffer();
    	    try {
    	      BufferedReader reader = request.getReader();
    	      String line = null;
    	      while ((line = reader.readLine()) != null)
    	        strBuf.append(line).append("
    ");
    	    } catch (IOException e) {
    	      e.printStackTrace();
    	    }
    	    return strBuf.toString();
    	  }
    }
    
    说明:

    servlet是通过注解的方式配的,访问路径为:localhost:8080/lzugis/web/agent?url=http://localhost:6080/arcgis/rest/services/?f=pjson。




  • 相关阅读:
    JAVA设计模式---总述篇
    Java中对象创建时的内存分配
    for循环打印空心菱形的新方法
    springcloud2.X通过actuator加载配置无效问题
    golang-错误处理
    golang-字符串
    golang-方法和接口
    golang-结构体与指针
    golang-数组、切片、映射
    golang-流程控制
  • 原文地址:https://www.cnblogs.com/lzugis/p/6539801.html
Copyright © 2011-2022 走看看