Get Servlet Context from JSP

Hello

I thought this would be simple to do but I am having all kinds of issues. All I am trying to do is to get the ServletContext from within a JSP.

I have tried all kinds of things but I am obviously not getting this. Some examples include…

<%= request.getServletPath().toString() %>
<%= getServletContext().getServletContextName() %>

Just to make sure I am getting the correct value, I am displaying it from a JavaScript function.

If you could point out my mistake here, it would be greatly appreciated.

Thanks in advance for your time.


<%@ page import="javax.servlet.*" %>

<HTML>
<HEAD>
<SCRIPT LANGUAGE="JavaScript">
<!--
	function submitForm()
	{
		alert( "Here: " );
		alert( "Here: " + <%= request.getServletPath().toString() %> );
		mainForm.submit();
	}
-->
</SCRIPT>
</HEAD>
<BODY onload="submitForm();">
<form name="mainForm" action="/<%= request.getServletPath() %>/Print" method="post">
</form>
</BODY>
</HTML>

I haven’t tested it, but I think request.getServletContext() may be what you’re looking for. What exactly are the issues you’re having?

1- Why do you want the context?

2- The context is included with every JSP as a convenience object named ‘application’ (same idea as ‘request’)

3- request.getServletPath() returns a string that begins with a /, so, if it’s working, your action begins with //

4- Comment out the auto-submit and view source on the generated page to see what the JSP is outputting.

Here is what I am trying to accomplish…

I have a link called Print to PDF. When the user clicks this link, I call a JSP… the JSP I posted above.

This JSP is located in the directory jsp/JspName.

This JSP then calls a servlet which converts the data on the page with the link to PDF and displays the PDF document in this window.

My problem that was occurring is since I have the JSP located in a directory called JSP, posting of the form mainForm was not calling the servlet Print.

So I hardcoded the servlet context in front of the servlet print and everything worked.

Based on your suggestion of request.getServletContext(), when I try this Eclipse tells me “The method getServletContext is undefined for the type HttpServletRequest”.

Is this not an HttpServlet request? I am not sure why this is not an option for me. If you can not tell, I am still learning how to code with JSP’s.

getServletContext() is an HttpServlet method inherited from GenericServlet, not a HttpServletRequest method.

I think this may be a case of mistaken nomenclature.

The Servlet Path is the portion of the URL that follows the root - this is want you wanted.

(root: www.example.com, servlet path: \foo\bar\work\MyServlet)

The Servlet Context is the server container that holds your servlet, a servlet/JSP programmer rarely, if ever, needs to access this (and should have a really, really good reason for doing so)

As I hinted at above, the easiest way to troubleshoot a misbehaving JSP is to look at the page source and see what it is outputting, then look in your logs for errors.

sorry, I meant getServletPath() … but yes looking at the generated source is a good place to start in determining the problem.

Thank you for clarifying this but I do not think I am asking the correct questions.

In Eclipse, I named my dynamic web project testingApp. I am running the app thru Tomcat 5.5 in Eclipse. So when Eclipse builds the project, in the wtpwebapps directory, it builds a directory called testingApp. In testing app is where all of my code is for this project. So I was referring to this as the servlet context for this project.

Now, when I do my form post from my JSP, if I hard code this, everything works.


<form name="mainForm" action="/testingApp/Print" method="post">
</form>

So what I am trying to do is use code to determine the value “testingApp” instead of hard coding it.

When I use the getServletPath(), it is returning the location of my jsp. How can I get the value, testingApp?

Also, this is not a strong area for my, so I am not offended at all if you correct how I refer to things. It will only increase my understanding so I do appreciate any feedback.

Thanks for your time.

I finally got it…

<%= request.getContextPath() %>

Thank you for your help.