I am using session attributes in my .gsp page like below:
<g:set var="maxValue" value="${session?.MY_VAR}"/>
I am making an AJax call from Javascript to the Groovy controller ( MyController.groovy ) and setting it like below:
session.setAttribute("MY_VAR", "abc");
After Ajax call returns back to the GSP. The value of session variable is not updated. It still stores the old value from the previous load.
Any ideas on how to solve this ?
Your GSP is generated on the server and the resulting HTML is sent to the browser. Your Ajax call does not force a page refresh, so the GSP along with the line below is not reevaluated and the variable maxValue is not changed.
<g:set var="maxValue" value="${session?.MY_VAR}"/>
You should consider render as JSON in your controller to return maxValue and using JavaScript to update the DOM after the Ajax response is received. See render for more info.
Related
Is there a way to let my page using ajax update a certain part content on my jsp webpage when the database change?
Currently I have:
1. db.java - a class to connect to database and query out the data I want
2. admin.jsp - a jsp page to display those data I want
My question:
How to using JSP and servlet automatically and dynamically update a page content while database changes, I know how to use JQuery ajax call servlet, but to achieved this, the only way I can get is set a time interval and loop repeating call ajax function, is this way workable ? or there are others better methods to do it?
I have been doing similer thing using jquery .load().. I have a struts2 action which is returning a VO ,which is binded with JSP view . i am calling same action via jquery .load() and updating JSP div.
I am using a polling timer which is handled in javascript to trigger this .
http://api.jquery.com/load/
Regds,
Amit
Hi I have one jsp page and I have to update two html select boxes with the data of an inf file. On selecting the first combo(initially also not loading the second combo), the second will have to change accordingly. Since java script variable is not accessible from java, I had sent the value of first combo to an another jsp by ajax. And then return it to first jsp itself.Plaese help me.
Maybe I'm getting it wrong but looking at your code it seems that you are doing AJAX request on selectbox change event, but you are not doing anything with the response.
So basically what happens is you are doing an initial render to your jsp and everything is fine. Now you are changing selectbox option and AJAX request is sent to dummy.jsp. dummy.jsp is receiving it and redirecting the maker of the request to User.jsp. User.jsp-s content is sent back but nothing is done on the client side AJAX call with this response.
What you should do is have your logic at dummy.jsp(or even better at some servlet) which based on your AJAX call request parameters returns for example a JSON with data that you can process in jQuery.ajax success event. In there you can use this JSON to readjust your selectboxes and other data with javascript.
Check the examples at the end of jQuery.ajax manual: http://api.jquery.com/jquery.ajax/
I am setting a session variable in doGet() like
HttpSession session = request.getSession(true);
session.setAttribute("MySessionVariable", "String goes here");
I am trying to display that in my JSP page upon ready like
<%= session.getAttribute("MySessionVariable")%>
and I am making a call to the servlet on ready like
<Script>
$(document).ready(function() {
$.ajax({
type : "GET",
url : "UserInfoDisplay"
});
});
</Script>
But what I see is that, the value is null for the first time and when I refresh again I get the value. That is the ajax call onReady and the page load happens parallely. How should I fix this? I want the data to appear on the first load itself.
Well, it's not exactly like that. Page load happens first, and when the page is loaded, it invokes the servlet. You have to take into account that <%= session.getAttribute("MySessionVariable")%> is executed server side, before the browser gets any HTML. Then, the browser gets the response and your script calls the server again and sets the property, but the page was already rendered. You have to do this in a different way. Can you explain what are you trying to achieve?
If you need it only for the first time, then why make a ajax call in the first place?
Ajax is meant for sending asynchronous requests. Correct me if I am wrong, the better approach would be to redirect a request to the servlet and get a response, isnt it?
I have a scenario where I need to use ajax to invoke the controller command. After the command is successfully executed, it returns me a ViewName.
But this view is not getting invoked (JSP page corresponding to this view is not loaded). But I am getting the ajax response back.
I want to make the ajax call. And when I receive the response, based on this I want to submit the form. And the ajax response should be available for me on this JSP page that gets loaded.
Is there any way to do it?
One option: put a hidden field into your form. Once the ajax call comes back, in your success function, you put your response data into the hidden field. Based on any other parameter your might set the action of the form. Finally submit the form.
The point of the ajax call is to return data back to the existing page, but if the json is returning a view name, just set the window location to the viewname in javascript. That will forward the browser to the appropriate place.
I want to pass parameters from one jsp to another.
I have tried using the post method, <jsp:forward/>, but it doesn't work.
I have created a <form> in html (parameters passed using POST), which is submitted to a servlet which processes the request and forwards it to another servlet that displays a page.
From this servlet i have created links to another jsp, passing through the parameters as GETs in the URL. However, I actually want to pass the parameters to another jsp using POST, and then pass it on to another jsp.
What solutions do you have or this problem?
Check out the Request Dispatcher. You need to forward the request to the landing JSP.
http://docs.oracle.com/javaee/6/api/javax/servlet/RequestDispatcher.html
Sounds like you are creating a multipage form that gathers information from the user across several distinct pages. In that case one option is to use hidden fields on a form to store the previous values. This of course means that as the pages progress the amount of data passing back and forth from client to server increases.
You may consider a server side approach by storing the interim values in a database for instance, then only passing a token back to the client. When the next JSP page is submitted, use the token to look up the values in the database.
JSP has built-in request object.when one jsp redirect to another jsp with some parameter, you can get parameter value using this request object.
<%
String param1 = request.getParameter("parameter_name");
%>
you can find example here -
http://www.roseindia.net/jsp/RequestObjectInJSP.shtml
Why don't you call a page on the click of a submit button by creating
an url in the below format (in javascript):- var
url="your_page_name.jsp?value1="+encodeURIComponent(document.getElementById("your_text_field_or_any_other_field_id"));
and then call the page by using your url
document.your_form_name.action=url;
document.your_form_name.submit();
and then use request.getParameter() method either in servlet
or in the jsp that u'v metioned in the url (servlet or the jsp u'll be
calling thorugh u'r jsp).