aspose file tools*
The moose likes Web Services and the fly likes JSP to connect to a web service Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Web Services
Bookmark "JSP to connect to a web service" Watch "JSP to connect to a web service" New topic
Author

JSP to connect to a web service

Michelle Streeter
Ranch Hand

Joined: Sep 22, 2011
Posts: 48

I created a web service using Glassfish v3 and using Eclipse Helios. When I want to develope a front end using Flash Builder, I told it where the WSDL file was and it automatically created the service connection for me. It all worked great.

I then wanted to learn to use JSP so I can learn how to really connect to the web service. I bought a book on JSP however it only teaches how to connect to the database directly and it teaches how to use java beans. What it did not teach is how to connect to a web service. A EJB.

Now, when I created the EJB web service, the tutorial which taught me this did teach how to connect to the EJB but this only worked as long as the service was on the same server. What I am trying to do is connect to the EJB service from a diff server. Lets say my JSP is on localhost for now and my web service is on exthost. And lets say my wsdl is located at http"//exthost:8080:/WorkBeanService/WorkBean?wsdl which basically tells you the service is at http"//exthost:8080:/WorkBeanService/WorkBean and it contains a function called count().

So how do I connect to this ejb service and call the function in a JSP or Servlet?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60057
    
  65

No JSP book is going to tell you how to connect to a web service -- that's not really a job for a JSP.

If you want to learn JSP, I'd find a more suitable use for JSPs; connecting to a web service isn't one of them.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Michelle Streeter
Ranch Hand

Joined: Sep 22, 2011
Posts: 48

Really? Java server pages is not for server data? What is the purpose of JSP if not to display java bean backend data? Or is this the purpose of a Servlet? And if so, take notice I had jsp or servlet in my question. Surely there is something for front ends that does not rely on frameworks. Which I was told is what JSP is for.
Michelle Streeter
Ranch Hand

Joined: Sep 22, 2011
Posts: 48

Tell you what, let me rephrase my question. I read somewhere where Bill Gates claims the direction he wants the internet to go is developers create web services. And then the consumer can consume the service. Now as I understand this, there could be two approaches to this. One would be that the consumer can only consume my service using my front end. But I am not so sure that was the point. I think the point was and what I think the purpose of the WSDL file is that if done right, anyone can create whatever front end they wish to consume your web service back end. And so if I am going to offer my web service back end to people to consume, I need to know how to create a front end which will be on a separate server.

Now, I have accomplished this using flash builder. But Flash builder did most of the work for me. I am now trying to learn how to do it for myself. If JSP is not the answer, then maybe servlets. If not servlets then what? I am trying to stay away from frameworkds because they can be buggy and my front end is dependant on how well they developed their front end. Now, reread my first question.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60057
    
  65

JSPs are for display -- all other work should be done in Java classes. While you could consume web services in servlets, in a properly structured web app, that would delegated to an even lower layer (the Model) that hides the details from the presentation layer. But at the very least, this is not a job for a JSP.

