How to respond (if possible) to http get requests in GWT? - java

I would like to make a http get request with my Android phone (i know how to do that) and get the file in return (or some other response from database on the server).
Is it possible to do that in GWT (i just started reading tutorials) on the server side or will i have to learn PHP or sth. else?
I've seen this http://code.google.com/webtoolkit/doc/latest/DevGuideServerCommunication.html but don't know if this is what i need.
Thank you very much!

What you need on the Server side is a Servlet engine such as Jetty or Apache Tomcat (or one of many others). You would then write your RPC call as per the link you provided and the server-side Java Servlet (Which is what GWT expects you to provide) would read the file and transfer the data in the file back to the client. The client GWT part of the app would then read the message asynchronously and then do whatever.
For the Server part you need to know Java, I would assume you know that if you are programming GWT.
If you wanted to use something else, like an existing HTTP service in PHP, then you would use the RequestBuilder to build your get or post and send it to the server.
One thing to remember is that everything in your client folder will be compiled to Javascript by GWT. So even though you are working in one project you are actually coding two different systems. One which is in Java (The server folder) and the client piece which GWT translates to Javascript, which runs in the browser.

Related

Java application server without HTTP

I have a client software that is written in C++/C# and a database. Now I don't want the client to access the database directly, so I thought about placing an application server in the middle. This one should get a short request from the client, ask the database for new data, do some filtering (that can't be done in sql) and then return the data to the client.
My search for this kind of software brought me to Glassfish or Tomcat but my problem in understanding is, that these always want to talk http with html/jsp. Because most of my data is encrypted anyways, I don't need such plain text protocols and would be totally happy with something that just takes a byte stream.
On the other hand would it be nice to have a server handle the thread pool for me (don't want to implement all that from scratch).
After more than a day of searching / testing I'm even more confused than at the beginning (ejb, beans, servlet, websocket, ... so many things to google before understanding just the simplest tutorials).
TL;DR: how do I get Tomcat/Glassfish to just open a socket and create a new thread for every request, without any HTML/CSS/JSP involved?
Jetty and Tomcat are so called servlet container and thus primarly targeted at HTTP exchanges. Glassfish is an application server that uses a servlet container as one of its modules. I would stop thinking in that direction - that's all more like web applications and web services - some levels too high what you are asking for.
I think you should more look into sth. like Netty which is merley a "high performance protocol" server. Take a look at the documentation here (even some sort of tutorial there which might fit your use case).
GlassFish is an "enterprise application server", targeting the Java EJB specification. That's surely overdone for your purpose. You can give Tomcat a try. It is a "servlet container", targeting Java Servlet specification. Servlets have one purpose: listening to an incoming URL (request), executing Java code and returning a response, usually over HTTP.
For sure, you may start your own (plain) ServerSocket, for example using a ServletContextListener (which will be started once your application starts). But you should go for a higher protocol to send the data, like Hessian and Burlap, which is implemented in both, Java and C++ and easy to set up.

How to run a server to accept API calls from another application using an existing Java class?

I have wrote a java class which accepts some input and gives some output.
I have another web application, which would like to send a request json containing the input data to a server and receive response json containing the output data. The server will receive the request, parse it and format the input for the java class, and then utilize the java class to calculate the output, and finally format the response and send the response to the web application.
However, I am stuck at how to write this server.
Currently I have some ideas in mind:
1) Using a big frame work such as Play or Spring -> but usually those requires tomcat to deploy, which is too heavy for my little java class
2) Write my own HTTP server with java, according to this guide http://oreilly.com/catalog/javanp2/chapter/ch11.html
Anyone knows that if there is a light weighted java framework or java library that I can use for my purpose? By utilize which, I can simply write my server and run my service, but do not need to handle too much networking and threading?
Thanks for helping me out! All answer appreciated!
Jetty is an example of a server that can be run embedded. This removes needing to build a .war and deploy it to a servlet container such as Tomcat. Here are some examples.

Building a server to an android application

I checked this thread which actually looks for the same thing that i look - but he didnt got answered.
What i'm looking for is a way to connect between server (as a java application) and a client (as a android application) - in a way that the client will be able to call remote methods in the server, and get remote objects from the server. (doesn't matter if it uses AIDL or not..)
I worked with RMI and it's fine - but it unnable to use on Android, so i have to find another way.
Thanks for help!
Two possible avenues:
Use servlets on the server and pass data via GET/POST
JAX - pass data via SOAP requests
I'm sure there are others, too.

reading web.xml init-params from client gwt

I have a gwt appengine app that I am building. It has a web.xml file with some init-params in it. On the client side I am using a java class with an 'onLoad()' method. This is a plain 'EntryPoint'. I would like to read those init-params from the web.xml file when the page is loading. I know I can read them from the server side using getServletConfig().getInitParameter("string") but what I want to do is to read that init-param from the client side. Is there a simple way? Everything I read tells about doing this from a Servlet. Any help would be appretiated.
You are client side and you want information which are server side, so you need to use a technology to do so ,the servlet is the one that will allow you to collect information and send back the result to you client which will be processing it asynchrously.
You can fetch those values from Server ( servlet ) by either GWT-RPC or GWT JSON
GWT JSON Turorial - https://developers.google.com/web-toolkit/doc/latest/tutorial/JSON
GWT RPC Tutorial - https://developers.google.com/web-toolkit/doc/latest/tutorial/RPC
You can find tutorial examples in the sample folder of the GWT zip files.
Note : Both approaches have pros and cons and you can decide on their feasibility based on you application scope.
GWT-RPC is widely used if it is complete end to end java on client and server.
GWT-JSON is used more often when fetching data from non java server.

Java: Read POST data from a socket on an HTTP server

I have a website (python/django) that needs to use a load of Java resources that may or may not be on the same server. Therefore I am writing a mini webserver in Java that will receive a request and then when processing is finished, POST some data back to a url on the site.
I have got the java code receiving connections on sockets and responding with some simple HTML.
My problem is that I will POST data to the Java server and that code needs to act on the data. How do I go about reading the data that is posted in the HTML request, if it is even possible. If not, is there any other way you would do this.
If you think I am going about this in completely the wrong way then please tell me and I will consider another method, but after conversing with some Java developers, this seemed like the best way for what I was doing.
Thanks
You probably don't need to write a http server yourself, just use some lightweight java web server/servlet container like jetty or simple
and looks here if you still want to know how to parse a http POST request http://www.jmarshall.com/easy/http/#postmethod
In your case I probably wouldn't work with low level socket connections.
I recommend you to use a servlet container with some sort of webservice or maybe only a simple servlet depending on your needs. With a servlet you can easily access the POST data, process it and return something to the caller.
If you don't want to use a stand alone servlet container like Tomcat you can try an embeddable servlet container like Jetty or winstone servlet container.
If you need something more sophisticated you can use some webservice technology like JAX-RS or JAX-WS. This allows you to provide a fully featured API for your Java resources in an easy way.

Categories

Resources