redirect page invoked from java class - java

Im new to java web app, JavaEE if I'm not mistaken. it happen that I was task to finished a certain project, while learning Java web app. So I'm not even certain if where am at is right. Here is the dilemma Im facing.
It had a timer on a certain java class. based on the timer count, per couple of seconds, the page is suppose to redirect to another page. To explain further of what I want to achieve, refer to the following:
user opens Page1
after 30 mins redirects to Page2
after 15 minutes redirects to Page3
the timer is set on the java class and not on the jsp file.
is there a way that the java class could issue a redirect command to the web browser?

As far as I know this is very tricky if not near impossible to achieve without using the client side. The problem is that the communication between the server and the client works in a request/response manner. This means that if the user requests a page, controller prepares it and then it is served to a client via response. Doing it right away without a timeout isn't a problem with response.sendRedirect(), but in order to push an action from the server to the client after some timeout, you would have to implement some sort of a listener on the client side.
This problem can be of course solved with a script on the client's side which could handle automatic redirects.

Use JavaScript timeout function and not Java's
setTimeout(function(){
// Set your redirection logic here
},(60000 * 10 ));

Try this :
response.sendRedirect(request.getContextPath()+"/YourJsp.jsp");

Related

Tracking site XHR with Java

Tried to use htmlUnit to send POST requests to communicate to server and met a tiny problem: target .php url is being changed from time to time
(www123.example.net -> www345.example.net, etc.). The only way to get new adress is to open site and check it's XHR requests, find one which goes to www???.example.net and then use this address to send POSTs.
So the question is: is there a way to track XHR using htmlUnit or any other Java library?
If you really need help you have to show your problem in more detail, provide some info about the web site you are requesting, show you code and try to explain what you expect and what goes wrong. Without this details we can only guess.
Looks like you should try to think about HtmlUnit more like a browser you can control from java instead of doing simple Http requests. Have a look at the simple samples on the HtmlUnit web site (the one at the bottoms is for you).
Try something like this (the same steps as the user of an ordinary browser does)
* open the url/page
* fill the various form fields
* find the submit button an click
* use the resulting page content
Usually HtmlUnit does all the stuff in the background for you.

server restart notify to clients

I am working on web based applications.
Server : Tomcat
Technology : Simple Jsp/Servlet/JQuery
I need to restart my server as an when new updates are there. And these changes are frequent almost every 1 or 2 day. I think to create some mechanism like I can say to every logged in user to save their changes and server will start in few minutes. (Timer will be there). Popup should be open though user is ideal.
Is there any direct way to do this so? Or I need to implement ajax call on every few seconds to server on every jsp page to check if any message is there on server???
Any idea will be appreciated.
Thanks in advance.
For the approach you are taking, I would suggest you to use Async Serlvets(Req. min Servlet API 3.0) or Apache Tomcat's comet technology(Kind of Async Servlet).
You will make ajax call on every page when it(page) loads(ajax onload() for eg.) to Async Servlet and will idle until response from server comes. This Async servlet should send Server Restart notification to all connected clients- whenever you trigger notification manually. Once ajax client gets notification, it will display the Warning(or user friendly message).
This will remove the need to make unnecessary polling to server after fixed internal - A big plus resource wise.
Personally I wont suggest this way, better get agreed on specific timeframe for deployment everyday(every two days) with clients and perform deployments in this time.
If you are from India- You must be aware about IRCTC website- Which is not available for train reservation every night for 1 hour.

Dynamically update JSP page via Servlet