Remember that in modern JSPs, there should be no Java code -- that's a practice that's been discredited for almost 10 years now. All "heavy lifting" (such as consuming web services" should be done in servlet controllers or their delegates.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60057
    
  65

I always recommend that novices to JSP read the following 2 articles:
  • The Secret Life of JSPs
  • The Front Man

  • They should be helpful in understanding JSP and where it fits into the web app ecosystem.
    Paul Clapham
    Bartender

    Joined: Oct 14, 2005
    Posts: 18135
        
        8

    Frankly, you're going about this decision process backwards. Your first step should be to get some service-consuming code running in a plain old Java application. Just get the code written and make sure it runs. Then if you want to put it in a servlet or a Java bean or even (heaven forbid) a scriptlet in a JSP, you can do that. Those are all places where you can put Java code, so trying to decide in advance which of them you are going to use is pointless.

    And yeah, there are Java tools which do the work of generating that code for you, just like there were in Flash. But again those tools don't care where you plan to run that code, so you won't find out about them if you're looking for things related to web applications.
    Michelle Streeter
    Ranch Hand

    Joined: Sep 22, 2011
    Posts: 48

    Okay, now I am confused. I just read the first article about jsp's and it seems it is exactly what I want. But you sent me that link to tell me its not what I want so....... Huh?
    Michelle Streeter
    Ranch Hand

    Joined: Sep 22, 2011
    Posts: 48

    Paul
    If you read my first question, you will know I already have a web service working fine. It originally started when I was working a tutorial and we first created the web service and had it attached to a database. Which I tweaked to work with my database and my data. In the tutorial, at first, we used SOAPUI to test the web service to see if the data we wanted was working. Then we created a servlet to display the data on the front end. However, in the tutorial, the servlet resided on the same server as the service. When I finished the tutorial, I had a working web service with a pretty basic front end. I then asked the tutorial owner what front end designer did he recommend and at first he recommended Flash Builder. So I purchased flash builder and like I stated earlier, flash builder asked me where the wsdl file was and it created the interface to work with my service. I then created some really good apps using my service backend. great. But then I started running into bugs flash builder had and so I again asked the tutorial guy what front end did he recommend if I were not to use a framework. Even JSF is a framework. He suggested JSP. So I purchased a book on JSP but the book did not address EJB Only JB. hence why I am asking how to consume WDSL files to create non framework font ends with. Be it JSP, Servlet, or whatever. I am posing the question with the service being on a separate server so I can somewhat accomplish what flash builder did. So if someone would point me in the right direction rather than just telling me I am going in the wrong, it would be great.
    Paul Clapham
    Bartender

    Joined: Oct 14, 2005
    Posts: 18135
        
        8

    Okay. So I read all of those posts three times and I still don't know whether you have Java code which can act as a web service client. So do you?

    When I had to have my web application connect to a web service and send it requests, here's what I did:

    (1) Produce Java code which did that
    (2) Insert said Java code into one of the servlets

    I'm just totally baffled by all this talk of "non framework front ends"... as far as I'm concerned you're fishing in the wrong pond. Why do you think you need a "front end designer"?
    Michelle Streeter
    Ranch Hand

    Joined: Sep 22, 2011
    Posts: 48

    As stated before, yes I do have a front end servlet that does display the service backend data. But it is very basic. And it resides on the same server and refers to the service directly without a url.

    I also have a Flash Builder front end that does consume the service and it does this from any server I wish. I does not require to be on the same server.

    And so I decided I wanted to design a better front end without a framework like Flash Builder but do the things Flash builder did. In essense create a servlet or JSP page that works from any server and not just the same one my service is on.

    Now, based upon Bears comment, I think he is suggesting I keep using a servlet to access my service. Which is fine. Its just I need to learn how. Funny thing, after I read the second article that Bear sent me, it gave an example of how to connect to a service using JSP. Which I found to be ironical. (Sortof quoting how Robin Williams did it in Good Will Hunting)

    Since I am only speaking from my perspective, I have no idea how you are coming up with me saying the things you, Paul think I am saying. I have said it over and over again how I got here and that I do have a servlet working but only on the same server. Would like something similar which runs on a diff server. Kinda how flash builder did it.
    Paul Clapham
    Bartender

    Joined: Oct 14, 2005
    Posts: 18135
        
        8

    If you look at my last post the only thing I did was to quote exactly what you posted. So I don't believe you can accuse me of misinterpreting what you said. It's true that I don't understand what you are trying to ask, but that's a separate issue.

    So would it be fair to say that you do indeed have Java code that acts as a client to a web service? I asked you this already, but you haven't answered. It isn't fair to accuse me of failing to understand you if you don't answer questions that I ask, don't you agree?

    Michelle Streeter
    Ranch Hand

    Joined: Sep 22, 2011
    Posts: 48

    Paul, I think I figured out what is stumping you

    I did a search on JSP and WSDL and the result of the search put me on this site in this forum. And so I posted my question here because of the that search. So I think you maybe saying you think my question belongs somewhere else rather than in the web services section of this site. Which might be true. But I posted a similar question before in the JSP section and nobody responded.

    Michelle Streeter
    Ranch Hand

    Joined: Sep 22, 2011
    Posts: 48

    Michelle Streeter wrote:As stated before, yes I do have a front end servlet that does display the service backend data. But it is very basic. And it resides on the same server and refers to the service directly without a url.

    I also have a Flash Builder front end that does consume the service and it does this from any server I wish. I does not require to be on the same server.



    Paul Clapham wrote:So would it be fair to say that you do indeed have Java code that acts as a client to a web service? I asked you this already, but you haven't answered. It isn't fair to accuse me of failing to understand you if you don't answer questions that I ask, don't you agree?



    Exactly how is this not answering your question.
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 60057
        
      65

    Did you read both articles? The first explains what JSP is -- and it should be plain the doing anything but displaying data is a misuse of the technology. Bit the second should have driven home that there should be no Java code in a JSP. Ever.

    Therefore, consuming web services in a JSP is not a possibility. Now, displaying the results of fetching data elsewhere (controller, bean, model, etc) is a perfect use of a JSP. But as Paul pointed out, the Java code to consume the web service should be independent of where you want to use it.
    Michelle Streeter
    Ranch Hand

    Joined: Sep 22, 2011
    Posts: 48

    The only other way I think I can answer your question is to say that I created an EJB using Eclipse which did create an EJBClient. The EJB in the EJB project implements the EJBClient Interface. The EJB is using JPA to interface with my database and I have a conversion class which converts Entities to dto's for the Client side. All this is on the in a project that is in an EAR which has been deployed to a production server. I have also saved both the EJB and the EJBClient to a jar so I can import it.

    When I created the flashbuilder version it used the wsdl to recreate the dto objects and interface with the service

    I now have a new session in Eclipse which does not have the service code in it. I do however have the jar files and I am trying to get some kind of front end to work with the service like Flashbuilder did so if I ever want to create a front end for a service I did not create I can.

    Hopefully this is more what you were asking for. Oh and btw, the side says it is a friendly place for greenhorns and so if I am not able to frame the question as you wish just remember this is suppose to be a friendly place for those of us who do not know how to frame the question better.
    Michelle Streeter
    Ranch Hand

    Joined: Sep 22, 2011
    Posts: 48

    Bear,
    Yes, I think I got the message. Do you suggest then that I use servlets? Just servets unlike the one in my tutorial that will connect to a different server using a url rather than directly from within itself.
    Michelle Streeter
    Ranch Hand

    Joined: Sep 22, 2011
    Posts: 48

    To be even more specific here is the tutorial I used

    http://programming.manessinger.com/tutorials/an-eclipse-glassfish-java-ee-6-tutorial/

    Paul Clapham
    Bartender

    Joined: Oct 14, 2005
    Posts: 18135
        
        8

    Michelle Streeter wrote:
    Michelle Streeter wrote:As stated before, yes I do have a front end servlet that does display the service backend data. But it is very basic. And it resides on the same server and refers to the service directly without a url.

    I also have a Flash Builder front end that does consume the service and it does this from any server I wish. I does not require to be on the same server.



    Paul Clapham wrote:So would it be fair to say that you do indeed have Java code that acts as a client to a web service? I asked you this already, but you haven't answered. It isn't fair to accuse me of failing to understand you if you don't answer questions that I ask, don't you agree?



    Exactly how is this not answering your question.


    Well, the problem is that it doesn't use the word "Yes" or the word "No". Either of those would be a satisfactory answer. As it is, it sounds like the answer is "Yes", but you already complained about me misquoting you, so I'm not sure where to go from here.

    Anyway it seems like I'm in over my head here, since I haven't ever had to use large-scale frameworks to consume a web service. As I said, I just used Axis to generate the client and then put the generated code in the place I needed to run it. But you don't seem to be going down that road. So let me just say that to get from something which connects only to your own server, to something which connects to some other server, shouldn't require a major tooling change. At least I wouldn't think so. Seems to me you should be able to redo whatever you did before, only changing some of the parameters. Of course I could be wrong, you might have chosen an unsuitable tool which can't be changed in that way, but I have no way of telling that.
    Michelle Streeter
    Ranch Hand

    Joined: Sep 22, 2011
    Posts: 48

    Paul Clapham wrote:
    Michelle Streeter wrote:
    Michelle Streeter wrote:As stated before, yes I do have a front end servlet that does display the service backend data. But it is very basic. And it resides on the same server and refers to the service directly without a url.

    I also have a Flash Builder front end that does consume the service and it does this from any server I wish. I does not require to be on the same server.



    Paul Clapham wrote:So would it be fair to say that you do indeed have Java code that acts as a client to a web service? I asked you this already, but you haven't answered. It isn't fair to accuse me of failing to understand you if you don't answer questions that I ask, don't you agree?



    Exactly how is this not answering your question.


    Well, the problem is that it doesn't use the word "Yes" or the word "No". Either of those would be a satisfactory answer. As it is, it sounds like the answer is "Yes", but you already complained about me misquoting you, so I'm not sure where to go from here.

    Anyway it seems like I'm in over my head here, since I haven't ever had to use large-scale frameworks to consume a web service. As I said, I just used Axis to generate the client and then put the generated code in the place I needed to run it. But you don't seem to be going down that road. So let me just say that to get from something which connects only to your own server, to something which connects to some other server, shouldn't require a major tooling change. At least I wouldn't think so. Seems to me you should be able to redo whatever you did before, only changing some of the parameters. Of course I could be wrong, you might have chosen an unsuitable tool which can't be changed in that way, but I have no way of telling that.


    Read the fourth word in. I believe it is the word YES. And it has been there all along. But just for you.

    YES YES YES YES YES I have a client. A working Client


    YES, I have a working front end that implements that client and even on a diff server. But that one was created for me. Now I would like to create my own. I do believe this is over your head though. Good luck.
    Jimmy Clark
    Ranch Hand

    Joined: Apr 16, 2008
    Posts: 2187
    Below is a simple strategy to display data that was retrieved via web service from a Java Server Page.

    1. Create a web service to connect to a Java application, e.g. application that connects to RDBMS

    2. Create a plain Java class that connects to the web service. Make sure to include a main method for testing. This is your web service object.

    3. Create another plain Java class that contains any business logic and any data access logic such as using the data-oriented web service.

    4. Use an object from the class created in step #3 above from within a Java servlet and create a data object that can be displayed in Java Server Page

    5. Create Java Server Page using data object created in step #4 above.


    JSP1 ----> Servlet1 -----> Business Object1 ------> Web Service Object -------> RDBMS

    .NET1 ------> Web Service Object -------> RDBMS

    C++ ------> Web Service Object -------> RDBMS

    Swing GUI -----> Business Object2 ------> Web Service Object -------> RDBMS



    Paul Clapham
    Bartender

    Joined: Oct 14, 2005
    Posts: 18135
        
        8

    Yup, you're right. I avoid big-framework solutions because I like to keep things simple. That's why I suggested you should do the same. However it's your right to look for such a solution if you want to go that way; perhaps you'll find somebody else here who can put you on the right path.
    Michelle Streeter
    Ranch Hand

    Joined: Sep 22, 2011
    Posts: 48

    This solution I already have and works great as long as everything resides on the same server. Where it goes wrong is when you have your web service on one server and your front end implementation on another server. Now, you have to access your web service via a url of some sorts. And that is what I am trying to learn. how I discovered you can do this was when FlashBuilder did it. I do not know how exactly it did it but I know it did it using the wsdl file. I am trying to get away from big frameworks which is why I went back to JSP pages. But even in this thread Bear says JSP is not the way to go which I guess he means using Servlets. Which is fine. I only wish I could find something that teaches me how to do it. I purchased a book thinking it would teach me and it didnt. I have spent two days searching the web and still not found the answer. I have asked in this thread and instead of how, I got a debate on how I asked the question. What Jim stated, I already have and does not work on separate servers. Though I do believe its close, only some minor tweaks I am sure. But discovering those tweaks has been a pain. I really wish I knew how Flashbuilder did it. They didnt have my server code and yet they were able to build the connection and it worked on a separate server. But they just did it, they didnt teach us how they did it.
    Michelle Streeter
    Ranch Hand

    Joined: Sep 22, 2011
    Posts: 48

    Hey Paul answer this

    I have a web service I create. Its an EJB service. It is a customer management service. Now you are writing an app that does lets say inventory. Rather than rewrite a customer aspect of your app, you deside to implement my customer management service so the only part you have to write is your inventory part. Saves you a ton of time and money. My customer management service resides on my server. I am not giving out my source code. You have access to my WSDL files and I if needed, I may even provide compiled jar files. Now how do you add my customer management service to your inventory program? Your program will reside on your server, not mine. How do you do it?
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 60057
        
      65

    You keep focusing on JSP and servlets, but that's all just a red herring. You need to figure out how to do this with Java. Wether that Java is in a servlet or not is completely moot. Completely. (And since there should never be any Java code in a JSP, it's completely off the table.)

    Write a Java class with methods that can consume your web service. Test them from a command-line program. And then you'll be ready to call them from a servlet or something else.

    You should either be focusing on the java.net classes, or looking into a 3rd-party library such as HttpClient.
    Michelle Streeter
    Ranch Hand

    Joined: Sep 22, 2011
    Posts: 48

    Bear, I am trying to create something to use SOAP to communicate with my service. That is why I have been focusing on Servlets. Hence why I keep referring to the WSDL file. As far as I know, there are only three ways to talk to a service via an external server. The WSDL and SOAP, HTTP, and REST. SOAP uses XML. I am not sure what HTTP uses. And REST somehow uses the URL and returns something, either html or xml. If there are others, I do not know about them. So with that said, what am I missing?
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 60057
        
      65

    You are missing that you should not be focusing on servlets. They are completely orthogonal to the issue. You should be looking up information on how to consume SOAP services.
    Michelle Streeter
    Ranch Hand

    Joined: Sep 22, 2011
    Posts: 48

    Well, so far I have seen recommendations using JAX-WB, CXF, use wsdl2java to create java classes, SAX and stAX. I am sure there might be more. But since the answers I have seen are on web sites like this one, you want to make life easier and give you suggestions and why
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 60057
        
      65

    Sorry but I don't have any pointers on consuming SOAP services, I'm a writer and consumer of RESTful web services. (I personally wouldn't touch SOAP with a 10-foot pole.)

    A google search on "consuming SOAP web services with java" seemed to bring up some likely resources.
    Michelle Streeter
    Ranch Hand

    Joined: Sep 22, 2011
    Posts: 48

    Why Rest instead of SOAP? It seems to me that REST would be easy to attack since how you request info is via a URL. Unless my understanding or REST is incorrect
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 60057
        
      65

    REST is no less secure than any other web service technology. Written correctly, of course.

    It's a common misconception that information passed in the body of the request is more secure than information on the URL. Both are as vulnerable to attacks as the other.
    Michelle Streeter
    Ranch Hand

    Joined: Sep 22, 2011
    Posts: 48

    So then why not soap
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 60057
        
      65

    In my opinion, it's an old-fashioned, over-complicated, cranky dinosaur that's not in keeping with the intended use of HTTP.

    There are plenty of resources on the Web that discuss RESTful services vs. SOAP and other approaches. If this is a topic that interests you, I recommend following up with them. I'd start with this article.
    Michelle Streeter
    Ranch Hand

    Joined: Sep 22, 2011
    Posts: 48

    I am doing a tutorial on using JAX-WS which used Eclipse

    http://java.dzone.com/articles/jax-ws-hello-world

    I am at the part where I ran wsimport and it generated six java classes. However, in one of the classes it generated, it already has errors.

    Description Resource Path Location Type
    The constructor Service(URL, QName, WebServiceFeature[]) is undefined GreetingImpService.java /WS-Client/src/com/lmco/greeting/service line 46 Java Problem

    Did I do something wrong?
    Michelle Streeter
    Ranch Hand

    Joined: Sep 22, 2011
    Posts: 48

    Here is the bummer part of most tutorials. It turns out the javax.xml.ws.Service does not have a constructor for the errors. So I deleted the constructors which were created and then ran the client and it failed. Gotta love it.
    Michelle Streeter
    Ranch Hand

    Joined: Sep 22, 2011
    Posts: 48

    The real answer to my original question, though not well worded because I didnt know how to word it better, is there are tools like wsimport that read the wsdl file and generate the service for you so you can test while creating whatever front end you want. And so in the end, the project would look very similar if you just reuse the service one you created or if you had imported it from the wsdl.

    I wonder if there are better tools than wsimport? Apparently Flashbuilder is using or designed a better one.

    btw Bear,
    I went back to the original tutorial and tried to implement the RESTful part of the tutorial but the tutorial did not follow through and so I will have to figure it out some other way. But here is how it started:

    http://programming.manessinger.com/tutorials/an-eclipse-glassfish-java-ee-6-tutorial/
    Michelle Streeter
    Ranch Hand

    Joined: Sep 22, 2011
    Posts: 48

    Just to complete this thread, sortof. I saw this tutorial about REST

    http://javaeenotes.blogspot.com/2011/02/rest-using-jax-rs.html

    after this tutorial, I figured out how the url's worked for REST and so now I got the previous J2EE Restful tutorial to work as well.

    Now, I need to figure out how to consume the xml which is returned from the RESTful urls.
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 60057
        
      65

    Be aware that XML isn't the only possibility. I'm a much bigger fan of JSON.
    Jimmy Clark
    Ranch Hand

    Joined: Apr 16, 2008
    Posts: 2187
    is there are tools like wsimport that read the wsdl file and generate the service for you so you can test while creating whatever front end you want.


    There are different types of usages for "web services." Some may facilitate access to data. Some may facilitate access to some sort of business logic. In both cases, they are integration components, not implementation. They facilitate communication between applications. An application or component implements the functionality that the web service connects to, i.e. the web service is there for access, i.e. integration.

    In terms of your statement above, a WSDL-based file describes a web service at a very high-level and is limited. There are no tools that will read a WSDL file and generate the code for a web service. At best, you will get a skeleton based on what is provided in the WSDL file. A programmer will always have to develop the code further.

     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: JSP to connect to a web service
     
    Similar Threads
    EJB 3.0 WebService Problems
    Webservice client code generation using axis2 and eclipse galilio
    Problem when create client for web services because of XSD import url
    web service deployment on shared hosting tomcat server
    Facing Problems Running My First WebService