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

点点圈 提交于 2019-12-11 20:39:17

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/5391823/how-can-my-spring-flex-project-use-a-vanilla-httpservlet-via-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!