Setting a session and ajax call happens parallely - java

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?

Related

Session Attribute Not getting updated in .gsp Page after Ajax Call

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.

Javascript not executing on JSP forward action

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/

how use RequestDispatcher foward keeping url requested?

I have a JSP page to search customers. This page calls the controller, which execute a method to return a list of customers and after forward to the origin URL;
I used to forward : request.getRequestDispatcher(urlOrigin).forward(request, response);
(note 1: request.getHeader("Referer") was used to get complete origin URL )
(note 2: There a method to split the complete origin URL and get name page )
Since it, I have the following url in the browsear :
(http://domain/ProjetoT/mvc)
Its the url of my controller
If I search a customer again won't work, because the controller url will be recognized as origin url.
I Tried use : response.Sendredirect(urlOrigin);
But I lost my object and the list of customers didn't rendered.
Anyone can help me please?
Thanks!
Instead of intially accessing the JSP page directly in the browser, you could access it through the same controller used to process the search. To do that you would have to program your controller to detect if you are in initial display mode or if you are in "submit" mode. This is typically done by checking the presence of a parameter that is sent in the submit.
So in initial display mode, your controller would just forward to the JSP without any further processing, while in submit mode it would do what it is currently doing. This way you would be using the same URL for both the initial display and the submit and the problem you described should go away (that is, if I understand your question correctly).

How to change url in user browser without client redirect in servlet

I want to forward from one page to another but with the same I want url to be changed. Suppose user is here http://mywebsite/register and when he completes his registration process then I want this in his address bar http://mywebsite/home
Is it possible without using sendRedirect , I mean by the way server side forwarding only? or any other way around to this problem?
You could just let the HTML form submit to that URL directly.
<form action="http://mywebsite/home">
But this makes no sense. You'll also run into problems when redisplaying the same form with validation messages in case of validation failure. You'd need to redirect back to the original page if you intend to keep the original URL and you'd need to fiddle with storing messages in the session scope instead of the request scope because a redirect basically creates a brand new request. You'll without a redirect also run in "double submit" problem whenever the enduser presses F5 after submitting the form.
Just let the servlet redirect the successful POST request to the desired URL. That's the canonical approach. Even more, this is a recommend "design pattern": the POST-Redirect-GET pattern.
AFAIK there's no way around a redirect since the browser has to update the url at some point. And if you'd update the url after the forwarded to page has been loaded it would issue a refresh and the page would be loaded again (which might result in an endless loop).
Why don't you want to use a redirect in that case?

JSP page values are not getting populated after getting values from JSP popup window

I am passing JSP values to JSF's backing bean. Once I get the values in bean, then I am trying to assign values to inputText fields with setters method, something like this.
public void testProcess(){
empBean.setEmpName(empBean.getEmpId());
}
testProcess method is called in the action of the JSP page.
My question when I set the value in the bean, my JSF page's values are not getting populated. Do I need to explicitly refresh my JSF page, if so how could I do that?
Yes, you need to refresh the main page. What happens on the server will not automatically make something happen on the client (browser).
One fairly common pattern for this type of operation is to have the popup do the form post using ajax (easy with many javascript frameworks like jQuery). See http://jquery.malsup.com/form/ for a nice example using the jQuery form plugin. In the response handler for the ajax-call, you reload the main page (using the window.opener property) and close the popup. In the example with the jQuery Form plugin, you would do something like:
<script type="text/javascript">
// wait for the DOM to be loaded
$(document).ready(function() {
// bind 'myForm' and provide a simple callback function
$('#myForm').ajaxForm(function() {
// reload main page
window.opener.location.reload();
// close popup
window.close();
});
});
</script>
The reason you want to submit the form using ajax is that you want to wait to refresh the main page until the post is completed and doing a regular post will cause the popup to reload, which in some browsers will invalidate the window.opener property, making it impossible to reload the main page.

Categories