I am new to web programming with Java. I have a Client/Server written (in Java) and I want updates from the Client to be sent from the Server to a web interface for a user to view. The timing of the updates will be random, but I want to be able to dynamically update a web page with new data without the browser having to refresh.
Hope this makes sense:
I've tried creating a Servlet that Observes (implements Observer) my Server (which extends Observable) for updates (Strings), however I don't know how to dynamically add these to a browser window. I have tried printing directly from the Servlet using PrintWriter out = response.getWriter(); in the doGet() method, but response.getWriter() is unavailable in the Servlet's update() method.
My initial thoughts were to use a JSP page (I need to eventually incorporate HTML/CSS) that receives the Observer updates from the Server but I'm not sure.
I've done some research into Comet/PUSH, but I'm not sure if this is what I need - perhaps a bit overkill? Any advice on how to achieve what I'm after would be greatly appreciated.
The most common way to do this is for the client to poll the server for changes. Use an AJAX request on the client side to poll an endpoint on your server.
You will then need to use JQuery / Javascript to update your web page with the new data retrieved from the AJAX request.
I would suggest using JQuery in theb rowser and using the AJAX function: http://api.jquery.com/jQuery.ajax/
It allows you to do a callback after the request and in that call back you can update the content of your web page with the data retrieved from the request.
You cannot update a web page from server, not without recurring to polling or push.
If you want a true push, then comet is not overkill, otherwise you can use a polling script on the web page.

Send data to front end when back end updates [duplicate]

This question already has answers here:
Real time updates from database using JSF/Java EE
(3 answers)
Closed 2 years ago.
The backend of my web application receives updates from several clients. When such an update happens it should be communicated to all other clients.
How can I initiate an update from the server to all web browser clients when my backend is updated?
I'm using JBoss, JSF and the Spring framework.
See similar Stack overflow quetion : WebSockets vs. Server-Sent events/EventSource
I'm assuming, as DarthVader did, that your frontend is a (generally) stateless HTML page of some sort. Something in a browser. If you want all clients to be pushed changes automatically, you have three options:
Comet: (deprecated)
Comet is essentially making AJAX requests that have no request timeout limit. You make the request, and it sits there and streams data through it as is neccessary. This can be done with hidden iFrames or standard XMLHTTPRequests (which jQuery can wrap for you). You can read more about this method here.
Long Polling:
Essentially, you use the javascript setInterval method to continuously poll your server for changes. Simply set an interval that does a standard AJAX GET request to the server, and upon every success, update your page accordingly.
Browser APIs
HTML5 WebSockets
Using any type of Event-Based backend (Twisted, EventMachine, node.js, etc) makes WebSockets the ideal solution. Simply have all clients register with the backend, and upon a submit from any given client, push the changes to all other clients. You can read more (and see a nice example) of WebSockets on this page. Browser support => canIuse
Server-sent event (SSE)
With server-sent events, it's possible for a server to send new data to a web page at any time, by pushing messages to the web page. These incoming messages can be treated as Events + data inside the web page.
Browser suppport => canIuse
When you say front end, you are talking about stateless http client.
You cant push anything from your web servers to http or stateless clients.
The "trick" to do this if using asynchronous calls from front end to your back end, periodically.
Think about gmail, how do you think it displays that you have an email when you recieve a new email. You browser contantly, sending Asynch calls to gmail servers, if and when there is a new message, it displays it.
So Clients are stateless. use Ajax.
is this clear?
There are a couple of ways to go around this.. The way it should be in the future is following standards like Websockets
For now you are stuck with Comet which is essentially sending a request to the server and keeping it open (not signaling a response end) and just streaming data through it (Parking the request they call it). Or periodic polling, where you just do an AJAX request to the server every predefined interval to ask if the server has something new to say. Needless to say the first work around requires streaming support on both the server and browser but is more efficient in most scenarios.

detecting if a file is ready and serving it for download

I have a Spring MVC web application that is generating a report on the server, once the report is generated, I need to enable a button that allows the user to download it. I am not sure how to go about doing this.
I figured that I will have to spawn off a thread that will just keep checking for the existence of the file and use javascript (jQuery or prototype most likely) to handle the UI elements, but I'm just not sure how to tie these all together.
There are no threads in Javascript. Instead you'll set a timeout to do the polling. The polling would take the form of a URL that will respond with some sort of "ready" indicator when the file is ready. If the file is not ready, then the AJAX success handler will start another timeout. When the server says it's ready, your Javascript handler will make the button visible and no further polling will be necessary.
Check this example here http://forum.springsource.org/showthread.php?t=70489 and let know if it works
You could use some type of messaging on the server that tells the client when the file is ready e.g. we us a table for all report requests and the server writes the status into the table and the client is then asking for the status of the report job with an AJAX call every few seconds.

Categories

Resources