Solved

Webservice reference in servlet fails with "cannot be resolved to a type"

Posted on 2014-09-18
8
173 Views
Last Modified: 2014-09-24
I am trying to create a servlet (tomcat) that accesses a database, then a USDA web service. I've successfully deployed/tested the database connectivity. When I added the web service access, eclipse reports the problem: AwdbWebService_Service cannot be resolved to a type. I think I am referencing the service, not a type--anyway, it won't resolve as a service instance with whatever I am doing incorrectly. I generated the AwdbWebService package from a wsimport from the wsdl @wcc.nrcs.usda.gov/awdbWebService/services?WSDL . Please advise.

The error is tripped by this line (#74):

AwdbWebService_Service lookup = new AwdbWebService_Service(wsURL,new QName("http://www.wcc.nrcs.usda.gov/ns/awdbWebService","AwdbWebService"));

 package localdomain.localhost;

 import java.io.IOException;
 import java.io.PrintWriter;
 import java.math.BigDecimal;
 import java.net.URL; //added for usda webservice

 import java.sql.Connection;
 import java.sql.ResultSet;
 import java.sql.Statement;
 import java.text.SimpleDateFormat;
 import java.util.*;
 import java.util.logging.Logger;

 import javax.annotation.Resource;
 import javax.servlet.ServletConfig;
 import javax.servlet.ServletException;
 import javax.servlet.annotation.WebServlet;
 import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import javax.sql.DataSource;

 import javax.xml.namespace.QName;       // added for usda webservice
 import usda.nrcs.wcc.awdbWebService.*;

 @WebServlet(value = "/MyServlet")
 public class MyServlet extends HttpServlet {

   // use this for usda reservoir station values later
   static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

   protected final Logger logger = Logger.getLogger(getClass().getName());
   @Resource(name = "jdbc/mydb", lookup = "jdbc/mydb")
   private DataSource dataSource;



   @Override
   public void init(ServletConfig config) throws ServletException {
      super.init(config);
      logger.info("Init");
      System.out.println(getClass().getName() + ".init");
  }



  protected void service(HttpServletRequest req, HttpServletResponse resp) throws  ServletException, IOException 
{
    PrintWriter writer = resp.getWriter();

    writer.println("<html>");
    writer.println("<head><title>MyServlet</title></head>");
    writer.println("<body><h1>MyServlet</h1>");

    writer.println("<h2>DataSource</h2>");
    Connection conn = null;
    try {
        writer.println("Datasource: " + dataSource + "<br/><br/>");
        conn = dataSource.getConnection();
        Statement stmt = conn.createStatement();
        ResultSet rst = stmt.executeQuery("select 1");
        while (rst.next()) {
            writer.println("Resultset result: " + rst.getString(1) + "<br/><br/>");
        }
        rst.close();
        stmt.close();
        conn.close();
        writer.println("SUCCESS to access the datasource");

        // Now try accessing usda
        URL wsURL = new URL("http://www.wcc.nrcs.usda.gov/awdbWebService/services?wsdl");

        AwdbWebService_Service lookup = new AwdbWebService_Service(wsURL,new QName("http://www.wcc.nrcs.usda.gov/ns/awdbWebService","AwdbWebService"));
        m_webService = lookup.getAwdbWebServiceImplPort();


    } catch (Exception e) {
        e.printStackTrace(writer);
        e.printStackTrace();
    } finally {
        if (conn != null) {
            try {
                conn.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    writer.println("</body></html>");

}

Open in new window

0
Comment
Question by:ditallop
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 40330684
import usda.nrcs.wcc.awdbWebService.*;

Open in new window

Try using the IDE to dig into that to get the type that it's telling you is missing. I suspect you won't be able to, so , if so, there's something wrong with the way you're pointing at the generated classes.
0
 

Author Comment

by:ditallop
ID: 40330784
It thinks awdbwebservice is a type -- the wizard keeps popping up for me to define a new class. I've got plenty of those under the imported package! :) The import usda.nrcs.wcc.awdbWebService.* package has about 2 dozen method classes.  I just rebuilt it all again from the wsdl--on c:\  to keep it pure.  The full path it generated:
c:\gov\usda\nrcs\wcc\awdbWebService.  I removed the old package location from the project, this time pointing to the one I just created. This time the import line looks like:  import gov.usda.nrcs.wcc.awdbWebService.*;  Same maddening results.

Here's the import statement:
wsimport -p gov.usda.nrcs.wcc.awdbWebService -s c:\ http://www.wcc.nrcs.usda.gov/awdbWebService/services?WSDL
0
 

Author Comment

by:ditallop
ID: 40330798
cehj,
to answer your direct suggestion, there isn't any additional info as to what type it thinks it needs to resolve to. just that it thinks i'm referencing one.
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 86

Expert Comment

by:CEHJ
ID: 40330863
Don't import .* Import each type you use
0
 

Author Comment

by:ditallop
ID: 40330918
alright, I'll try that.
0
 

Assisted Solution

by:ditallop
ditallop earned 0 total points
ID: 40334115
@CEHJ,
You are correct about the import. Thanks so much for your help ! :)

For those following this thread:

The package statement at the start of the java classes I generated with wsimport begins with:

package gov.usda.nrcs.wcc.awdbWebService

My import statement however, looked like this: import usda.nrcs.wcc.awdbWebService.*;

In essence, I placed the source @ the wrong level and defined the build config to point incorrectly for the package references defined in the java classes. I removed the build reference, then moved the tree to begin pointing on the gov level. Now that there wasn't a mismatch, the unresolved type error vanished.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 40334260
Good. Furthermore, as i hinted above, its bad practice to import .* All classes should be named explicitly. If you're using an IDE, there's no excuse, as something like 'Fix imports' will do that for you automatically
0
 

Author Closing Comment

by:ditallop
ID: 40341137
While CEHJ identified the import as a problem, the details I've outlined in exactly why would be helpful to anyone following this thread with a similar issue.
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question