I have a web page where a part of the page is loaded via AJAX,
but I also wanted to make it work if JavaScript is not enabled.
so, i wrote the code as
<script>
fetchCitiesAndDisplay ();
</script>
<noscript>
<%
out.print(Cities.getHtml ());
%>
</noscript>
where
fetchCitiesAndDisplay ()
is a javaScript Method, which will get the content (executes the code in Cities.java) and displays it.
Cities.java
is a Java class which will collect the cities from database and generates the html content to be displayed.
When JavaScript is off, everything works great.. as the ajax call will not be executed and only Cities.getHtml () will be called once.
but, when JavaScript is enabled, The Ajax call is also executed and Cities.getHtml () is also called.. even-though the display looks right because of tag, Cities.java will get the call twice for the same data, and it eats up a lot of time...
How to solve this?
I'm wondering if maybe you don't understand how <noscript> works. It doesn't come into play in your server-side template so whatever server-side logic you put in there will ALWAYS execute whether javascript is ultimately enabled or not in the browser.
Therefore, this doesn't make a whole lot of sense to me because the HTML inside <noscript> is always going to be generated whether javascript is on or off. It will always be in the page. So, if it's always in the page, then why fetch it via ajax when javascript is on?
i have to agree with #jfriend00, its not possible to identify if javascript is enabled using 1 page
You can use javascript to set a hidden variable with some value, when the page submits you can check for it and identify if javascript is enabled or not.. but this requires 1 request/response cycle, may be you can have an interim page which forwards the request to the intended page (with the hidden variable i discussed above).
Why not have your javascript code load a a tiny html/image/etc snippet on page load. If javascript is enabled, you can see that file was requested. If it's disabled, there will be no request for that file.
Related
Is there a JSP tag that allows that part of the page to process asynchronously? If not is there a way to create one?
Something like the following:
<Util:Asynchronous>
<%
DataSource source = ...
%>
</Util:Asynchronous>
... rest of page
I don't want to have to wait for the data source to finish before processing the rest of the page.
Thanks.
You need to use AJAX. Also it is bad coding practice to put your data access commands in your JSP pages. Very susceptible to hacks, not to mention performance hits.
The servlet container must wait that you are page is entirely rendered before sending it back to the client. Thus your requirement makes no sense for me.
If a part of the page takes too much time to process, you could use Ajax to load a portion of your page. There is already pages at StackOverflow about Ajax.
I notice that most GXT/GWT applications put the nocache.js file after the body tag. And few seem to put in the include in the header tag. Why is that?
Given the fact that the GWT script tag will be evaluated synchronously (the tag), but fetched asynchronously (the code, into an iframe), I don't see why not put it as the very first thing. Time saved!
Unless, you have some kind of complex logic that cannot have the chance to be properly displayed before the onModuleLoad() call (e.g., images evaluated but still not fetched), much like Steffen Schäfer pointed out. But you can defer you app startup for them though.
For more info, have a look here.
From my point of view there are 2 cases:
If you use GWT to only enhance your page that is generated on the server side then put the <script> at the end. That allows your browser to render the initial content of the page before parsing the JS code.
If you built a single page application that is completely generated by GWT on the client side, there's no content to be initially shown. In that case you can put the <script> to the head.
Be aware that 1. also applies if you implemented a loading animation or placeholder content to be initially shown.
I have made a JSP page that has form fields and java code in scriplets .I have imported the java code in JSP page and made object to call the function of that java class.
When i execute the JSP, the JSP page remains blank and only when all the Java code has been executed then the html form fields are rendered on the view.
I have two questions:
1.)How can i quick this process and how can i show something like "Loading..." until that html is rendered
OR
2) How can I make HTML fields to appear as soon as JSP page is invoked while letting all the java code simultaneously running in background.(The java class whose object is made in JSP calls webservices and this may take time, meanwhile i want user should start inputing in the form fields).But user cannot see any form fields until the whole Java code has executed.
KINDLY HELP!
You can display a page first and then to load other content through Ajax.
AJAX is a very powerful tool so I am struggling with it :-).
Is there any way or API(in java) so that I can get the HTML code which is generated by AJAX?
Generally, AJAX make use of inner HTML code and hence this inner HTML code is missing when I look into the page source of a page.
e.g click here
Just see the section OTHER NEWS. The content is populated by AJAX. When I look into the page source the code is not there.
I need this HTML code through a java program. How can I get it?
To have a Java application use the content received via AJAX, you need to first find the URLs from where the content is getting called from. In case this it would be http://itm2083.com/get_wwo_content.php?featureGroupId=8355&featureDisplayLimit=1&sponsorName=vortalx&wwoDivCounter=5&domainUrlForWWo=http://item2083.com/&featureImgDisplay=FLAG_TRUE&featureGroupImageWidthLimit=200&featureGroupDefaultImageUrl1=http://wwo.itmftp.com/75x75.gif&featureGroupDefaultImageUrl2=http://wwo.itmftp.com/75x75.gif&featureGroupDefaultImageUrl3=http://wwo.itmftp.com/75x75.gif
The featureGroupId= parameter has 5 IDs: 8355, 8359, 8367, 8369, 8429. Use these to pull the content from the Other News box.
The featureDisplayLimit= parameter determines how much content is pulled from the server.
If you want the nice HTML as well, the Java app will have to recreate it, as the HTML rendered on the site is created by JavaScript code.
As title says, how do I call a java class method from a jsp, when certain element is clicked (by example an anchor)? (Without reloading the page)
If this can be done, how I pass the method, some part of the html code of the page that invokes it?
Im using jsp, servlets, javascript, struts2 and java, over Jboss AS.
What you want to do is have javascript fire off an AJAX request when the said element is clicked. This AJAX request will go to the server which can then invoke any java code you want.
Now you can build this all yourself or you could use one of the many off the shelf solutions. I would recommend Googling around for a JSP Ajax tag library. Like this one http://ajaxtags.sourceforge.net/ .
As Marko pointed out, you might need to read some more about the client/server separation in web programming. If you want a framework to help you do remote Java invocation from Javascript, have a look at DWR.
You can use a Ajax call to do it .
Now when the HTML object is clicked call a java-script. Then in the JavaScript make a Ajax call to the servlet something like this
$.get("Query?ID="+id ,function(RespValue)
{
}
Here Query is my servlet mapping defined in web.xml and Id is the parameter i am passing you can sent multiple parameters too. and RespValue is the response value returned from the servlet.
In the servelt write a do Get method and execute your java code. If you want to return some value use the function(RespValue) else remove it.
You can't execute server side java code in client browser.
What you can do is to perform new http request that will perform some action on server and return the action result.
Given the tone of the question better go read some JSP tutorial. No forum post answer will explain it better.