I am using jsp with script tag (<% %>) .And I use ajax to update some contend of jsp which have Java code with in script. In the script tag I get some session variable to process. And in ajax call I update that session variable from the action class but I can't get updated value for the front end jsp variable(inside the script tag).
Can any one tell me how do I do this?
You are trying to use javascript to update some Java variables which, fortunately or unfortunately, have already been processed by your application server and are no longer present on your output HTML page.
I think you mixed up server side and client side programming. JSP code is executed at server side and a your session data will remain at server only. All the output generated from server will be sent to browser and displayed. Although you make a ajax call, the session data will only update at server side but not update at browser unless your ajax call can handle the result from server and update the content accordingly.
Related
I have an tomcat server application where in a HTML form is displayed. Once I submit the data the page redirects to another page. In the back-end there is a JAVA program which takes some hours to complete execution. In the mean time if some-one tries to open my HTML page displaying form(First page) from another browser or another tab, it has to redirect automatically to the execution page. Is it possible to do this with Tomcat Server and JAVA? (I am using it on RHEL machine. So please do not post platform specific solutions.)
Here is an approach:
Introduce a flag in your server side code to determine whether the multi-hour-job is in progress
Whenever backend job is started, set the flag. When job done, reset the flag.
Either in your servlet doXXX method or in a servlet filter, check flag if set then redirect, if not then return the form view
I have a jsp page(calendar) with lots of JSTL tags in it.
I set the attributes in my servlet and get them in my jsp page thanks to JSTL, EL.
When I press nextweek, I open a xmlhttp which sends a GET to my servlet(Ajax).
All my attributes renew so i want to get them again in my jsp page.
I do not want to dispatch the servlet to my jsp page because of performance latency.
I don't want to fetch servlet results because they are attributes.
I just want to refresh my JSTL & EL so they will get the new values (without refreshing the page).
Is this an illogical way of thinking? but anyway, how can I refresh my JSTL,EL, scriptlets so the new values will appear?
I just want to refresh my JSTL & EL so they will get the new values (without refreching the page)
This is impossible. Note that EL and JSTL run on server side in view build time, so once they're applied when generating the server response, they can't be updated in the page until the server generates new content using the view (basically, your JSP with JSTL, EL and other components)1.
You should look into AJAX requests to your servlet (or the controllers you're using) and probably handling a JSON response to resolve the behavior of your JSP page.
More info:
How to use Servlets and Ajax?
1 Scriptlets also fall in server side category but I omit them since you should not use them for being highly discouraged to use in modern Java web development. More info How to avoid Java code in JSP files?
HttpServlets, JSP, JSTL, EL, and scriptlets are all server side components, ie. get executed on the server to produce an HTTP response. Javascript and AJAX are client side components, ie. work on the returned HTTP response.
You cannot refresh my JSTL & EL on the client side because they simply do not exist.
A possible (and common) solution is to have the request you make with AJAX produce a JSON response which you use to populate/replace HTML elements, where your EL had previously been used to set a value.
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.
I have a servlet in tomcat. It takes a really long time for the java code in the backend to execute. Is there a way to load static resources (css,images,javascript) in parallel with the code in the backend? Right now, they are only loaded once the code finishes running.
You could use an Ajax-style solution where you paint your page without data, with a placeholder for retrieving the data, maybe even with a "loading" spinner graphic.
The way that an Ajax call works, when the page is loaded, some Javascript will fire that will launch an Ajax request to Tomcat via XmlHttpRequest that will start the calculation. The browser will notify the browser when the tomcat request is completed. Then there will be some javascript in the webpage that will take the response and replace the placeholder. If the server returns an HTML fragment, it's as simple as executing in javascript placeholder-div.innerHtml = your-response-text.
Here's a basic tutorial on Ajax and a Java-based example that has the web front-end communicating with a Java Servlet back-end.
I need know to execute one ajax function before than one jsp:include puts in same jsp.
I try put one session parameter with this ajax function, this parameter is used for include, but first jsp execute include and parameter is not put and imposible to use.
Someone know fix my problem?, thanks guys!
AJAX and JSP are completely different technologies and are run on different systems and at different times.
JSP processors are executed when the page is generated, i.e. on the server side.
AJAX functions are triggered in the browser by Javascript (hence AJAX) which is available after the page has been generated (using JSP) and delivered.
If the AJAX function triggers a JSP on the server side, the JSP or Servlet should read the needed parameter from the request first (the AJAX call would put it there), e.g. by accessing the implicit request object in an expression within the <jsp:include> tag.