我的 Spring-Flex 项目如何通过 Java 使用普通 HttpServlet?
我有一个有点棘手的问题。我正在尝试找出利用我正在开发的 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。无论我如何解决这个问题,我都希望尽可能接近“最佳实践”。有人可以提供任何帮助吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果不对代码进行一些重构,我认为没有任何好方法可以从应用程序的服务层执行此操作。让您的服务层以任何方式依赖于 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