Using a GAE Endpoints object in a servlet - java

I'm adding a simple web interface with the already working backend I wrote in Java for and Android app. I know there is a way for exposing GAE ednpoints to JS. https://cloud.google.com/appengine/docs/java/endpoints/consume_js
Instead I want the calls to the endpoint to be generated from a java servlet and pass them to a JSP so I initialise the UserEndpoint and call it's methods inside the doGet().
Is there anything wrong with this method ?

Related

How to talk to a Java servlet from a Native IOS App

we made a HTML5 mobile app, & now we are creating a IOS native App. We want the IOS app to call methods on the Java servlet (with stripes) (running the HTML5 App) so that we can use the same behind-the-scenes logic. Do we need to setup a web service to do this?
I.E we would like to just call the existing Stripes action beans from IOS (as they are already set up to receive ajax requests, but will this violate the cross-domain law)?
Thanks!
Cross-domain isn't an issue if you're making requests from a native app, so it should be possible to make requests to your AJAX ActionBeans directly.
You can directly call the logic by using API and get the response. Do servlet mapping to the url pattern you need. then in servlet extend the HttpServlet and override doGet and doPost method, which has HttpServletRequest and HttpServletResponse as input parameters, which can be used to communicate with your mobile app.

java servlet google in app payment

I have studied the java in app billing code snippet at
https://developers.google.com/in-app-payments/docs/tutorial
and I am unable to use it to make my application capable of doing in app payments.
My first question is how do I set up a servlet to handle payment requests. Do I put the getJWT() method in the servlet and call it from the doPost() method?
My second question is what do I do with the String that getJWT() returns? It should be the json object that holds the purchasing information, but I don't know how the jsp file I have should process it.
I have searched for example code using java servlets and jsps to study but found none. I have found python code, but I can't translate python into java yet. If anyone knows of an example (complete) of google in app billing using java servlets and jsps I would appreciate it if you could post a link also.
Thank you.
On the server you need an HttpServlet derived class that takes the order request (in doPost()), calls the JWT libraries together with the Seller Secret to generate the signed JWT string, then returns the result in the response.
On the client side in the HTML page you can use a templating system (e.g. AppEngine + JSPs) or Ajax calls to your servlet to get the generated JWT.
The generated JWT is one of the paramenters to the goog.payments.inapp.buy() JavaScript API.
Below is a simple AppEngine Python implementation of In-App Payments. You can re-use the client-side code and replace the server-side with the Java implementation:
https://code.google.com/p/iap-python/

How can I upload a file to Struts from a standalone Java class?

I am trying to call a Struts action from simple, standalone Java class.
The Struts action uses FormFiles to receive file uploads.
I am reading about URLConnection and HttpURLConnection, but am not sure how to proceed.
In general form submission is just an HTTP POST request with parameteres.
It gets generated by your java standalone class and sent to the server, the server proceeds this request and then redirects it to Struts.
However I wouldn't use here directly the low-level java API since it doesn't implement the HTTP protocol.
If putting an additional jar to your project is not an issue, I suggest you to use
Apache HTTPClient project
Here is an example (much more simple than it would be with HttpConnection):
Example
As per my understanding from your question ... you are creating object of action class (by standalone java class) and want to use it similar as action class works in struts.
But when you create object of action class it works as a normal java class instead of struts's action class.
Action class object must be created by struts framework only.

Get userPrincipal in Javascript

I need to get userPrincipal inside a piece of Javascript code. My app currently uses Dojo on the client side and servlets on the server side. I am not using JSF, nor JSP (and if possible I would try to avoid using it only for this purpose), nor JQuery (I would avoid mixing JQuery with Dojo).
I have read this interesting post Mixing JSF EL in a JavaScript file . Following the idea #1, I have written a Bean but I am not sure on how to embed the call to the Bean in my (quite long) js file.
If you don't use JSF or JSP or anything similar, just bare servlets, I recommend creating a servlet which takes the userPrincipal name from the HttpServletRequest (or any other place) and returns it in JSON format. Then you can do an AJAX call to the servlet and find out what you need. I don't know Dojo too well, but I believe you can insert this AJAX call in your event chains pretty easily.
I don't recommend mixing managed beans with plain-old servlets.
If AJAX is not an option for you because of its asynchronous nature, you cand create a servlet that serves text/javascript content which offers the principal's name in the form of javascript code and then add a script tag to your page.

Using a Web-Service with Java Servlets

I'm trying to develop a very simple Java web application using JSP and Servlets.
1) There is a textbox and a submit button on the page,
2) The user enters his name, say John, to the textbox and clicks the button,
3) The string is forwarded to my servlet,
4) At the doPost method of my servlet, I access the posted string variable,
5) The web service I'll use has a sayHello method that takes an argument and returns "Hello " concatenated with the argument,
6) So, I call the sayHello method of the web-service, get the returned variable and forward this to a JSP, which basically writes Hello John.
I'm familiar with the JSP and Servlet thing, but I don't know how to use an already existing web-service, or how to make use of a functionality that is already implemented in that web-service.
All I have is the name of the method, sayHello, the URL of the web service, http://example.com/hello_service and a link to a wsdl file which contains xml-like code that I do not know how to make use of.
My question is, how do I make use of that web service, or how do I call a method inside a servlet?
Thanks in advance.
I'm using Eclipse for JavaEE Developers. How do I generate a client automatically?
Drop the WSDL file in your dynamic web project (or create a new project for it), rightclick it, choose Web Services > Generate Client, complete the wizard with default settings. A new package will be created where the generated WSDL client code is been placed. One of those classes have a ServiceLocator in the classname.
In the servlet, you need to instantiate the ServiceLocator class, get the SOAP service from it and then invoke the desired methods on it. Further detail can't be given since the WSDL is unknown.
See also:
Eclipse - Creating Web Service Client (Eclipse's own tutorial does it bit differently)
You can use "wsimport" from jax-ws to generate a client jar for the web-service. Then, including the client jar in your classpath, you can call the web service just like you would call any regular method.
you have to create client stubs which will be part of your code project (which has the servlet). The WSDL defines how to generate these stubs. The you can call the methods in the stub from your servlet. You can use a variety of tools to generate these stubs, Axis2 is one of the most widely used.
Here is the apache Axis2 documentation which tell you how to do it.
This stub will have the methods that the wsdl has defined. You will basically call these methods and internally the stub implementation (autogenerated from wsdl by axis2) will create the SOAP request based on the arguments you pass to the method. Then it will send this request over HTTP or HTTPS to the webservice URL. You will feel like you're calling code that resides on your machine, but internally it makes the call to remote webservice.

Categories

Resources