Javascript not executing on JSP forward action - java

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/

Related

Removing success message after jsp page refresh?

I have a JSP page which displays a success message on successful deletion of an item.
However, if the page is refreshed, I want the message to not be displayed. I tried all I could do but it just doesn't happen.
I am setting the attribute in a Struts 2 class in request scope. I am retrieving the same attribute in my JSP page using request.getattribute(). After the if statement, I am trying to set the same attribute to null. But the success message remains after refreshing the page which I don't want.
Please help!
If you refresh the page, (from the browser I assume) you are actually sending the same request to server again. Which means, it will execute whatever it executes, put again that attribute on request, and you will get it back in the JSP page.
If you want to avoid that, you should have a way to tell, on the server side, that the operation has been already executed in order not to put the attribute on the request( for example, if you insert something, always check before if is not inserted already, and if so, just don't put that attribute on the request).

Submitting the form after receiving ajax response

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.

Forwarding the request from one jsp to another jsp with all request parameters?

i have this scenario. User enter some stuff on jsp form in browser and submit. In servlet i process the request and show the jsp page1 to client which has just continue
button. Now on click of continue, i want to forward this request to another jsp page2 with all request parameter present on page1. basically i want to get all request parameters which were present in first request on page 2 also? I am not getting how should i go
for this? I dont think i can use jsp forward as per my understanding it would work only when both when we want to forward from jsp (on server side) not on client side?
There are two ways to implement that:
output all parameters in hidden fields, and submit them in the 2nd request
store everything in the session
Following are a pair of ways to achieve this
Put the information in session object in first request and access it from session object in second.
On the intermediate page displayed, have hidden form elements which will carry the values and send it back on continuing.
You can use Servlet Forward to pass all request parameters to another JSP or Servlet:
getServletContext().getRequestDispatcher("/yourJSP.jsp").forward(request, response);
Servlet forward will forward the existing request to another JSP or Servlet, so all the request parameters and attributes will be available to the destination JSP or Servlet (The reason you can use the Servlet forward is that JSP is also a Servlet after translation).
You may find following resources useful:
Pass data from servlet to jsp
Servlet Redirect vs Forward

Pass parameters from one jsp to another using post method

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).

HTML page as AJAX response?

Can I send an entire HTML page with an AJAX response? If so, how to render that HTML page and what are the pros and cons doing that. The reason why I am asking this question is I found out that if we use response.sendRedirect("index.html") as a reply to an AJAX request in servlet we get the indx.html as AJAX response XML.
Your question doesn't make much sense and I can't quite tell what you're asking - the response to an ajax request will be whatever the server sends back. It could be plain text, XML, HTML, a fragment of an HTML/XML document etc. What you can do with depends on your script. If you're using a library like jQuery, what happens on the client side and what you can do with the response can also depend on how the library interprets the response (Is it a script? It it HTML/XML or JSON?).
if we use response.sendRedirect("index.html") as a reply to ajax request in servlet we get the indx.html as ajax response xml. Can some one pls explain this
An ajax request will behave much like a 'regular' HTTP request. So when you send back a redirect from your server (HTTPServletResponse#sendRedirect), the browser follows the redirect just like it would for any other request. If your ajax request was to a resource that required HTTP BASIC authentication, you'd see a prompt to login, just like you would if you visited the URL directly in a new browser window.
If you want to send HTML as a response, because you want to update divs, tables or other elements, but still want to use the same css or javascript files then it can make sense.
What you can do is to just send it as plain/text back to the javascript function, it can then take that and put it into the inner html element that you want to replace, but, don't do this if you want to replace the entire page, then doing what you want is pointless.
When you make the http request for your ajax call, it has its own response stream, and so when you redirect you are just telling the browser to have that http request go to index.html, as #no.good.at.coding mentioned.
If I get your question, you just want to know whether you could return whole entire HTML with AJAX and know the pro and cons.
The short answer to your question is yes, you could return the entire HTML page with your AJAX response as AJAX is just an http request to the server.
Why would you want to get the entire HTML? That's confusing to me and that is the part that I am not clear about. If you want to want to render the entire HTML (including tags like html, body, etc?), you might as well open it as a new page instead of calling it via Ajax.
If you are saying that you only want to get fragments of HTML to populate a placeholder in your page via AJAX then this is an acceptable practice. jQuery even provides load() function (http://api.jquery.com/load/) to make that task easy for you to use (check the section Loading Page Fragments).
Using this method, you could populate the placeholder using the HTML Fragments that is dictated by your server logic (i.e when the login fails or succeed) including the one via server redirect in your question.

Categories