繁体   English   中英

我的Spring-Flex项目如何通过Java使用普通的HttpServlet?

[英]How can my Spring-Flex project use a vanilla HttpServlet via Java?

我有一个棘手的问题。 我试图从我正在研究的S​​pring-Flex(带有BlazeDS)项目中找出利用香草HttpServlet的最佳方法。 我的团队拥有一个他们过去使用过的HttpServlet(由其他团队构建),它通过给定一些通过HTTP GET传递的键/值对来处理请求。 我想使用该HttpServlet类来完成相同的工作,但是当我的Flex客户端在Service上调用方法时,我想在Java中调用它。 理想情况下,调用HttpServlet的类可以使用标准的@Service@RemotingDestination批注( 请参见此处 )。

因此,这是HttpServlet:

import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
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;

public class SomeServlet extends HttpServlet {

    private int responseCode;
    private String responseMsg;
    private String dynamicValue;

    public void processRequest(HttpServletRequest req, HttpServletResponse res)
    throws ServletException, IOException {
    try {

        res.setContentType("text/html");
        res.setHeader("Cache-Control", "no-cache");
        PrintWriter out = res.getWriter();

        StringBuffer postdata = new StringBuffer();
        StringBuffer parameterssb = new StringBuffer();
        HttpURLConnection conn = null;

        if (req.getParameter("dynamicValue") != null)
        dynamicValue = req.getParameter("dynamicValue").toString();

        parameterssb.append("key=value&key2=" + dynamicValue);

        postdata.append("GET /wr/ProcessIt.asp HTTP/1.0\n");
        postdata.append("Pragma: no-cache\n");
        postdata.append("Content-Type: application/x-www-form-urlencoded\n");
        postdata.append("Content-Length: " + parameterssb.length() + "\n");
        postdata.append("\n");
        postdata.append(parameterssb.toString());
        postdata.append("\n\n");

        String urlString = "";
        urlString = "http://domain.com/wr/ASP_processing_page.asp";
        URL url = new URL(urlString);
        HttpURLConnection.setFollowRedirects(true);
        conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("GET");
        conn.setDoOutput(true);
        conn.connect();

        OutputStreamWriter osw = new OutputStreamWriter(
            conn.getOutputStream());
        osw.write(postdata.toString());
        osw.flush();
        responseCode = conn.getResponseCode();
        responseMsg = conn.getResponseMessage();

        String redirURL = conn.toString();

        if (redirURL.indexOf("success=yes") >= 0) {
        // Handle success
        } else {
        // Handle failure
        }

        if (conn != null) {
        conn.disconnect();
        }
        out.close();
    } catch (Exception e) {
        // Handle failure
    }
    }

    public void doGet(HttpServletRequest req, HttpServletResponse resp)
    throws ServletException, IOException {
    processRequest(req, resp);
    }

    public void doPost(HttpServletRequest req, HttpServletResponse resp)
    throws ServletException, IOException {
    processRequest(req, resp);
    }
}

我想弄清楚的是如何编写我的@Service类。 我已经尝试过走几条路,但是运气并不好。 到目前为止,我已经考虑并提出了有关此问题的次要问题(但我尚未制定解决方案):

  • 我需要@Controller来完成此类工作吗?
  • 我需要一个Servlet Factory吗?
  • HttpServlet可以是Spring托管的bean,可以用另一种方式调用ASP页面吗?
  • (我最不喜欢这个想法)我是否应该从Flex调用HttpServlet,并通过Remote Object利用Servlet需要的日志和数据?

我之所以想从Java中执行此操作,是因为我很固执,这是因为我想进行一些日志记录,并且可能会利用@Service类范围内的其他Singleton bean。 但是,无论如何我都想尽可能地接近“最佳做法”。 谁能提供任何帮助?

如果不对代码进行一些重构,我认为没有任何很好的方法可以从应用程序的服务层执行此操作。 让服务层以任何方式依赖于Servlet API通常不是一个好主意,如果要尝试通过模拟HTTP请求来调用Servlet,则需要这样做。

实际上,如果您根本不想更改Servlet,我认为从Flex调用Servlet是最简单的方法。 我没有看到使用HttpService这样做不是那么简单的任何原因: http : //livedocs.adobe.com/blazeds/1/blazeds_devguide/rpc_httpws_06.html#1118029

否则,如果您确实必须从服务层的上下文中调用此ASP终结点,则建议将进行调用的位重构为一个独立的@Service类,该类不依赖Servlet API。 您甚至可以通过使用Spring的RestTemplate进行调用并处理结果来稍微简化代码: http ://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/remoting 。 html#rest-client-access

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM