My angular application is bundled and deployed together with a java application. Angular AOT build is placed in the folder WebContent/app.
While taking the AOT build,
If I specify --base-href /app, I'm getting 404 after loading the index.html. In that case, I have to add --deploy-url /app/ during my build, for the application to be working properly.
But If I specify --base-href /app/ then scripts, styles, and other resources are served correctly. And there's no need to specify --deploy-url
What I could observe is that If I don't append "/" to base-href, the server request made is using the context-root - http://localhost:9080/application-name/styles.***.css and when I append the "/", server context-root is appended with app - http://localhost:9080/application-name/app/styles.***.css
Why is adding "/" at the end of base-href make all this difference?
Could someone explain this behavior as I'm not able to find anything on docs?
Thanks in Advance.
After some digging, I found the answer. It is not related to Angular at all.
Tha <base> tag is part of HTML Specification: Link
As MDN Doc says,
The HTML < base> element specifies the base URL to use for all relative URLs in a document. There can be only one < base> element in a document.
It is used to specify the base of relative URLs.
So coming into my problem
When <base href="/app"> is used, the browser treats the href as a file rather than a directory. So the base URL will not be prepended to relative URLs.
When <base href="/app/"> is used, the browser treats it as a directory and all relative URLs will be prepended by base URL.
More can be found out here and here
Related
Files is stored in this location.
D:/uploads/component.png
This is the HTML code to download a file.
<a target="_blank" href="../{{att.filePath}}"><strong>{{att.fileName}}</strong></a>
But when I click on the link it opens up as in this path, which is wrong as it is not in the server.
http://localhost:9190/D:/uploads/component.png
How can I see the file in web browser, what am I doing wrong here.
Though you are not showing your variables' contents, it seems your variable contains the absolute path for the file. Therefore you don't need to add relative parts (e.g. ../) to the path.
Also, if you want to host a file for downloading, you must make it available inside the hosted directory (document root). You can achieve this in multiple ways:
A) Simply copy the file/directory into the document root. Then you can also use relative links if you wish (you have to change the variable!).
For example, you may have a structure like this:
public_html/
uploads/
component.png
index.html
Then you can link the file in index.html using the absolute path /uploads/component.png, or using the relative path uploads/component.png.
B) Create a symbolic link to the file/directory inside the document root.
For example if you create a symbolic named uploads in your document root for your D:/uploads/ directory, you will have this structure:
public_html/
uploads/ -> D:/uploads/
index.html
This way you can still have the file physically at D:/uploads/component.png but it will be available also in public_html/uploads/ and you can use the same paths as in method A).
C) If you are developing a webapplication (it seems so, as you tagged Java and Tomcat), there is another option. You can define a controller method which maps requests like /uploads/* and implement it so it will read the file specified by the URI from your D:/uploads directory.
I deployed application in tomcat under webapps/testing1 folder.
http://localhost:8080/testing1/index.html
This html page has anchor tag with href as "/login"
When I click on anchor, it takes to localhost:8080/login instead of localhost:8080/testing1/login
I did not do any change in tomcat conf xmls. How can I make all paths starts with slash would goto application context?
Suggestions would be appreciated.
By declaring a link href="/login" you make that an absolute link on your host. If you want login inside your app you'd have to prepend the context path (request.getContextPath ()) or use relative links ( href="login" )
If you deployed context successfully in Tomcat and it is Start up successfully. Then the Application access directly the web.xml from the WEB-INF folder.
It access the servlet using the <url-pattern> and <servlet-class> to identify the servlet.
If you are worrying about accessing HTML files from the servlet you can access them directly from the context path.
i.e
if file present at CONTEXT/html/file1.html you can access it as html/file1.html
if file present at CONTEXT/file2.html you can access it as file2.html
You have to specify the . prefix to the href value.
<a href="./login"> so that the URL will be localhost:8080/testing1/login
I am making a web application using jsp and servlets .But am facing two problem that i have no ides how to remove :
Problem 1 : I am creating new folders in my WEB-INF folder .But what i want is that instead of giving full paths .I just provide relevant path Like :
File tempfilesstore = new File("C:\\Users\\admin\\Desktop\\SharedCrpto1\\web\\RetrievedFiles\\"+fileid+"-"+personname);
if(!tempfilesstore.exists())
tempfilesstore.mkdirs();
Can this full path be avoided as only path from web folder of the application is required.
Problem 2 : I keep a image in this folder by performing some operation on original image being browsed by the client on browser.
Now when i see the image in folder then it is present their But if i try to see the same image in browser it does not display the image .When i refresh my page for 3-4 times than sometimes it get displayed and sometimes after manually opening it by going to specified location.What can be reason for it ?Please help.
Here is how am trying to get image on browser :
<img src="RetrievedFiles/<%=path%>/<%=sharedfilee%>" alt="Image Preview Not Availablee" width="300" height="300" />
Here ,
String path=presentfileid+"-"+personname;
String sharedfilee=rs.getString("FILE_NAME");
First of all, in a JEE point of view, all files within the WEB-INF folder are not meant to be accessed by anyone but your server. It means that images, CSS files, javascript files, etc. in this directory will not be rendered by your web browser. Your JEE server will prevent that to happen.
So, in order to access files from your web browser, you need to put them outside the WEB-INF folder (at the same level, in a "images" folder, for instance).
For your first problem : Yes, you can use relative paths to instantiate files, using your classLoader.
this.getClass().getClassLoader().getResource("resourcePath")
or
this.getClass().getClassLoader().getResourceAsStream("resourcePath")
depending on your needs. The first one returns a URI (http://docs.oracle.com/javase/7/docs/api/java/lang/ClassLoader.html#getResource(java.lang.String)) whereas the second returns an InputStream (http://docs.oracle.com/javase/7/docs/api/java/lang/ClassLoader.html#getResourceAsStream(java.lang.String)) that you can use with a FileInputStream.
The ressources are located in the classes folder, and the path is relative to the class your get the resource from. For example, you can use "/myImage.png" as a path to get the image at the root level.
Putting all your resources files in the "classes" folder (within subfolder if you want to) is a good architecture design.
For your second problem, you have mainly 2 solutions :
if the image is rendered without any transformation, put it in a folder outside WEB-INF (see the beginning of my comment), and it will be visible from outside. In you JSP, you can access it like that :
request.getContextPath() + "/" + sharedfile
if the image needs a transformation, use a servlet instead
I hope that helps you.
Regards,
Alexandre FILLATRE
I am doing some basic templating in my JSP-based webapp. For example, I want to have a standard header and footer (basic HTML) that I pull into each of my JSPs.
My content JSP is at /WEB-INF/jsp/home.jsp, and I have template JSPs at /WEB-INF/jsp/template/, such as /WEB-INF/jsp/template/Body-Footer.jsp.
So now, within home.jsp, I want to pull in my template files. First, I try the jsp:include action:
<jsp:include page="template/Body-Footer.jsp"></jsp:include>
It generates the error javax.servlet.ServletException: File "/template/Body-Footer.jsp" not found
Strange to me, considering that Eclipse says that the path is valid.
Okay, so then I switch to the include directive:
<%# include file="template/Body-Footer.jsp" %>
This works just fine, pulls in my footer HTML.
But why does the jsp:include not work? After some experimentation, I find that putting in the absolute path does get it to work:
<jsp:include page="/WEB-INF/jsp/template/Body-Footer.jsp"></jsp:include>
Now it works fine, no errors.
So here's my question: why? Why do I (apparently) need to use an absolute path with the jsp:include action, but not with the include directive?
/WEB-INF/jsp/template/Body-Footer.jsp is not an absolute path. Its also a relative path. The problem is that template/Body-Footer.jsp is an incomplete relative path, whereas the other is complete. That is, the paths are relative to your app path. Since /WEB-INF/ is under your app path, you have to include it. Absolute path means like C:/program files/tomcat/webapps/yourapp/WEB-INF/jsp/template/Body-Footer.jsp
Answer to WHY - The jsp:include is a runtime directive unlike the <%# include ... %> directive which happens to be a compile time directive (translation time, actually).
See more on: JSP Performance using jsp:include
Bottom line - directives are run against different folders as a base.
Btw. JSP pages should be outside of WEB-INF folder, if you want to follow official recommendation:
The Java EE 6 Tutorial - Web Module Structure
I read JSP 2.0 spec and here:
Relative URL Specifications
* A context-relative path is a path that starts with a slash (/).
It is to be interpreted as relative to the application to which
the JSP page or tag file belongs. That is, its ServletContext
object provides the base context URL.
* A page relative path is a path that does not start with a
slash (/). It is to be in- terpreted as relative to the current
JSP page, or the current JSP file or tag file, depending on where
the path is being used.
For now javax.servlet.ServletContext.getRealPath("/WEB-INF/test/test.jsp") is null for security reason.
Assume that context-relative path is path from your WAR root.
Can anybody tell me how do i give absolute path of the img tag's src attribute?
The following doesn't work
<img alt="NO IMAGE" src="/home/administrator/tiger-info0[1].gif"/>
I am working On Ubuntu and i am very sure that image exists on this path.
This is probably happening because the image is located outside the web server's document root.
Your web server will not be able to serve anything from outside the document root. One possible workaround is to use a scripting language that has access to the file system, and route the images through the script. For example, you may want to check out the following implementation in php:
Serving Images Outside Document Root Via PHP
You can also create a symbolic link of /home/administrator/ into the document root:
ln -s /www/yoursite /home/administrator
hmm why don't you copy the image to your web directory and give it the relative path? you server (apache?) may not be able to access the file to serve the browser.
if you are making a local html page you can use that path but if you are creating a website you have to use the absolute path to the document root. And make sure the image path is correct (use firebug)
Give your path correctly with domain or use ../ or ./ is for to represent correct relative path.
You cannot access files that are not in your document root. Get Java application server to not delete your folder. You can probably do this by having one folder into which your users can upload files, and add that folder to your project. You can let users create subfolders inside that main folder, and since the main folder is a part of your project, cleaning the build will not automatically delete it or its subfolders.