JSP发布JSON数据不发送数据

时间:2018-08-12 16:40:41

标签: jsp jsp-tags

为了克服JavaScript CORS问题,我编写了proxy.jsp文件。该jsp文件将充当中间服务器,它将从js文件中获取所需的url和数据。然后建立与url的连接,并发出get / post(在以下情况下是其发布)请求。

<%@page contentType="application/json; charset=UTF-8"%>

<%@page import="java.io.*" %>
<%@page import="java.net.*" %>

<%
        // many of these calls can throw exceptions, so i've just
        // wrapped them all in one try/catch statement.
        try
        {
            // create a url object
            String path = request.getParameter("name");

            // Read from request and get the data to update
            StringBuilder buffer = new StringBuilder();
            BufferedReader reader = request.getReader();
            String line;
            while ((line = reader.readLine()) != null) {
                buffer.append(line);
            }

            URL url = new URL("http://..baseUrl../"+path);

            // create a urlconnection object
            URLConnection urlConnection = url.openConnection();
            out.println(urlConnection);
            urlConnection.setDoOutput(true);
            urlConnection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");

            //Start post only - START

            //update the data with new data
            OutputStreamWriter bwr = new OutputStreamWriter(urlConnection.getOutputStream());
            bwr.write(buffer.toString());
            bwr.flush();
            bwr.close();

            // For POST only - END

        }
        catch(Exception e)
        {
          e.printStackTrace();
        }
        out.flush();

  %>

有了这个,我没有收到任何错误,但是当我重新加载URL时,它没有显示我的帖子数据。 这段代码到底出了什么问题?因为我是jsp世界的新手,所以我没有错过任何事情吗?

0 个答案:

没有答案