How to get servlet path in jsp? [duplicate] - java

I want to retrieve root path of webapplication, then I want to append link to root path.
I tried request.context but it returns "http://localhost:8080/webapp/web-inf".
For example my root folder path is
path = http://localhost:8080/webapp/
and I want to append remaining link to this path
helpPath= /help/page/help.htm
name
Any help or pointer really appreciated.

<%=request.getContextPath()%>
will give you the rootpath of your application so in your case it will be http://localhost:8080/webapp
As per comment:
<%=request.getContextPath()%>/help/page/help.htm
will give you your page

You can use pageContext.request.contextPath
${pageContext.request.contextPath}
So you can use,
name
But the better way is to set the base href to this path and then use the path as it is.
<head>
<base href="${pageContext.request.contextPath}">
</head>
<body>
name
</body>

I believe you can use the getRealPath() method of the ServletContext.

You can also use
<c:url>
jstl tag. It will add the context path for you.

<%=request.getContextPath()%> will give /webapp
So your link should look like :
name

Related

What is the difference between the /urlpage and urlpage?

I can't understand why sometimes a '/urlpage' is used over a simple 'urlpage'.
for example on a form,
<form action="TestServlet">
<form action="/TestServlet">
I want to understand what is the proper way of specifying a path to a html, servlet, jsp, jsf and etc.
The first one is relative to the current path. For example, if you are on /appcontext/abc/def/Something, TestServlet will be on /appcontext/abc/def/TestServlet
In the second one, it is relative to the context of your app: if you are on /appcontext/abc/def/Something, /TestServlet will be on /appcontext/TestServlet

jsp ${pageContext.request.contextPath} dosent get requested

Currently i have a .jsp project where my welcome page is a servlet
<welcome-file>frontpage</welcome-file>
The servlet sets gets two ressources, a header file containing the < nav> and a footer containing the < footer>
request.setAttribute("header1", sc.getResource("/includes/nav.jsp").toString());
request.setAttribute("footer", sc.getResource("/includes/footer.jsp").toString());
And forwards to the index.jsp page
getServletContext().getRequestDispatcher("/WEB-INF/jsp/index.jsp").forward(request, response);
My question is.
When i get the ressource (footer.jsp), how can i in the footer.jsp dynamically import / include images?
I tried the following
<img src="${pageContext.request.contextPath}/images/picture1.png" alt="picture1"/>
But the expression ${pageContext.request.contextPath} gets treated as a string instead of a command, and does not get the context path.
I suspect its because the content of the footer.jsp is fetched in this manner and their for the context path isint actually ever requested within the footer.jsp.
But how do i solve this?
add <%# page isELIgnored="false" %> in top of your JSP page, to enable expression language.
and to include a JSP page with other use <jsp:include like:
<jsp:include page="/includes/nav.jsp"/>
<jsp:include page="/includes/footer.jsp"/>
This is not the way to include stuff. Use jsp:include action to include the header/footer. If for some reason you really want to do it in the servlet, see this post. As long as you just grab a resource like you do, you're reading the file like any text, there is no JSP compilation/evaluation.

forward from spring jsp to spring jsp

in catalog
webapp/WEB-INF/views located my jsp pages
I want to forward from 1.jsp to 2.jsp
in 1.jsp I write
${candidate.name}
but it doesn't work.
How to fix it?
Use <c:url and then the value attribute of <c:url is set to the href link to the other JSP. So for example, it would be:
${candidate.name}
I had used it in the following manner in my webapps (where circuits is a folder under /WEB-INF/views):
Edit Circuit
A relative url should start with a / character
Change the anchor tag as below and try again
${candidate.name}
Reference Link

What does this expression language ${pageContext.request.contextPath} exactly do in JSP EL?

I have a web app, where I have different navigation anchor tags such as Home, Profile and etc.
What I want:
When I press anchor tags like home or profile. I just want to ensure that current user gets its information in that Tags/JSP Page.
Sample Example that I am trying:
Profile
The pageContext is an implicit object available in JSPs. The EL documentation says
The context for the JSP page. Provides access to various objects including:
servletContext: ...
session: ...
request: ...
response: ...
Thus this expression will get the current HttpServletRequest object and get the context path for the current request and append /JSPAddress.jsp to it to create a link (that will work even if the context-path this resource is accessed at changes).
The primary purpose of this expression would be to keep your links 'relative' to the application context and insulate them from changes to the application path.
For example, if your JSP (named thisJSP.jsp) is accessed at http://myhost.com/myWebApp/thisJSP.jsp, thecontext path will be myWebApp. Thus, the link href generated will be /myWebApp/JSPAddress.jsp.
If someday, you decide to deploy the JSP on another server with the context-path of corpWebApp, the href generated for the link will automatically change to /corpWebApp/JSPAddress.jsp without any work on your part.
Include <%# page isELIgnored="false"%> on top of your jsp page.
use request.getContextPath() instead of ${pageContext.request.contextPath} in JSP expression language.
<%
String contextPath = request.getContextPath();
%>
out.println(contextPath);
output: willPrintMyProjectcontextPath
For my project's setup, "${pageContext.request.contextPath}"= refers to "src/main/webapp". Another way to tell is by right clicking on your project in Eclipse and then going to Properties:

What is the best way to use context-param in JavaScript

In my web.xml I set context param "pathToImages". In my jsp files to get path to my images files I use EL, like this:
<img src="${initParam.pathToImages}"/image.jpg" />
But I have problem with JavaScript. In my js files I have code:
setInnerHTML("somePlace", "<.img src='images/s.gif'>");
I know this isn't beautiful code but it isn't mine :) I cannot get my pathToImages in the same way like from jsp. What is the best way to do this?
Pass it as parameter to the js function: function createImage(path) {..} and invoke it with createImage('${initParam.pathToImages}')
Another, perhaps a better option is to have a js variable and initialize it with the desired value. In the JS file:
var imagePath;
function init(config) {
imagePath = config.imagePath;
}
and in your header:
init({imagePath: '${initparam.pathToImages}'});
If you are using templates to build your pages (i.e., tiles or velocity templates) you could assign the variable in your base template with a scriptlet, like:
<script> var imagePath = '<%= config.getServletContext().getInitParameter("pathToImages") %>'; </script>
Then you can refer to the imagePath variable in your js files.
Not pretty, but I don't believe there is a way to access the servlet context from a javascript file directly.

Categories

Resources