reading web.xml init-params from client gwt - java

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.

Related

Is there an easy way to log the content of the console to a web page?

Currently, I develop with java / spring-boot / log4j2 , and as much as I have searched I have not seen anything interesting to log directly to a web page in real-time (something like swagger style with requests, which is by configuration without having to write code) .
Do you know anything?
You need Javascript to modify the DOM of a webpage, not Java.
You could use Spring to send Server-Sent-Events (SSE) to a frontend JS library, or host a WebSocket or other REST API on some web server and use AJAX to issue requests, upon which the DOM is modified as part of a response, but this really has nothing to do with Java/Spring/log4j
If you want a packaged distribution of Spring w/ some Javascript framework, JHipster is a popular option.

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.

How to send HTTP POST Requests from a Servlet to an external Web Service?

I am using GWT RPC to communicate between the client and the servlet, but from that same servlet, I need to send XML data through a POST HTTP Request to an External web service.
Basically, I send it some XML data, the Web Service processes it and returns some other XML Data that I will be using.
I tried using com.google.gwt.http.client.RequestBuilder but I was getting an Exception error (java.lang.UnsatisfiedLinkError).
From what I've read, this is due because that class is a CLIENT class, and therefore cannot be used in the servlet (server-side).
What else can I use to build POST HTTP Requests?
Thanks in advance.
(Since the servlet is some java bytecode running, instead of java translated to Javascript on the client, I can basically use Java classes from the JRE/JDK)
Do NOT use any GWT specific classes in your servlet code!
why narrow the wide thing!
There are variety of APIs you can use to send HTTP requests to external servers..
If you don't like to use any external libs, then use java.net.URLConnection
it is simple to use..
Or even you can use the most simpler one, Apache HTTPClient

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

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.

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