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

发布于 2024-10-25 09:31:34 字数 3819 浏览 1 评论 0 原文

我有一个有点棘手的问题。我正在尝试找出利用我正在开发的 Spring-Flex(带有 BlazeDS)项目中的普通 HttpServlet 的最佳方法。我的团队有一个他们过去使用过的 HttpServlet(其他一些团队构建的),用于处理给定通过 HTTP GET 传递的一些键/值对的请求。我想使用这个 HttpServlet 类来完成相同的工作,但当我的 Flex 客户端调用服务上的方法时,我想用 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 工厂吗?
  • HttpServlet 可以是 Spring 托管 bean,并以另一种方式调用 ASP 页面吗?
  • (我最不喜欢这个想法)我应该从 Flex 调用 HttpServlet,并通过 远程对象

我固执地想要从 Java 中执行此操作的原因是我想要进行一些日志记录,并且可能在 @Service 类的范围内利用一些其他 Singleton bean。无论我如何解决这个问题,我都希望尽可能接近“最佳实践”。有人可以提供任何帮助吗?

I've got a bit of a tricky question. I'm trying to figure out the best way to leverage a vanilla HttpServlet from a Spring-Flex (with BlazeDS) project I'm working on. My team has an HttpServlet that they've used in the past (that some other team built) that processes a request given some key/value pairs passed over HTTP GET. I want to use this HttpServlet class to do the same work, but I want to call it in Java, when my Flex client invokes a method on a Service. Ideally, the class invoking the HttpServlet can use the standard @Service and @RemotingDestination annotations (see here).

So, this is the 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);
    }
}

What I'm trying to figure out is how to write my @Service class. I've tried going down a few different roads, but haven't had much luck. So far, I've considered and come up with secondary questions on the matter (but I've yet to work out the solution):

  • Do I need a @Controller for this sort of work?
  • Do I need a Servlet Factory?
  • Can the HttpServlet be a Spring managed bean, and call the ASP page another way?
  • (I like this idea least) Should I call the HttpServlet from Flex, and leverage logging and data the Servlet needs with a Remote Object?

The reason for my stubbornness in wanting to do this from Java is that I want to do some logging, and maybe leverage some other Singleton beans in the scope of the @Service class. However I work this out, I want to be as close to "Best Practices" as possible. Can anyone offer any help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

川水往事 2024-11-01 09:31:34

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

实际上,我认为如果您根本不想更改 Servlet,那么从 Flex 调用 Servlet 是最简单的方法。我不明白为什么使用 HttpService 来实现此目的不是很简单:
http://livedocs.adobe.com/blazeds/1/blazeds_devguide/rpc_httpws_06 .html#1118029

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

Without doing some refactoring of the code, I don't think there's any great way to do this from your application's service layer. It is typically not a good idea to have your service layer be dependent on the Servlet API in any way, and that would be required if you were going to try to invoke the Servlet by simulating an HTTP request.

Actually, I think invoking the Servlet from Flex is the simplest way to go if you don't want to change the Servlet at all. I don't see any reason why it wouldn't be quite straightforward to use HttpService for this:
http://livedocs.adobe.com/blazeds/1/blazeds_devguide/rpc_httpws_06.html#1118029

Otherwise, if you really must invoke this ASP endpoint from within the context of your service tier, I would recommend refactoring the bit that does the invocation into a separate @Service class that has no dependency on the Servlet API. You could potentially even simplify the code a bit by using Spring's RestTemplate to do the invocation and handle the result:
http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/remoting.html#rest-client-access

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文