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.
Related
I want to make the js and css files which are modified are to be downloaded at the client end when a page is accessed. I have these approaches
Manually add the modified timestamp the URL in each page.
I was thinking of writing a scriptlet code in all the jsp pages which will read all the js and css files modified timestamp and append it to the url in the page.
Add the modified timestamp while building the war file using ANT.
I have following questions.
Can any one let me know which would be a better solution of the above approaches? I am open to any other solutions also.
I went through this answer on SO and using it I can get the modified date but how to change the jsp file?
Is there anything similar to this in java?
In this situation better or best solution is took shape according to your exact requirements. I might derive simple questions like; Will your static resources in same server or included in your app in same server etc.. May be some other better ways..
I don't have ant experience so I can't talk about it now , but you can go with java way already.I want to share just idea/s. A filter(looks the .css or .js requests , gets resources and look resource lastmodified date or checksum return as version on response) or custom jsp tag will provide your requirements. Write a custom jsp tag <resource:static path="app.js"/> like that example. So it may look specific file's last modified date, assumed under the same document root, and it can produce <script type="text/javascript" src="app.js?version=8637"> like this result, so this result will bust the cache.
I have never before heard of Thymeleaf. Now I have received a set of Thymeleaf templates. An HTML page is assembled from several of these files with th:includes and th:replaces. I wish it to happen, so I could see the page, but I don't wish to write Java code to run all this.
In other words, I just want th:includes and th:replaces to be processed, and th:text and other instructions ignored, so I could see prototype page assembled from templates.
Is it possible, and if so, how?
Thymeleaf is the view layer, you still need the Model/Controller to be able to drive the manipulation of it. Without java code to enact and to activate the relevant logic for th:include and th:replace you won't be able to manage this at this stage.
I am currently teaching a software engineering class where approximately 275 students are learning JSP. I would like to be able to provide the students with an online service that enables them to practice their basic JSP skills. The idea is that students will provide some JSP code to pass some pre-determined tests.
For example: The students will be asked to provide the code for a JSP page that would return "Hello NAME" where NAME is a GET parameter that has been passed in to the page.
A correct response to this question would be.
Hello <%= request.getParameter("name") %>
I can currently test student submissions like this using a Linux-based solution where I write the submitted student code out to an existing JSP file, fetch the JSP page while passing in GET parameters, wait for Tomcat to reload and compile the JSP page, parse the HTML result, and return the test results to the students. This process is rather slow (overwriting JSP file on disk) and does not scale up well.
I'd like to be able to run this JSP "verifier" on the Java-version of Google App Engine which can be configured to scale up automatically when lots of students are practicing at the same time.
My question is, how can I evaluate submitted JSP code in Google App Engine without being able to overwrite the contents of an existing JSP file? Can I pass the code that I want to run to a JSP page and have that page return the results?
I can not figure out out to have the page use the passed in code to do something with the passed in GET parameters. In short, I'm missing an eval() method.
Any ideas?
I've never tried this, but I'd look at the Jasper Compiler (Tomcat's implementation of JSP). It can generate a class file from a .jsp, which you may be able to load from GAE. Though I could also imagine the required effort to get it working might be too much.
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.
I'm in charge of updating an existing java app for an embedded device (a copier).
One of the things I want to do is create a servlet which allows the download of all the files in our sandboxed directory on the device (which will include the application log files, local caches, etc). At the moment these files are all in a single directory with no subdirectories.
Basically what I'd like to do is as follows:
Log.log
Log.log.1
Log.log.2
SomeLocalCache.txt
AnotherLocalCache.txt
where each line is a clickable link allowing download of the file.
However, my HTML experience is basically nil, and my familiarity with the Java API is still fairly rudimentary, so I'm looking for some advice on the proper way to go about it.
I've read through all the samples provided, and here's what I'm thinking.
I can create a servlet at a specified URL on the device which will call into my code. Let's call this /MyApp.
I add another link below that, let's call it /MyApp/Download.
When this address it reached in a browser, it displays the list of files.
This list will have to be created on the fly. I can create an HTML template file and put it in the res folder (this seems to be the recommended method for the device in question), but the whole list of files/links will need to be substituted in at run time. Here's an example I found using <ol>+<li> tags for the list and <a> tags for the links. I can generate that on the fly pretty easily. Is that a reasonable way to go?
e.g.
<ol>
<li>
Log.log
</li>
<!--more <li> elements-->
</ol>
Clicking on an individual file will link to /MyApp/Download/File.ext which will then trigger the file download via my servlet (I've found this code which looks promising for the actual download).
The device will require users to log before they are allowed to access the /MyApp link or any sub-links, and I can additionally require that the logged in user be an admin before allowing file download, which together seems like sufficient security in this case (heavy security is not required for these files).
So am I missing anything big or is this a reasonable plan of engagement?
EDIT
Judging by this link when to use UL or OL in html? Many people are going to hammer the answer and comment below because they say it is important to put semantic information into the HTML.
My point is simply this -- the only difference is browsers will display one with bullet points (as OP seems to want) and one with numbers (as the OP does not want.) I suggest he change the HTML to the way he wants it to render, or leave it as is, and make some CSS changes.
Yes there is a semantic difference between the two... they will both still render in order as defined here http://www.w3.org/TR/html401/struct/lists.html Is the HTML the place to put semantic information? I think not, code that generated the HTML is the correct place. Your cohesion may vary.
I won't change my original comment for the sake of history.
END EDIT
Seems fine to me -- however <ol> is not really used any more, I'd go with <ul>. Don't worry, it is still ordered as you would expect.
The reason for this is the only difference between the two was browsers would automatically number (render with a number before) ordered lists. However, with CSS all the rendering control can be in the CSS (including numbering) and everyone is happy.
Hardly anyone uses the auto number anymore. In fact via CSS, lists can and are used for all sorts of crazy things, including CSS menuing systems.
Here's a summary you need to do:
You can use File#listFiles() to get a File[].
You can use JSTL c:forEach to iterate over an array.
You can use HTML <ol>, <ul> or <dl> elements to display a list.
You can use HTML <a> element to display a link.
You can use a Servlet to write an InputStream of a local file to OutputStream of the response. Remember to pass at least Content-Type, Content-Length and Content-Disposition along.
You can make use of request pathinfo to pass file identifier safely. E.g. map servlet on /files/* and let link point to http://example.com/files/path/to/file.ext and in the servlet you can get /path/to/file.ext by request.getPathInfo().
A basic and solid servlet example can be found here: FileServlet. If you want to add resume and compressing capabilities, then you may find the improved FileServlet more useful.
That said, most appservers also just supports directory listing by default. Tomcat for example supports it by default. You can just define another <Context> in Tomcat's server.xml with a docBase of C:/path/to/all/files and a context path of /files (so that it's accessible by http://example.com/files.
<Context docBase="/path/to/all/files" path="/files" />
That's basically all. No homegrown code/html/servlet needed.