Including the JQuery file in JSP confusion? - java

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.

Related

JSP Template in other file - Spring Boot

I'm making a web application in spring boot. I have a lot of views created in JSP, but every view has the same footer and the same menu. Of course I can just copy all menu and footer code and paste it to 30 JSP but I would like to do somethink like this: I have file in which I have something like that menu="HTML CODE WITH MENU", and then in JSP in body section I just put {menu}, and all HTML code is put inside JSP. When I want to change my menu I will just change its code in "menu" variable/string or whatever it will be, and menu in all JSP will change. I tried a few solutions for example with include but it doesn't work. I search scritly the solution I have mentioned. Has anybody got any ideas how I can do this ?
Since you are using JSP you can use the standard JSTL <c:import> tag as explained here. The other options would be to use <%# include #> or <jsp:include>, either of them will work for local files:
<jsp:include page="footer.jsp" />
<%# include file="footer.jsp" %>
<c:import url="footer.jsp" />

keep a page without reloading with request everytime

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

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