So in eclipse when you generate a .jsp file it automatically includes the following top line:
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
Is there a way to also include javascript code so that I can have some of the file written in java and some written in javascript?
Usually I include my JavaScript in JSP like this:
<script type="text/javascript" src="js/script.js"></script>
This way I can minimize the js code and also cache it differently than JSP files in Tomcat.
The answer is Yes, you can add the js in jsp. And this also has benefit. It helps you to use the values stored in request and session, because the js code which contains the jsp stuff in jsp will be compiled while the *.js file will not. And also you should keep in mind that you'd better put the js code in .js file not in jsp, so the browser doesn't need to load it repeatly and code seems more clear, also good for debug. Another answer has give you the example code to import js file, so I just ignore it.
Related
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" />
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.
My assignment is to create a class in java and add tags through comments to make it look just like this http://docs.oracle.com/javase/7/docs/api/. My problem is though that the CSS doesn't stay with the html file. It comes out without the CSS. I've tried doing
<style>href="http://docs.oracle.com/javase/7/docs/api/stylesheet.css"</style>
but I can't get the CSS to stick. What am I doing wrong?
This is the way you link css files in html
<link href="http://docs.oracle.com/javase/7/docs/api/stylesheet.css" rel="stylesheet" type="text/css">
Did you add the lines as ususer2812693 mentioned. Just check whether, the css is file loaded into the browser using firebug. And also the css classes and ID s are correct with html elements' classes and ID s. Or else, first try to download the css file which you refer and, try to call it locally from your html page.
I have a working servlet code, which processes some command prompt commands and I have an editor code written in html, now I want to add this html file to servlet. How do I do it? tried moving the html file to WEB-INF.
I've looked into this link on How to integrate HTML design into Servlet? but everything seems damn cumbersome as I have a very large html file.
Any other fixes?
If your editor.html contains only static JS, CSS and HTML (you don't depend on the data provided by your servlet), you can simply forward your servlet into JSP and include your html page from it using
<%# include file="editor.html" %>
i have CSS file in one folder,html file in one folder,javascript in one folder. where shoul i link these files and how?whether in html or jsp page.please let me know.. even after giving a link like
where CSS is my folder.
how a proper aggregation is needed in in eclipse considering the industry standards ?
You link the files on your html page in the head of the document.
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="../css/main.css">
<script src="../js/main.js"></script>
</head>
...
Some people recommend linking javascript at the end of the document (just before the closing body tag) so that the visible elements can be rendered first before the javascript is parsed.
Just to clarify, css must be in the head; javascript can be anywhere and possibly/probably optimal at the end.
use link element to link various resources to your page like: CSS files, a favorite icon ...
check out this video demo
and script tag (as shown above by Crazysheep) to link scripts