I am using a Java Web Service for
#WebService()
public class myWebService {
/**
* Web service operation
*/
MyClass Obj ;
#WebMethod(operationName = "webmethod1")
#Oneway
public void webmethod1(#WebParam(name = "serailNo") String serailNo) {
obj = new MyClass();
//do some operations on obj;
}
/**
* Web service operation
*/
#WebMethod(operationName = "webmethod2")
public void webmethod2() {
//do some operations on obj after doing intial operations in web method1
}
}
I am unable to access obj in webmethod2. It is getting a null pointer exception.
As a detail : I want to create a object . That object should be accessed across all web methods. In such a way webmethod1 will do intial operations on obj and followed web method2 will use the same obj.
How can i achieve this
If you are creating a brand new object, what you could do, would be to make your webmethod1 return whatever object it creates, and then, make webmethod2 take that same object as a parameter.
Another option would be to make webmethod2 take in the serialNo parameter and check that the obj variable is not null. If it is, it will call webmethod1 by passing it that same serial number, and create the object so that it has something to work on.
The reason you are getting a nullpointer is because the webservice is not stateful, i.e the object does not exist during the second call. It is possible to create a stateful webservice but that depends on the kind of webservice and server your running it on...
As npinti mentioned you could send the object along with the service. Although that might not be favorable, e.g if the object is big.
You could create a local cache on the server containing all the created objects by and mapping them with the serialNo.
Related
This question already has answers here:
How to access an object's parent object in Java?
(4 answers)
Closed 3 years ago.
I need to get the "host" object of another object. Here an example
Suppose I have the
class Peer {
final Service service;
public Peer(Service service) {
this.service = service;
}
public void ping() {
service.ping();
}
}
and
class Service {
public String hola() {
this.getPeer()? //HERE I NEED SOME COMMAND TO GET the Peer object, is this possible?
}
}
Then in Service class, I need to get the peer object. Is this possible?
You need to store the Peer object inside your Service object while creating an instance of Service, that's the only way for retrieving it.
Create inside Service a constructor which accepts Peer
class Service {
Peer peer;
public Service(Peer peer){
this.peer = peer;
}
public Peer getPeer() {
return peer;
}
public String hola() {
Peer peer = getPeer();
}
}
Thing is:
this.getPeer()? //HERE I NEED SOME COMMAND TO GET the Peer object, is this possible?
There is no "command" to do that. Simply spoken: java objects have no notion of the context they were created in. The only way for an instance of Service to know about the Peer that created it: for example by passing a reference to that Service object, for example within its constructor. Or by having a setter method on the Service class that allows you to do that.
But of course: that sounds like the wrong way. A service instance should be really independent of the Peers using it! In other words: if it really makes sense that a Service knows one (or multiple?!) peers, then the Service class needs a field to "remember" that relationship. But at least the example given here rather suggests that your Service should not at all know (or care) about the Peers using it!
I have a requirement where am setting up a List of objects as model attribute in spring controller and I need it to assign to an object in angular .ts file.
Here is what I need to do. My java object:
public class Lab {
//some members of the class
}
This is how am adding it to Model:
public String getIndexPage(#RequestParam(value="val") List<Lab> labs, Model model){
model.addAttribute("val", labs);
return "index.html";
}
The corresponding object in Angular named inside lab.ts:
export class Lab {
// same variables as in java
}
And here is the component.ts file where I need to assign the val list:
export class VideoFormComponent implements OnInit {
selectedValues: Lab[] = new Array(4);
ngOnInit() {
this.selectedVideos = "${model.val}"; //assignment statement
}
But here is where am getting the error which says:
Type '"${model.val}"' is not assignable to type 'Lab[]'
What is the correct way to use that list inside the script file?
"${model.val}" is just a string in Typescript.
If model.val is an array of objects that are shaped like Lab[], you can do this.selectedVideos = <Lab[]>model.val.
If model.val is an array already typed as Lab[], you can simply say this.selectedVideos = model.val
I'm not sure what you're trying to do, but I'm assuming you're using Angular HttpClient or websocket or something to first get the data from a server endpoint to the client.
How are you providing the model data to the angular component? If your data comes from a Http request you should create a service that makes the request and then call it from your component and set the result in the selectedValues variable. Or if you are providing the data as an input to the component you should use the #Input decorator. In either case the data is provided externaly to the component. For what I read, you are trying to use a model object that has a val attribute, but you are not providing it anywhere.
Lets say I have a Java function something like
public int getNumber(){
}
which returns some value based on it's logic. And I have a JS file something like
Tapestry.Validator.amountValidator = function(field, message) {
field.addValidator(function(value) {
if (value != null) {
// code here
}
}
});
};
Now I am asking myself is it possible in JS or JQuery to pass value from Java function to it's function(value) in JS and if so, how can it be achieved?
UPDATE: As suggested by abalos answer, Tap for myself has already done 3 out of 4 stages for it. I am providing a function that deals with server side and logic behind it.
#InjectComponent
private TextField amount;
#Inject
private FieldValidatorSource fieldValidatorSource;
public FieldValidator<?> getAmountValidator()
{
return fieldValidatorSource.createValidators(amount, "required,max=" + getBroj());
}
Now here validator is taken from a logic inside a function getBroj(), which is maximum number of what it takes. And this works like a charm on server side. Now I was thinking that what I don't have( using my logic ) is only Client side, and I can achieve it by updating current Validation class from Tapestry that will handle with this kind of request yet known to that class. And to do it I would need to call a js file with a function calling something like above in the example, but I am not quite sure how to pass value from getNumber() function to the JS function above.
You don't need Jersey or DWR or any other framework at all for invoking a method in Tapestry. You just need to ask your questions properly.
final private static String EVENT_NAME = "whateverEventNameYouWant";
#Inject
private ComponentResources resources;
#Inject
private JavaScriptSupport javaScriptSupport;
/** Method that will provide the value you want to pass to JS. */
#OnEvent(EVENT_NAME)
public JSONObject provideValue() {
JSONObject object = new JSONObject();
object.put("value", /* the value you want to pass to JS */);
// other values you may want to pass
return object;
}
void afterRender() {
// This creates an URL for the event you created. Requesting it will
// invoke any event handler methods for that event name.
Link link = resources.createEventLink(EVENT_NAME);
javaScriptSupport.addScript("var eventUrl = '%s';", link.); // the JavaScript variable name doesn't matter. You can choose any you want
}
Then, in your JavaScript, do an AJAX request using the URL in the eventUrl variable. I'll leave this part for you to figure out from the jQuery documentation. The received data is exactly the JSONObject or JSONArray you'll return in your event handler method.
I think you have some very heavy misconceptions into what types of languages Java and jQuery/Javascript are. First off, with the exception of node.js, jQuery/Javascript are used for client-side operations. Java is used for server-side operations. This means that you will need to pass a value from the server to the client.
Now, what you are asking for looks initially like it is trying to perform validation. This should not be completed only on the client-side. There are ways to get around client validation and it is best to leave information from the client in an "untrusted" state until it is validated on the server.
With all that said, to do what you are trying to do will require the use of some method for the client to communicate with the server. My favorite way to do this for simple operations is through a web service.
Here are steps to do what you require, but note that this is not the only way.
Create a web service with Jersey.
Pass the value to the web service via AJAX with either JSON or XML with a request that contains the value.
Perform your validation on the server-side with the information from the service.
Pass a response from the rest service back to the client-side AJAX call and use it for your JS/jQuery code.
Let me know if you have any questions.
Using XML-RPC client and server, redstone.xmlrpc.XmlRpcServlet and XmlRpcClient
I don't have control of the implementation of the Servlet, but just the client. I am sending an object to the servlet.
The servlet seems to be using some form of reflection on the object I send it. The problem is that I have a method on the object called:
public boolean isPhysicalDevice()
{
return !getAddress().isChannel();
}
However, the servlet gets a list of the keys on the object and gets one key as "physicalDevice", which is not an attribute of the object but only exists as part of the name of this method. Is there a way, I can perhaps annotate the method on the object I send, so the servlet doesn't try to reflect this method and treat it as an attribute of the object?
Factor out the interface you want the servlet to see and pass it a Proxy for that interface which simply delegates every invocation to your object. (Proxy is Serializable.)
I have written a web-service application that has in a main class generated random value per request (for logging).
I cannot set it as a static field because next request will override it.
I also cannot pass it to the every class that I use in the main one (as an argument or with setter).
Is it possible to create some semi-static field - visible for one request but not for every other that go to the web-service ?
You can safely assume that, in the Java EE model, each single request is served by a single thread and that there is no contention by concurrent requests.
Having said that, you can employ a Singleton using a ThreadLocal, let the Servlet populate the value and have the underlying classes access the sigleton without having notion of the threads or the HTTP request context:
public class RandomValueHolder {
private static ThreadLocal<Long> randomValue;
public static Long getRandomValue() {
return randomValue.get();
}
public static void setRandomValue(Long value) {
randomValue = new ThreadLocal<Long>();
randomValue.set(value);
}
}
Why not use HttpRequest and store the value as attribute
Save the data in the request itself with Request.setAttribute() and use the corresponding Request.getAttribute() to retrieve it.