WebReference.com - Part 5 of chapter 3 from Professional Java Web Services, Wrox Press Ltd. (5/6)
[previous] [next] |
Professional Java Web Services
RetrieveServlet
The RetrieveServlet
class is located in RetrieveServlet.java
. The class is listed below:
package com.wrox.jobresume.client.retrieve;
// Imports needed for the Servlet API
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import javax.servlet.ServletConfig;
import java.io.PrintWriter;
// Imports needed for the Apache SOAP API
import org.apache.soap.encoding.SOAPMappingRegistry;
import org.apache.soap.encoding.soapenc.BeanSerializer;
import org.apache.soap.Constants;
import org.apache.soap.SOAPException;
import org.apache.soap.Fault;
import org.apache.soap.util.xml.QName;
import org.apache.soap.rpc.Call;
import org.apache.soap.rpc.Parameter;
import org.apache.soap.rpc.Response;
import java.net.URL;
// Other Imports
import java.util.Vector;
import com.wrox.jobresume.common.Resume;
public class RetrieveServlet extends HttpServlet {
static String RPCROUTER = "https://localhost:8080/soap/servlet/rpcrouter";
// Initializes the servlet
public void init(ServletConfig config) throws ServletException {
super.init(config);
}
// Destroys the servlet
public void destroy() { }
protected void processRequest(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, java.io.IOException {
response.setContentType("text/html");
java.io.PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<body>");
out.println("<p align='center'><img align='middle' src='../wroxlogo.gif'</img> <font face='Arial Narrow' size='5'><b>Job Resumé Repository System</b></font></p>");
// Get the Unique ID (UID) of the resumé from the form
String uid = request.getParameter("uid");
SOAPMappingRegistry smr = new SOAPMappingRegistry();
BeanSerializer beanSer = new BeanSerializer();
// Map the types
smr.mapTypes(Constants.NS_URI_SOAP_ENC,
new QName("urn:jobresume", "resume"),
Resume.class, beanSer, beanSer);
// Setup the Call
Call call = new Call();
call.setSOAPMappingRegistry(smr);
call.setTargetObjectURI("urn:JobResumeRepositoryService");
call.setMethodName("retrieve");
call.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);
// Add the parameters
Vector params = new Vector();
params.addElement(new Parameter("uid", String.class, uid, null));
call.setParams(params);
// Invoke the call.
Response resp = null;
try {
URL url = new URL(RPCROUTER);
resp = call.invoke(url, "");
} catch (SOAPException e) {
out.println("Caught SOAPException (" +
e.getFaultCode() + "): " +
e.getMessage());
}
// Check the response
if (!resp.generatedFault()) //Successful response {
Parameter ret = resp.getReturnValue();
Resume value = (Resume) ret.getValue();
if (value != null)
displayResume(out,value);
else
out.println("<font face='Arial Narrow' size='2'>The resumé for uid " + uid + " could not be found.</font><br><br>");
out.println("<font face='Arial Narrow' size='2'>[<a href='../retrieve.html'>Retrieve A Resumé</a>] [<a href='../submit.html'>Submit A Resumé</a>]" +
" [<a href='../index.html'>Home</a>]</font>");
} else { //Unsuccessful response
Fault fault = resp.getFault();
out.println("Generated fault: ");
out.println (" Fault Code = " + fault.getFaultCode());
out.println (" Fault String = " + fault.getFaultString());
}
out.println("</body>");
out.println("</html>");
out.close();
}
/** Handles the HTTP <code>GET</code> method.
* @param request servlet request
* @param response servlet response
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException {
processRequest(request, response);
}
/** Handles the HTTP <code>POST</code> method.
* @param request servlet request
* @param response servlet response
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException {
processRequest(request, response);
}
// Returns a short description of the servlet
public String getServletInfo() {
return "Job Resumé Repository System SubmitServlet";
}
public void displayResume(PrintWriter out, Resume resume) {
out.println("<center><b><font face='Arial Narrow' size='3'>Resume UID: </font>" + resume.getUid() + "<br></b></center><br>");
out.println("<b><font face='Arial Narrow' size='2'>Candidate Contact Information: </font></b><br><br>");
out.println(resume.getFirstName() + " " + resume.getMiddleName() + " " + resume.getLastName() + "<br>\n");
out.println(resume.getAddress() + "<br>\n");
out.println(resume.getCity() + ", " + resume.getState() + " " + resume.getZipcode() + "<br>\n");
out.println("Phone #: " + resume.getPhoneNumber() + "\n </center></font><br>");
out.println("<b><font face='Arial Narrow' size='2'>Education: </font></b><br><pre>" + resume.getEducation() + "</pre><br>");
out.println("<b><font face='Arial Narrow' size='2'>Work History: </font></b><br><pre>" + resume.getWorkHistory() + "</pre><br>");
out.println("<b><font face='Arial Narrow' size='2'>References: </font></b><br><pre>" + resume.getReferences() + "</pre><br>");
}
}
This servlet is very similar to the SubmitServlet
. The main difference between the
servlets is that it invokes the retrieve()
method instead of the submit()
. So
the Call
object would be set up like this:
Call call = new Call();
call.setSOAPMappingRegistry(smr);
call.setTargetObjectURI("urn:JobResuméRepositoryService");
call.setMethodName("retrieve");
call.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);
[previous] [next] |
Created: June 17, 2002
Revised: June 17, 2002
URL: https://webreference.com/programming/java/webservices/chap3/5/5.html