Context path is not setting in tomcat 5 - java

One of our project we deployed in tomcat 5.5. our code is not an war file. deployed as a jar.
and we did not deploy directly under webapps . we created new folder and placed everything over there.
and we configured that path in server.xml under context tag
<code>
< Context path="/ABC" docBase="/app/apache-tomcat-5.5.26/webapps/ABC/"
debug="1" reloadable="true" crossContext="true">
< /Context>
</code>
The problem is.
we were able to land on home page but the images are not load and none of the links are not working
Home page link ; //ip:port/ABC/home.jsp
when we click any of the links then the context path is getting removed like the below
one of the link : //ip:port/firstlink.jsp
please help me where we have to config the context path
Thanks in advance

Your link <a href="/home.jsp"> is absolute to your domain, you need to include the context in the URL. To obtain the current context use request.getContextPath
getContextPath
String getContextPath()
Returns the portion of the request URI that indicates the context of
the request. The context path always comes first in a request URI.
The path starts with a "/" character but does not end with a "/"
character. For servlets in the default (root) context, this method
returns "".
...
Returns:
a String specifying the portion of the request URI that indicates the context of the request
HttpServletRequest#getContextPath()
You can retrieve this value in JSP with ${pageContext.request.contextPath}. Your link should be:
Home

Related

Why appending "/" to base-href removes the need of deploy url?

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

Tomcat context path issue

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

JSP include directive, jsp:include action, relative vs. absolute paths

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.

How to get file system path of the context root of any application

I am working on web application.I invoke on my jsp request.getContextPath(), but strangely I got address /streetshop.
Then I am appending some path as request.getContextPath() + "abc" and create folder.
Then its creating folder in D:// instead of my webapplication folder.
Please, tell me, I want to upload an image in put it in my web-application root/images/images.gif.
You mix things up here. HttpServletRequest.getContextPath() returns your web application root path. In your example this is /streetshop, so your URL may look similar to www.myapp.com/streetshop. If you want to access the internal file system path, you must obtain it from the ServletContext using request.getServletContext().getRealPath("/"). This should return the location of your WAR files' WebContent folder.
Keep in mind that if you modify contents of this path during runtime, you're going to loose everything when redeploying your application.

Relative Path Question Running a Tomcat Server

So I have a file structure like below
http://dl.dropbox.com/u/322696/FolderPath.JPG
When I start the Tomcat Server, I'm trying to figure out where it's root folder begins with if I were just to start the server in the server window and access a jsp page within the jsp folder. I need to navigate to images/banner/name.jpg (trying to make an image file). I've set the servers context to '/projectname' (the black rectangle next to [repository]). I've tried like:
File image = new File("../images/banner/name.jpg"); //If its root was the jsp I accessed
No dice. Is there a method I could run to determine what the relative path should be?
Are you asking how to get the on disk path from within tomcat? If so you use the ServletContext.
getServletContext().getRealPath("/")

Categories