HTTP POST
Have you ever wanted to create/access a web service, but found solutions like SOAP just too slow? We ran into this problem at work not too long ago. With some help, I had created a web service accessible via SOAP. However, when we deployed and ran it in production for a couple of weeks, we were not satisfied with the speed and memory performance of the entire process. Since the service was (potentially) a high volume call, we determined that the overhead of using SOAP was a big hang up. The wrapping and unwrapping of the SOAP envelope for each call was really bogging down the server's processor. Another solution was needed.
We caught a break in that the service was strictly an internal call, therefore conforming to some silly (kidding!) standard to support external customers was not needed. We turned to something pretty basic, a bare-bones HTTP POST. The service would accept incoming XML, perform it's function and output XML as a response.
What we need: a Java servlet. Yep, that's it, just have the servlet respond via POST and you can rig it to capture incoming XML and pass it along to your business logic to do the dirty work.
To test the servlet, we can create a simple JSP page:
public class PostServlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
BufferedReader in = null;
PrintWriter out = null;
try {
in = request.getReader();
out = response.getWriter();
// Set the response content-type
response.setContentType("text/plain");
// Read the incoming data from the request
String line = null;
StringBuffer inData = new StringBuffer();
while ((line = in.readLine()) != null) {
inData.append(line + "\n");
}
// Send the email message
BusinessLogicObject bc = new BusinessLogicObject();
String returnValue = bc.executeBusinessLogic(inData.toString());
// Write the return value to the response
out.print(returnValue);
}
catch (Throwable e) {
out.close();
in.close();
e.printStackTrace();
}
}
}
<%@ page language = "java"Boom, done, that's all she wrote. Until next time.
import = "java.text.*, java.util.*, java.io.*, java.net.*, java.sql.*"
%>
<%
String submitted = request.getParameter("submitted");
String templateId = "";
String xmlDoc = "";
String servletResponse = "";
if ("1".equals(submitted)) {
// Process the form and send the emailer message
templateId = request.getParameter("templateId");
xmlDoc = request.getParameter("xmlDoc");
// Compose the HTTP POST
PropertyManager.load("/emailer");
String emailerWebServiceHost = PropertyManager.getProperty("web_service_host");
String urlString = "http://" + emailerWebServiceHost + "/servlets/PostServlet";
URL url = new URL(urlString);
HttpURLConnection con = (HttpURLConnection)url.openConnection();
con.setAllowUserInteraction(true);
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(true);
con.setFollowRedirects(false);
con.setRequestMethod("POST");
con.connect();
// Set the request info (XML)
PrintWriter output = new PrintWriter(new OutputStreamWriter(con.getOutputStream()));
output.print(xmlDoc);
output.flush();
output.close();
// Get the response (String)
BufferedReader input = new BufferedReader(new InputStreamReader(con.getInputStream()));
String line = null;
StringBuffer inputData = new StringBuffer();
while ((line = input.readLine()) != null) {
inputData.append(line + "\n");
}
servletResponse = inputData.toString();
}
%>
<html>
<head>
<title>Send Email Servlet Test Page</title>
</head>
<body>
<p><b>Send Email Servlet Test Page</b></p>
<form action="servletTest.jsp" method="post">
<table border="0" align="center">
<tr>
<td>Select the template ID to use:</td>
</tr><tr>
<td>
<select name="templateId">
<option value="XMLType1">XMLType1</option>
<option value="XMLType2">XMLType2</option>
<option value="XMLType3">XMLType3</option>
<option value="XMLType4">XMLType4</option>
</select>
</td>
</tr><tr>
<td> </td>
</tr><tr>
<td>Specify the XML file to use:</td>
</tr><tr>
<td><textarea name="xmlDoc" cols="100" rows="20"></textarea></td>
</tr><tr>
<td align="center">
<input type="submit" name="submitButton" value="Submit" />
<input type="reset" name="resetButton" value="Reset" />
<input type="hidden" name="submitted" value="1" />
</td>
</tr>
</table>
</form>
<hr />
<table border="0" align="left">
<tr>
<td colspan="2"><b>Output</b></td>
</tr><tr>
<td>Template ID:</td>
<td><%= templateId %></td>
</tr>
</table>
<br clear="all" />
<br clear="all" />
<p>Servlet Output</p>
<pre>
<%= servletResponse %>
</pre>
</body>
</html>
No comments:
Post a Comment