dimanche 20 avril 2014

Java - l'appel de Webservices de jsp - Stack Overflow


I have created the wsdl successfully .Its url is "http://:/aebis/HelpdeskWebserviceImpl?wsdl". Now I want to use this url to call the function in jsp.I am using Jboss as server. Please suggest if any one can help. Thanks in advance.




Here is a 5-minute example using eclipse


I am gonna use this WSDL to demonstrate


http://www.webservicex.net/ConvertAcceleration.asmx?WSDL


Create a dynamic java project for your JSPs


enter image description here


enter image description here


enter image description here


Create your JSP and some backend java class


enter image description here


your JSP


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
</head>
<body>
<%= new myweb.MyClass().getResult() %>
</body>
</html>

and


package myweb;

public class MyClass {

public String getResult(){
return null;
}

public static void main(String[] args) {

MyClass c = new MyClass();
System.out.println(c.getResult());

}

}

Now create the WS client. Click on/select the project


enter image description here


right click and create a new Web Service Client from the given WSDL


enter image description here


enter image description here


Change MyClass to call the web service (you can test first using the class main too)


 package myweb;

import java.rmi.RemoteException;

import NET.webserviceX.www.AccelerationUnitSoap;
import NET.webserviceX.www.AccelerationUnitSoapProxy;
import NET.webserviceX.www.Accelerations;

public class MyClass {

public String getResult() throws RemoteException {
AccelerationUnitSoap a = new AccelerationUnitSoapProxy();
Accelerations x = Accelerations.decimeterPersquaresecond;
Accelerations y = Accelerations.centimeterPersquaresecond;
Object z = a.changeAccelerationUnit(1, x, y);
return z.toString();
}

public static void main(String[] args) throws RemoteException {

MyClass c = new MyClass();
System.out.println(c.getResult());

}

}

Add the web app to your server (if there's one. If there isn't, create a new server)


enter image description here


Clear the server (forcing it to refresh the app) and start it


And here it is.


enter image description here




The wsimport tool is part of the JDK and generates "portable artifacts" from a wsdl. That gives you classes to use easily for communicating with the web service without the need of doing the boilerplate code yourself.


But I feel that you may need some more background beforehand, to get a better understanding how to use JAX-RS web services or implement a state-of-the-art web application with JSF, so my advice is to consult the Java EE 7 tutorial for those two (chapter 28 and 7).



I have created the wsdl successfully .Its url is "http://:/aebis/HelpdeskWebserviceImpl?wsdl". Now I want to use this url to call the function in jsp.I am using Jboss as server. Please suggest if any one can help. Thanks in advance.



Here is a 5-minute example using eclipse


I am gonna use this WSDL to demonstrate


http://www.webservicex.net/ConvertAcceleration.asmx?WSDL


Create a dynamic java project for your JSPs


enter image description here


enter image description here


enter image description here


Create your JSP and some backend java class


enter image description here


your JSP


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
</head>
<body>
<%= new myweb.MyClass().getResult() %>
</body>
</html>

and


package myweb;

public class MyClass {

public String getResult(){
return null;
}

public static void main(String[] args) {

MyClass c = new MyClass();
System.out.println(c.getResult());

}

}

Now create the WS client. Click on/select the project


enter image description here


right click and create a new Web Service Client from the given WSDL


enter image description here


enter image description here


Change MyClass to call the web service (you can test first using the class main too)


 package myweb;

import java.rmi.RemoteException;

import NET.webserviceX.www.AccelerationUnitSoap;
import NET.webserviceX.www.AccelerationUnitSoapProxy;
import NET.webserviceX.www.Accelerations;

public class MyClass {

public String getResult() throws RemoteException {
AccelerationUnitSoap a = new AccelerationUnitSoapProxy();
Accelerations x = Accelerations.decimeterPersquaresecond;
Accelerations y = Accelerations.centimeterPersquaresecond;
Object z = a.changeAccelerationUnit(1, x, y);
return z.toString();
}

public static void main(String[] args) throws RemoteException {

MyClass c = new MyClass();
System.out.println(c.getResult());

}

}

Add the web app to your server (if there's one. If there isn't, create a new server)


enter image description here


Clear the server (forcing it to refresh the app) and start it


And here it is.


enter image description here



The wsimport tool is part of the JDK and generates "portable artifacts" from a wsdl. That gives you classes to use easily for communicating with the web service without the need of doing the boilerplate code yourself.


But I feel that you may need some more background beforehand, to get a better understanding how to use JAX-RS web services or implement a state-of-the-art web application with JSF, so my advice is to consult the Java EE 7 tutorial for those two (chapter 28 and 7).


0 commentaires:

Enregistrer un commentaire