1 Reply Latest reply: Aug 5, 2012 1:28 AM by in the line of fire RSS

    Error when call webservice on servlet

    ltthoi
      Hi All,

      I'm having a problem when calling webservice inside the servlet on the WebLogic environment.

      My code:
      * Webservice:

      package ws;

      import java.sql.Connection;
      import java.sql.DriverManager;
      import java.sql.PreparedStatement;
      import java.sql.ResultSet;
      import java.sql.SQLException;

      import java.util.Date;

      import javax.jws.WebMethod;
      import javax.jws.WebParam;
      import javax.jws.WebService;

      import javax.naming.InitialContext;
      import javax.naming.NamingException;

      import javax.sql.DataSource;

      @WebService
      public class getData {
      public getData() {
      super();
      }

      @WebMethod
      public String getHello() {
      return "HELLO HELLO";
      }

      private Connection getConn() throws NamingException, SQLException {

      InitialContext ic = new InitialContext();

      DataSource ds = (DataSource)ic.lookup("jdbc/hnxwebsite");
      //System.out.println("POOL !!!!");
      Connection con = ds.getConnection();
      con.setAutoCommit(false);

      return con;
      }

      @WebMethod
      public String getIndexData(@WebParam(name="param") String param) {
      StringBuffer sb = new StringBuffer();
      String sql =
      "Select * From idx_index_info iii where iii.index_code= ?";
      Connection con = null;
      PreparedStatement pstmt = null;
      ResultSet rs = null;

      try {

      con = getConn();

      pstmt = con.prepareStatement(sql);
      pstmt.setString(1, param);
      rs = pstmt.executeQuery();

      int columnCount = rs.getMetaData().getColumnCount();
      Object val;
      while (rs.next()) {

      for (int i = 1; i < columnCount; i++) {
      //System.out.println(i);
      val = rs.getObject(i);
      if (val != null)
      sb.append(val.toString() + "\n");
      }

      }
      } catch (SQLException e) {
      e.printStackTrace();
      } catch (NamingException e) {
      e.printStackTrace();
      } finally {
      try {
      if (rs != null)
      rs.close();
      if (pstmt != null)
      pstmt.close();
      if (con != null)
      con.close();
      } catch (Exception e) {
      e.printStackTrace();
      }
      }
      String str = sb.toString();
      int len = str.length();
      //System.out.println("LENGTH >>>>"+len);
      return str;

      }

      public static void main(String[] arg) {
      Date date = new Date();
      System.out.println(date.toGMTString());
      getData gd = new getData();
      System.out.println(date.toGMTString());
      //System.out.println(gd.getIndexData("ACB"));
      }
      }

      * WebClient:
      package hnx;

      import java.util.Date;
      import java.util.Map;

      import javax.xml.namespace.QName;
      import javax.xml.soap.MessageFactory;
      import javax.xml.soap.SOAPBody;
      import javax.xml.soap.SOAPBodyElement;
      import javax.xml.soap.SOAPElement;
      import javax.xml.soap.SOAPMessage;
      import javax.xml.ws.BindingProvider;
      import javax.xml.ws.Dispatch;
      import javax.xml.ws.Service;
      import javax.xml.ws.WebServiceException;
      import javax.xml.ws.soap.SOAPBinding;

      public class wsClient {
      public wsClient() {
      super();
      }

      public String procWeb() {
      String strmsg = null;
      try {
      QName serviceName = new QName("http://192.168.60.18:7001/","getDataService");
      // QName for Port As defined in wsdl.
      QName portName = new QName("http://192.168.60.18:7001/","getDataPort");

      // //Endpoint Address
      String endpointAddress = "http://192.168.60.18:7001/WsIndex/getDataPort?wsdl";
      // Create a dynamic Service instance
      Service service = Service.create(serviceName);
      service.addPort(portName, SOAPBinding.SOAP11HTTP_BINDING,
      endpointAddress);

      // Create a dispatch instance
      Dispatch<SOAPMessage> dispatch = service.createDispatch(portName,
      SOAPMessage.class, Service.Mode.MESSAGE);
      // Use Dispatch as BindingProvider
      BindingProvider bp = (BindingProvider) dispatch;

      // Optionally Configure RequestContext to send SOAPAction HTTP Header

      Map<String, Object> rc = bp.getRequestContext();
      rc.put(BindingProvider.SOAPACTION_USE_PROPERTY, Boolean.TRUE);
      rc.put(BindingProvider.SOAPACTION_URI_PROPERTY, "http://ws/");

      // Obtain a preconfigured SAAJ MessageFactory
      MessageFactory factory = ((SOAPBinding) bp.getBinding())
      .getMessageFactory();

      // Create SOAPMessage Request
      SOAPMessage request = factory.createMessage();

      // Request Body
      SOAPBody body = request.getSOAPBody();

      // Compose the soap:Body payload

      QName payloadName = new QName("http://ws/", "getIndexData","ns1");

      SOAPBodyElement payload = body.addBodyElement(payloadName);
      SOAPElement message = payload.addChildElement( "param");
      message.addTextNode("HNX302");
      request.saveChanges();
      // Invoke the endpoint synchronously
      System.out.println(message);

      SOAPMessage reply = null;

      try { // Invoke Endpoint Operation and read response
      reply = dispatch.invoke(request);
      } catch (WebServiceException wse) {
      wse.printStackTrace();
      }

      // process the reply

      SOAPBody bodyRes = reply.getSOAPBody();
      SOAPBodyElement nextSoapBodyElement = (SOAPBodyElement) bodyRes
      .getChildElements().next();
      SOAPElement soapElement = (SOAPElement) nextSoapBodyElement
      .getChildElements().next();
      strmsg = soapElement.getValue();
      System.out.println("AAA >>"+strmsg);
      } catch (Exception wse) {
      wse.printStackTrace();
      }
      return strmsg;
      }

      public static void main(String[] arg) {
      wsClient ws = new wsClient();
      Date date = new Date();
      System.out.println(date.toGMTString());
      System.out.println(ws.procWeb());
      System.out.println(date.toGMTString());

      }

      }

      * Servlet:

      package hnx;

      import java.io.IOException;
      import java.io.PrintWriter;

      import javax.servlet.*;
      import javax.servlet.http.*;

      import ws.GetData;
      import ws.GetDataPortClient;
      import ws.GetDataService;

      public class getIndex extends HttpServlet {
      private static final String CONTENT_TYPE = "text/html; charset=UTF-8";

      public void init(ServletConfig config) throws ServletException {
      super.init(config);
      }

      public void doPost(HttpServletRequest request,
      HttpServletResponse response) throws ServletException,
      IOException {
      doGet(request, response);
      }
      public void doGet(HttpServletRequest request,
      HttpServletResponse response) throws ServletException,
      IOException {
      response.reset();
      response.flushBuffer();
      response.setContentType(CONTENT_TYPE);
      PrintWriter out = response.getWriter();
      // GetDataService service = new GetDataService();
      // GetData getData = service.getGetDataPort();
      // String str = getData.getIndexData("HNX302");
      // int length = str.length();
      // response.setContentLength(length);
      wsClient ws = new wsClient();
      String str = ws.procWeb();
      try {
      out.println(str);
      } catch (Exception e) {
      e.printStackTrace();
      }
      out.close();
      }
      }
      ERROR: >>>>>
      [Another instance of the application is running on the server. JDeveloper redeploy the application.]
      [Application SClient stopped but not undeployed from Server Instance IntegratedWebLogicServer]
      [Running application SClient on Server Instance IntegratedWebLogicServer...]
      [03:41:48 PM] ---- Deployment started. ----
      [03:41:48 PM] Target platform is (Weblogic 10.3).
      [03:41:48 PM] Retrieving existing application information
      [03:41:48 PM] Running dependency analysis...
      [03:41:48 PM] Deploying 2 profiles...
      [03:41:48 PM] Wrote Web Application Module to C:\Users\W7\AppData\Roaming\JDeveloper\system11.1.1.2.36.55.36\o.j2ee\drs\SClient\sgetDataWebApp.war
      [03:41:48 PM] Wrote Enterprise Application Module to C:\Users\W7\AppData\Roaming\JDeveloper\system11.1.1.2.36.55.36\o.j2ee\drs\SClient
      [03:41:49 PM] Redeploying Application...
      [03:41:49 PM] Application Redeployed Successfully.
      [03:41:49 PM] The following URL context root(s) were defined and can be used as a starting point to test your application:
      [03:41:49 PM] http://192.168.9.100:7101/SClient
      [03:41:49 PM] Elapsed time for deployment: 1 second
      [03:41:49 PM] ---- Deployment finished. ----
      Run startup time: 1380 ms.
      [Application SClient deployed to Server Instance IntegratedWebLogicServer]

      Target URL -- http://127.0.0.1:7101/SClient/getindex
      [param: null]
      javax.xml.ws.WebServiceException: com.ctc.wstx.exc.WstxIOException: Exceeding stated content length of 228
      at com.sun.xml.ws.encoding.StreamSOAPCodec.encode(StreamSOAPCodec.java:117)
      at com.sun.xml.ws.encoding.SOAPBindingCodec.encode(SOAPBindingCodec.java:258)
      at com.sun.xml.ws.transport.http.client.HttpTransportPipe.process(HttpTransportPipe.java:165)
      at com.sun.xml.ws.transport.http.client.HttpTransportPipe.processRequest(HttpTransportPipe.java:101)
      at com.sun.xml.ws.api.pipe.Fiber.__doRun(Fiber.java:604)
      at com.sun.xml.ws.api.pipe.Fiber._doRun(Fiber.java:563)
      at com.sun.xml.ws.api.pipe.Fiber.doRun(Fiber.java:548)
      at com.sun.xml.ws.api.pipe.Fiber.runSync(Fiber.java:445)
      at com.sun.xml.ws.client.Stub.process(Stub.java:248)
      at com.sun.xml.ws.client.dispatch.DispatchImpl.doInvoke(DispatchImpl.java:189)
      at com.sun.xml.ws.client.dispatch.DispatchImpl.invoke(DispatchImpl.java:215)
      at hnx.wsClient.procWeb(wsClient.java:75)
      at hnx.getIndex.doGet(getIndex.java:40)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
      at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:227)
      at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:125)
      at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:292)
      at weblogic.servlet.internal.TailFilter.doFilter(TailFilter.java:26)
      at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:56)
      at oracle.dms.wls.DMSServletFilter.doFilter(DMSServletFilter.java:326)
      at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:56)
      at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3592)
      at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
      at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:121)
      at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2202)
      at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:2108)
      at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1432)
      at weblogic.work.ExecuteThread.execute(ExecuteThread.java:201)
      at weblogic.work.ExecuteThread.run(ExecuteThread.java:173)
      Caused by: com.ctc.wstx.exc.WstxIOException: Exceeding stated content length of 228
      at com.ctc.wstx.sw.BaseStreamWriter.finishDocument(BaseStreamWriter.java:1687)
      at com.ctc.wstx.sw.BaseStreamWriter.writeEndDocument(BaseStreamWriter.java:585)
      at com.sun.xml.ws.message.saaj.SAAJMessage.writeTo(SAAJMessage.java:396)
      at com.sun.xml.ws.encoding.StreamSOAPCodec.encode(StreamSOAPCodec.java:114)
      ... 29 more
      Caused by: java.net.ProtocolException: Exceeding stated content length of 228
      at weblogic.net.http.ContentLengthOutputStream.write(ContentLengthOutputStream.java:39)
      at com.ctc.wstx.io.UTF8Writer.flush(UTF8Writer.java:96)
      at com.ctc.wstx.sw.BufferingXmlWriter.flush(BufferingXmlWriter.java:214)
      at com.ctc.wstx.sw.BufferingXmlWriter.close(BufferingXmlWriter.java:194)
      at com.ctc.wstx.sw.BaseStreamWriter.finishDocument(BaseStreamWriter.java:1685)
      ... 32 more
      java.lang.NullPointerException
      at hnx.wsClient.procWeb(wsClient.java:82)
      at hnx.getIndex.doGet(getIndex.java:40)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
      at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:227)
      at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:125)
      at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:292)
      at weblogic.servlet.internal.TailFilter.doFilter(TailFilter.java:26)
      at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:56)
      at oracle.dms.wls.DMSServletFilter.doFilter(DMSServletFilter.java:326)
      at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:56)
      at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3592)
      at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
      at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:121)
      at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2202)
      at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:2108)
      at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1432)
      at weblogic.work.ExecuteThread.execute(ExecuteThread.java:201)
      at weblogic.work.ExecuteThread.run(ExecuteThread.java:173)

      Can anyone help?

      Thanks in advance.
      LTThoi