JSP include prefixes the project name in the link - html

I have a html file in a share folder (ie, out of web server location) and I have to include it in a jsp using
<jsp:include page="//TestFolder/Sample.html">.
While running the application the following error appears on the page:
The requested resource (/projectName//TestFolder/Sample.html) is not available
Here the application name (/projectName) is being prefixed with the target path. How to get rid of the application name so that the html can be included within the jsp?
Any clarifications would be appreciable.
Thanks.

The jsp:include action can be used only within the same servlet context. It accepts only relative url(either page-relative or application-relative)

Based on your comment, what you are trying to achieve is not possible with jsp inclued tag nor jsp include directive. This is because, at the end of the day, all JSPs become servlets. And a servlet with dependency outside of it's WAR (or any other packaging) would be quite tricky, now, wouldn't it?
I imagine, however, that you could create your own tag that would dynamically read a static HTML file and include it's contents in response. Just put into account that bypassing this limitation will put your application at risk of this HTML not being available unless you prepare your tag for that.

Related

ASP.NET Core read HTML email and display in webapp

I am working on a custom email webapp for my company. I have browsed the web and can't find an exact answer. Maybe I don't know what to "google" for exactly. I have never done anything email related.
In our database we are saving emails as a string. It contains everything.
<DOCTYPE><head><body><style> etc. How would I go about displaying this in my webapp.
I tried just pasting the HTML in a <div> inside one of my components but the styles would not load properly.
How would I go about reading/parsing the raw HTML in the string to then display it in my webapp. Is there a nuget package anyone recommends.
I am using Blazor on .net core 5.
As you may have found, putting an email HTML body into an existing web page will almost result in CSS pollution as the HTML may have styles (or may use styles from the web page).
My solution to this was to load and display the email HTML inside an <iframe> tag as this isolates the email from the page very effectively. The URL for the IFRAME is separate view on the server, which just returns the raw email HTML (remember to add security to this if needed in your app).
There are probably better ways to do this in CSS now without having to resort to IFRAME.

HTML reference an image using extension only without using explicit image file name

I have a question about referencing image in HTML. I have a single snippet of HTML code as
below, where I need to reference an image in a folder called static. The image has an extension of SVG
but it's name is dynamically created. If there a way for HTML to refer to this image by only referring to the file extension? The code below using a wild card doesn't work.
<p>
<img src="/static/*.svg" width="1000">
</p>
Additionally, can we add a logic in HTML such that if there is no SVG file in static folder, we don't render it; if there is one, then render it.
Thank you.
I don't think it's possible to do what you're asking with just HTML. However you can easily do this by adding an id to the tag and applying the attribute based on your condition. There are plenty of examples on the web on how to do this including one already answered here: Javascript set img src
This is possible with a server that is designed for this purpose. There is not a feature of just html that will do this, however. If you don't control the server on the backend, you probably can't get this to work, as it most likely will require custom backend code.
On the backend, you make a simple static html server that will match file patterns, and determine and serve the best match. You can do this any number of ways, and if you look up "how to make a static http server" for your favorite backend language, you likely will find an example to get you started, like these:
https://www.digitalocean.com/community/tutorials/how-to-create-a-web-server-in-node-js-with-the-http-module
https://blog.appsignal.com/2016/11/23/ruby-magic-building-a-30-line-http-server-in-ruby.html
https://stackabuse.com/serving-files-with-pythons-simplehttpserver-module/
You would then have to modify whatever base example you chose with your custom pattern matching code. Your server could be designed to do whatever you wanted with the request you send it, including the scenario you described.
This works because an img tag like the one in your example causes the browser to make a GET http request to your server, passing on the url in the src attribute. So, if you control that server, you can have it respond in any way you want, including treating specific characters like * specially.

How do you work with include pages in jsp or php or the likes without getting IDE / Editor warnings for CSS classes?

If the CSS files for your site are referenced in the parent page, obviously you can use those CSS rules and classes in the sub-page or "included" page (like a jsp include or a php include). That will run as expected in the browser. BUT, if you are using an IDE or smart text editor of some kind (I'm using Netbeans), you will get warnings about the CSS elements in the sub-page (a .jspf for example) unless that file has a redundant reference to the css files. Is there a work-around for this? I don't want to have to reference the CSS files in both my jsp and my jspf (jsp include).
One technique I've used is to abandon jspf files in favour of a templating system where if you want to include something from a template, the template is actually a full page of which part is marked to be included. I actually use a home-grown template system for this, but my understanding is that thymeleaf (http://www.thymeleaf.org/) offers the same feature.

Can JSTL be used in HTML page?

I have run JSTL tag on JSP page but could not run the same code on HTML page. So, if its possible can anyone help me on it!
As name it self indicates
The JavaServer Pages Standard Tag Library (JSTL) is a collection of useful JSP tags which encapsulates core functionality common to many JSP applications.
And if you see the wiki of JSTL
It extends the JSP specification by adding a tag library of JSP tags for common tasks,
Summary:You cannot use.
You can't, they only run on JSP pages. Sorry!
Not Possible to do this.It supports only JSP.
Any JSP file is recompiled is when the file is modified and the web container detects it, but definitely this is the wrong approach... What about saving HTML templates in the database and process them before JSP receives them?

Why would JSP tags appear in HTML source code?

I'm a front-end web developer at a company using Java on their server. As a front-end developer, I'm concerned with the HTML structure that the server produces, but I don't have control over anything our back-end team produces. Rather than ask someone on that team, I would like to gather knowledge from the Stackoverflow community so I can communicate intelligently with the back-end team. So, I am curious as to what would cause certain JSP tags to appear in the rendered HTML that is sent to the browser. We have tags in our HTML source such as:
<flow:fileRef id="vfileColor" fileId="vfile.color"/>
<flow:fileRef id="StyleDir" fileId="StyleDir"/>
<flow:fileRef id="vfileStylesheet" fileId="vfile.stylesheet"/>
I am more interested in knowing why these appear, not as much about what they do. Is there a server setting for Tomcat/Apache/etc. that would hide these tags from the browser? Any information would be helpful. Thanks in advance.
They will appear in the generated HTML source if the associated taglib isn't declared or its URI is wrong.
In this particular case with <flow:xxx> tags, you should have a
<%#taglib uri="a/valid/uri" prefix="flow" %>
in top of the JSP page(s) in question (even if it's only to be used as an include file). If you can't figure the right URI, then you should consult the taglib's documentation for the right one or extract the JAR file of the taglib and read the tld file.
You should also ensure that the JAR file containing the taglib classes and the tld file is been placed in the runtime classpath of the webapplication, e.g. in /WEB-INF/lib.