WebReference.com - Part 5 of chapter 3 from Professional Java Web Services, Wrox Press Ltd. (2/6) | WebReference

WebReference.com - Part 5 of chapter 3 from Professional Java Web Services, Wrox Press Ltd. (2/6)

To page 1current pageTo page 3To page 4To page 5To page 6
[previous] [next]

Professional Java Web Services

SubmitServlet

The SubmitServlet class is located in SubmitServlet.java. The class is listed below:

package com.wrox.jobresume.client.submit;
// 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.Resume;
public class SubmitServlet extends HttpServlet {
  /** The URL where the rpcrouter servlet is located.  It's the interface
   * into the Apache SOAP engine that is responsible for routing web service
   * requests to the corresponding service implementation.
   */
  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>");
    /** Create a Resumé object and populate it with the resumé information
     *  provided on the form
     */ 
    Resume resume = new Resume();
    resume.setFirstName(request.getParameter("firstName"));
    resume.setMiddleName(request.getParameter("middleName"));
    resume.setLastName(request.getParameter("lastName"));
    resume.setAddress(request.getParameter("address"));
    resume.setCity(request.getParameter("city"));
    resume.setState(request.getParameter("state"));
    resume.setZipcode(request.getParameter("zipcode"));
    resume.setPhoneNumber(request.getParameter("phoneNumber"));
    resume.setEducation(request.getParameter("education"));
    resume.setWorkHistory(request.getParameter("workHistory"));
    resume.setReferences(request.getParameter("references"));
    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("submit");  
    call.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);
    // Add the parameters
    Vector params = new Vector();
    params.addElement(new Parameter("resume", Resume.class, resume, 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();
      String value = (String) ret.getValue();
      if (value != null) {
        out.println(
    "<font face='Arial Narrow' size='2'>The resumé was submitted successfully!" +
    "The unique identifier(UID) generated by the system is <b>" + value +
    "</b></font><br>");
      } else {
        out.println(
    "<font face='Arial Narrow' size='2'>The resumé was not submitted successfully
    :-(</font><br>");
        out.println(
    "<font face='Arial Narrow' size='2'>&nbsp[<a href='../servlet/retrieve?uid="
    + value + "'>View Resume</a>]" +
    "[<a href='../submit.html'>Submit Another Resumé</a>]&nbsp[
      <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";
  }
}

All of the magic happens in the processRequest() method, which is called if the request is either a GET or POST.


To page 1current pageTo page 3To page 4To page 5To page 6
[previous] [next]

Created: June 17, 2002
Revised: June 17, 2002

URL: https://webreference.com/programming/java/webservices/chap3/5/2.html