JSP with conditional checks is static or dynamic - java

If I am going to include JSP if condition tag or any Conditional check tags will the JSP is static of dynamic. If I have a dispatcher configured for my webserver. Will the JSPs will run on each request or will the HTMLs that are rendered with the JSPs will be cached?

I think that the conditional tags are what make a page dynamic, because if it only is plain text will display the same thing over and over, but adding conditional statements make the page react to the user.
The JSP will be rendered on each request, the cashes are as simple HTML file in your browser's cache.

Related

Asynchronous JSP Tag

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.

GWT/GXT adding nocache.js file, in the header or after the body?

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.

render xhtml file from servlet

I wonder if there is a way to load, interpret and render a .xhtml file from a servlet.
What I want is to have a xhtml file witch contain for example a custom row within a list, to be loaded from a servlet or a bean, to be interpreted, and to obtain the html result.
Sounds to me like you're not in need of Servlets or JSF, but rather JSP (should look familiar, coming from PHP).
Notice that people here will tumble over themselves telling you to not put the logic into the page (and for good reason), but I think with your background, you'll be helped by starting from a JSP only approach. Than start moving the logic to a servlet. You can use a number of techniques to communicate the data between the servlet and the jsp. I like to use
request.setAttribute("someString", valueObject);
In your jsp you than can use:
<%= request.getAttribute("someString") %>
Calling your jsp from the servlet is done via
RequestDispatcher rd = req.getRequestDispatcher("/path.to.your.jsp");
If you really need a .xhtml file (your original question), you can use this last line as well, but it'll be real xhtml, like it would be served from a plain webserver (!= appserver)

How to render HTML form fields and run Java code in background simultaneously in JSP

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.

Find if JavaScript is enabled/disabled in JSP

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.

Categories

Resources