keep a page without reloading with request everytime - java

I have an java web application.
The home page is home.jsp .This page contains internally three jsp pages to load one is menu.jsp
main.jsp
footer.jsp
but when my application is loading every time it gets slower by menu.jsp as it contains all menus.
so i now need to load only main.jsp for every request so that it will become fast how can i do it.

You can use AJAX to asynchronously load some part of the page, but this is not the perfect solution, you need to check why the menu is slow, there is no excuse at all, solve your problems from the root not just paint over it!

Put all menus in with separate id and use id.innerHtml to write the the contents

Related

How to read from file on page load using jsp?

I have one webpage and a file on a server. How to read from file on every page load. I m using jsp. Is there any function available to check page load?
Every page load means that you come to server every time (cache is another story).
So, jsp is loaded from the server every time and here is simple directive to include file to jsp:
<%# include file="foo.html" %>
Keep in mind that server knows only about jsp changes but not about foo.html changes. So, if you change only foo.html server doesn't know about it. That's the reason why this approach is not common. It is used mostly for common templates and parts of all pages (like common footer) even there are other better modern techniques to do so (like CSS).
However, if you still want to use external file which constantly changes just remember that JSP is Java too and you can use whatever you do in Java (except it is not recommended - JSP should be simple viewer in MVC).
So, something like this will work:
<% out.write(Files.readAllBytes("foo.html")); %>
You can use any techniques to read file and write it to the response output.
Addition for your comment:
Text field is regular html. Input to it would be like this:
<input name=abc value="<% out.write(Files.readAllBytes("foo.txt")); %>">
but, again, please consider more modern techniques like DHTML, AJAX, CSS or simple JavaScript.

Including the JQuery file in JSP confusion?

I have a JSP page called main.jsp and the jsp page has three iframes.
Each Iframe loads an individual JSP(page1.jsp , page2.jsp , page3.jsp) and each JSP uses a individual JS file consists of JQuery code.
Currently I included the Jquery 1.9.1.js in every JSP page ( main.jsp and it's iframe laded JSP's ) and my project works fine .
Is this the proper way , Because I included the Jquery 1.9.1.js in every JSP .
How I can load the Jquery 1.9.1.js in my main.jsp alone and make visible to all JSP's .
I don't want to include the Jquery 1.9.1.js in every JSP's . I need the Jquery 1.9.1.js to be centralized.
Is this possible ?
Hope my question is little clear and understandable.Please don't hesitate to edit or ask questions.
I dont think it is possible when you use iFrames.
Since each iframe src will load the new page and it runs in its own window context
Its not easy to use a single jQuery.js to control the behaviour for the controls inside iFrame.
So you did correct.
Still if you want, you could use different <div> and load the content into it. This way one jQuery will work, since all the divs are in same window context
While using iframes you have to add jquery in each page.Each page in iframe is considered as child.If you were using divs in place of iframe then using centralized jquery1.9.1.js would have worked.
I think you should make templates Header.jsp and Footer.jsp. Then included the Jquery 1.9.1.js on header or footer.
Then you can use header or footer on your main.jsp and on every page.

Does the separate call goes to server to render the image in img tag on jsp file?

I know the how to show th image on jsp file. But never thought how it works? I mean Does image get rendered with html page right at the time when jsp page
is evaluated to html content by webserver and transferred to browser as bytes along with other html or it does not happen this way.
I did discuss with my collegues but they were not sure too. One of them told me that when you request any JSP page from server, jsp page is evaluated
to corresponding html content and images are not rendered at this point of time.So when browser gets this html page and see the tag like below,browser
makes separate call to server to for each image.Is that correct?If yes ,if there are 50 images on jsp page, will 50 request go to server to download the
image. He also mentioned not only images but javascript also included in JSP this way only?
I am not sure when and how the image included in jsp page is requested? Could not get this fact cleared
thru googling too. T
src="getImage.jsp"
The question came in my mind because on change of some value in dropdown, i want to to change the image . I thought i could do it
on client side. But if go by the approach mentioned in the last, looks like image has to be downloaded from server first.
It's very hard to tell what you're asking. If you mean, does an image you include in your JSP page via an img tag (e.g., <img src="/path/to/image.jpg">) somehow get "baked into" your JSP page when it's compiled into a servlet by your JSP container, the answer is no. The browser will request the JSP page, get back HTML et. al., and then request the image.
If you want to change images based on a dropdown I'd suggest using javascript.
First load all the images in javascript objects (on loading of the page)
then in the onchange event of the dropdown change the image.
googling for "preloading images javascript" should provide ample examples
returning an image from a jsp file is also possible
Write code in your jsp that writes the byte stream of an image to the jsp writer, make sure you set the mime type correctly.
Images are downloaded exactly as all other resources do, and get rendered by your browser.
If you ask if 50 instances of the same image in a single page will be downloaded 50 times, the answer depends on the HTTP caching policy headers for the particular image resource - if they allow a resource to be cached, it will get cached by your browser and will be downloaded over the wire only once.

How to search the correponding JSP file of any web page/HTML page?

I have a web application . When i right click on any html/webpage inside my browser , for some pages it shows the excaxt jsp page like
http://localhost:8080/MyWebApp/vustomer.jsp while for some pages it shows action class instead of jsp page like
http://localhost:8080/MyWebApp/TellerAction.do?actionCode=3&page=controlPanel. I am not getting what is reason behind these two different
behaviour? i mean why it does show jsp file name for some page but not for others?
I guess because HTML code of page sometimes has <a href='...jsp'>...</a> and other times it has <a href='...do'>...</a>.
Your best bet to find out why is to ask author of code :)
Usually, .do URLs are served by servlets, not jsp.

Persist the dynamically added tags after refreshing JSP

My application is in Spring and using .jsp for view. I have created some tags dynamically using jquery. Now if i refresh the page my all the dynamically created tags removed from page but I want to persist the dynamically added tags after refreshing.
There can be many ways for this. One way could be like this:
Whenever you dynamically create your tags using jquery, you also save them on server using ajax. jquery makes ajax interactions really convenient.
When you load the page on browser, make sure that you are fetching your dynamically created tags from the database and displaying them on your JSP.
This way your tags won't be lost if you reload your page.
You have to get the values from the database as an object and use tags from JSTL to display them in the JSP page. This way they will be displayed after refreshing too.
Hope you find this answer useful though its a late reply.

Categories