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?