I have created this method of the WebService
Service.java
package Servicios;
import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.WebParam;
@WebService(serviceName = "Service")
public class Service {
/**
* This is a sample web service operation
*/
@WebMethod(operationName = "hello")
public String hello(@WebParam(name = "name") String txt) {
return "Hello " + txt + " !";
}
}
I have also created a webServiceClient to consume it
and I have this index
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="jsp/actionLogin.jsp" method="post">
<input value="Hello" name="Hello"/>
<input value="Luisa" name="Luisa"/>
<input type="submit"/>
</form>
</body>
</html>
When adding a Web Service Client I generate these classes
And I have a JSP where I try to consume the WebService method of the generated classes
In this case it would be this method that I want to consume:
package servicios;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.xml.bind.annotation.XmlSeeAlso;
import javax.xml.ws.Action;
import javax.xml.ws.RequestWrapper;
import javax.xml.ws.ResponseWrapper;
/**
* This class was generated by the JAX-WS RI.
* JAX-WS RI 2.2.10-b140803.1500
* Generated source version: 2.2
*
*/
@WebService(name = "Service", targetNamespace = "http://Servicios/")
@XmlSeeAlso({
ObjectFactory.class
})
public interface Service {
/**
*
* @param name
* @return
* returns java.lang.String
*/
@WebMethod
@WebResult(targetNamespace = "")
@RequestWrapper(localName = "hello", targetNamespace = "http://Servicios/", className = "servicios.Hello")
@ResponseWrapper(localName = "helloResponse", targetNamespace = "http://Servicios/", className = "servicios.HelloResponse")
@Action(input = "http://Servicios/Service/helloRequest", output = "http://Servicios/Service/helloResponse")
public String hello(
@WebParam(name = "name", targetNamespace = "")
String name);
}
But in my JSP I can't see that method
<%@page import="servicios.HelloResponse"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<%
String value = request.getParameter("Hello");
String value2 = request.getParameter("Luisa");
servicios.Service.hello(value);
%>
</body>
</html>
But I get an error, what can I be doing wrong?
the mistake is:
non-static method hello(String) cannot be referenced from a static context
Edition:
Edition:
add interface
package servicios;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.xml.bind.annotation.XmlSeeAlso;
import javax.xml.ws.Action;
import javax.xml.ws.RequestWrapper;
import javax.xml.ws.ResponseWrapper;
/**
* This class was generated by the JAX-WS RI.
* JAX-WS RI 2.2.10-b140803.1500
* Generated source version: 2.2
*
*/
@WebService(name = "ServicioWeb", targetNamespace = "http://Servicios/")
@XmlSeeAlso({
ObjectFactory.class
})
public interface ServicioWeb {
/**
*
* @return
* returns boolean
*/
@WebMethod(operationName = "Hello")
@WebResult(targetNamespace = "")
@RequestWrapper(localName = "Hello", targetNamespace = "http://Servicios/", className = "servicios.Hello")
@ResponseWrapper(localName = "HelloResponse", targetNamespace = "http://Servicios/", className = "servicios.HelloResponse")
@Action(input = "http://Servicios/ServicioWeb/HelloRequest", output = "http://Servicios/ServicioWeb/HelloResponse")
public boolean hello(
@WebParam(name = "user", targetNamespace = "")
String user);
}
Let's imagine we have the following class:
And then we use the class like this:
This line of code:
It will give us a compilation error, because the method
hello
is not static. The class nameService
can only be used to access STATIC methods or attributes .If we edit the method definition
hello
and make it static:It would be valid to use it this way:
So there will be no compile error because it
hello
is a static method.Now to access an instance method it is mandatory to instantiate the class, otherwise there will be no way to access the members (methods and attributes) of the class
Service
.This same error would happen if we were to use an interface:
The error is because
hello
it is not a static method and even if we add the to the interfacestatic
method declaration , the error: will be fixed, however another error will appear, because the routine would not have an implementation (a body of code).hello
Service
non-static method hello(String) cannot be referenced from a static context
hello
The solution to this error is to create an object of type
ServiceWeb
and then call the methodhello
through the variableservicio
(of typeService
).For this code to compile correctly, the
ServiceWeb
if or if class must implementhello
the interface methodService
.