When I'm starting my Spring Boot application it takes time to load all needed artifacts which is totally acceptable. It takes 2 minutes for the user to wait.
Is it possible to implement starting page "Hello! We are starting... Just wait a moment" and reload it to main page when it's done? I'm using Spring Boot.
You can try this solution.
Tomcat is started when all Beans are initilizazed, so you can monitor this initilization and display on some page.
Maybe you could put HTTP server proxying your server. In case your server is down you could display a simple HTML page with the message your proposed.
ProxyPass / http://localhost:8080/
ProxyPassReverse / http://localhost:8080/
ErrorDocument 404 /loading.html
In this page perform AJAX call to your real page. For example (for convenience I used jQuery, but you could also use vanilla javascript or another library):
function pollPage(){
$.get("http://localhost:8080/realpage", function(data) { //when page is loaded, replace content of current page
var replacePage = document.open("text/html", "replace");
replacePage.write(data);
replacePage.close();
}, function(){
setTimeout(pollPage, 10000); //If page is still inaccesible try again in 10 seconds
});
}
This would replace the whole initial page with your real page.
Related
Is it possible to create a servlet that let's the browser of the client stay in that particular page, while at the same time changing a value of a session wide variable?
Basically I have a button on my webpage, which is encapsulated in an form that calls an servlet on submit. On that servlet I now want to increase a counter for that session by 1, but I don't want the page of the client to change, in case the user wants to press the button several times.
How can I program the servlet to not send back a response to the user, or if that's not possible, how do I sent a response that basically says: Don't do anything, stay where you are.
I tried simply not sending the response with .forward(), but the browser is then stuck on a blank page. I also tried .setContentType(null), but still nothing. Is this even doable?
Do an AJAX call to that servlet in client's browser using XMLHttpResponse object like var xhr = new XMLHttpResponse()
just google about AJAX request
Java application is loading raw ajax data after the session has timeout and the user logs back in.
Scenario:
User logs in successfully
Application loads and sits idle for the session timeout period
User selects an option that is an ajax call to refresh page content
Since the timeout period has passed, the user is redirected to the FORM-based login page defined in the WEB.XML file
User again logs in successfully
Problem starts here: Since the last URL request was for an ajax call, the page loads with the raw content without the full HTML page wrapper
Question: Since Tomcat is handling the Login and session creation process -
How can I handle this situation by either sending back a complete HTML page with the request or just send the user to the application landing page?
I was hoping for a more elegant solution, but here's what I did to solve my issue.
At the top of my servlet controller I added the following code. If the sessions variable "FirstTimeIn" is null I know the user just logged in. I then check to see if the userPath is "/ajaxCall". I use this to identify all my ajax requests. If the userPath is "/ajaxCall", I then redirect the user to the landing page of my web application.
if (session.getAttribute("FirstTimeIn") == null) {
session.setAttribute("FirstTimeIn", "No");
if (userPath.equals("/ajaxCall")) {
response.sendRedirect("sc?action=dashboard");
}
}
I have an idea to make something pretty sweet but I'm not sure if it's possible. Here is an example of a very basic ajax function that I might use to establish a connection a server...
function getFakePage(userId)
{
var ajaxObject, path, params;
ajaxObject = getAjaxObject();
params = "?userId=" + userId
path = getInternalPath() + "someServlet" + params;
ajaxObject.open("GET", path, true);
ajaxObject.send();
// On ready state change stuff here
}
So let's say I have a URL like this...
https://localhost:8443/Instride/user/1/admin
And I wanted to use javascript to redirect the user to this this URL. Normally I would just do this...
window.location = "https://localhost:8443/Instride/user/1/admin";
But my idea is to create a javascript (no js frameworks please) function that could combine the ajax code with the window.location code. Basically what I would like to accomplish is to create a connection with the server via ajax, send a servlet on that server the url I would like the user to be redirected to, and then redirect the user to that URL. So that for however long it takes the user to connect to my server from wherever they are in the world they see a loading icon instead of a blank white page.
So to clarify exactly what I am trying to accomplish; I do not want to put window.location within the success of my ajax function (because that would be encompass two round trips), and I do not want to return a huge chunk of HTML for the requested resource and add it to the page. I want to establish a connection to the server with ajax, send a servlet the URL the user wants to go to, and then somehow override the ajax function to redirect that user. Is this possible?
And I know some of you might think this is stupid but it's not when you're talking about overseas users with slow dial up connections staring at white pages. If it's possible, I'd love to hear some insight. Thank you very much!
First, let me say that the best solution is finding what is causing the slowness and fixing it.
Now as to your question, yes you could do it. You could even shoehorn it onto an existing application. But it wouldn't be pretty. And it comes with it's own set of problems. But here are the steps:
Browser calls ajax cache service requesting "somepage.html"
Browser loads loading icon
Server creates somepage.html and caches it in a temporary cache, (ehcache or other library would be good, probably with file backing for the cache depending on size)
Server responds to ajax request with ID for cached page
Browser now redirects to "somepage.html?cacheId={cacheId}" where the id is from the ajax call.
Server uses a filter to see if any cache can be served up for the page instead of the actual page, thus speeding up the request.
Having said that, it would be better to just have the new page load quickly with a loading icon while it did any of the heavy lifting through ajax.
You can't do an AJAX request and a location change in one. If you want to do only one request you have to choose one of those methods. ie. return some data and replace content on your current page, or load a completely new page.
It doesn't make any sense to want to want to do both. What you could want is stateful URLs; where your URL matches the content displayed, even if that content comes from an AJAX request. In that case an easy solution is the use the # part of the URL which you can change freely (window.location.hash). Some modern browsers support changing the whole URL without causing the page to reload. I've used # with great success myself.
My Servlet response type is html and my response contains a hyperlink to another web site.So, now i want to capture the information about whether the user clicked the link or not? and also calculate the total clicks? i am using Tomcat 7 as a server.
Is this possible in setting response header (302 or 404)?...
Please Guide me to get out of this issue?
Yes, you can use a 302: instead of providing the link to the other website, you provide a link to your own servlet, do your accounting and then send back a redirection (301/302) http status with the other web-site URL in the response Location header.
This maybe a bit simplistic though, since the user will leave your original page (is this what you want ?) and search engines may not like this if your web app is public.
I think right now you are redirecting the request(link for another website) at client side.In this approach your server cannot get the information about the click.
What you can do create a servlet and call this servlet on click now this servlet is responsible to redirect the request to another website. Add an static integer counter and increment this when servlet call each time.
Use the method setStatus():-
setStatus(HttpServletResponse.SC_FOUND);
or
setStatus(HttpServletResponse.SC_NOT_FOUND);
I'm trying to achieve the following behavior using the Servlet 3.0 API:
send an inital html page
send subsequent responses that update the page
This all works except the only way I could send the initial page without getting the response committed is by manually writing using the HttpResponse Writer...
I was wondering if there is a way of using something similar to RequestDispatcher#include with an html page without running into problems with the AsyncContext. Some things I tried until now and didn't work:
use AsyncContext#dispatch: as much as I read on the Internet, it is destined for sending the final response to the container in order to render it
use RequestDispatcher#forward: getting IllegalStateException due to trying to write more content in the OutputStream
use RequestDispatcher#include: if I initialize the AsyncContext before calling this method, request.isAsyncSupported returns true, after calling the method, it returns false. I read that it calls flushBuffer() and sets the commit flag to true on the response
Also, in the Servlet 3.0 spec there are some lines mentioning that dispatching from async servlet to normal servlet is possible but will commit the answer. I believe a static html page belongs to this category...
If you have any ideas of how an elegant include can be done without affecting the ability to still send streamed responses back to the client, please let me know.
Thanks
One solution (not the only one): if it's just a html page, then write the html page itself in html and do ajax calls to the serrvlet that needs to provide the updates.
use static elements on the page that store data and use requestdispatcher.
Or you could also just simply refresh the entire page with such an arrangement using response.setHeader("refresh", "5; URL=officer.html").
I really don't understand your need to send multiple requests without the response being committed to a servlet. are you trying to interact with a serving thread multiple times ?