0

I was reading a book (school book) on jsp and there was something I couldn't understand. In the book we had to execute a jsp page and for that they set a context in servlet.xml in tomcat 5.5:

<Context path="JSPTEST" docBase = "G:\Tomcat5.5"\webapps\JSPTEST"></Context>

and the jsp in that folder is accessed at http://localhost:8080/JSPTEST/filename.jsp

The first thing I realized is that the docBase is actually a webapp on the server so with or without setting this context it will still run on the same url so what's the point?

I installed a tomcat 7 and added a context to conf/context.xml

Context docBase="F:\work\bscit\serverside\practical" path="serverside" reloadable="true" />

I put hello.jsp under the practical folder restarted the tomcat and there was no response.server was down with this in catalina_log. To me that's another way to set a virtualhost (I have a good idea on how to create a virtualhost under apache httpd) so am kind of confused right now.

Question 1 what exactly context are used for ? I've had a look at tomcat doc it didn't ring the bell

Question 2 how can I make the "same" thing work on tomcat 7 (without putting the file in tomcat of course)?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
black sensei
  • 6,528
  • 22
  • 109
  • 188

1 Answers1

2

1- A context is what describes a webb-app inside a web container. There you can define which path to use to access the web-app path="JSPTEST", where the resources are located (the document base / root) docBase = "G:\Tomcat5.5"\webapps\JSPTEST" and other stuff such as JNDI resources for accessing database for example.

So the config you did in Tomcat 5 is to be read as follow:

When i ask the server for the path JSPTEST (witch is the root of your web app) he looks inside the physical directory defined in docBase of the corresponding context element. Once there the server looks than for the resource filename.jsp processes (since it is no static content, jsps need first to be compiled and run to generate html) and serves it to the client.

In the same way you can read the config made for Tomcat 7.

2- To be able to retrieve the resource hello.jsp from Tomcat 7 (or wahtever version) the url should be http://localhost:8080/serverside/hello.jsp

The name of the directory where the content is physically stored plays no role in the url for accessing the content. It is the combination between the elements path and docbase.

Reply 2

Putting the context element inside the conf/context.xml, which itself has a context element as root is wrong and this is what is causing the error you are getting (an xml-parsing error while reading conf/context.xml). there are two ways to configure a web-app using the context.xml, take a look here . Read point 1 and 2 carefully!

So to make things work:

Either

1- Put a file named serverside.xml with the content <Context docBase="F:\work\bscit\serverside\practical" path="serverside" reloadable="true" ></Context> in conf/Catalina/localhost/

Or

2- Create a directory inside webapps named serverside and put your jsps inside of it. In that case there is no need for a serversid/META-INF/context.xml file, unless you want to define other resources for the web-app, docBase and path elements may not appear in it.

Then call http://localhost:8080/serverside/hello.jsp

(1) is good for development, (2) is good for production.

Mangu Singh Rajpurohit
  • 10,806
  • 4
  • 68
  • 97
A4L
  • 17,353
  • 6
  • 49
  • 70
  • Hello thanks for the reply. i've actually tried already by hitting `http://localhost:8080/serverside/hello.jsp` but apparently the wholes server is down. here is the context configuration in `conf/context.xml` : `` error is pasted here : http://pastie.org/4354291 . this is simplictic webapp without web-inf et al. – black sensei Jul 29 '12 at 17:55
  • If the server was down then you wouldnt habe got HTTP 404 error ;-). – A4L Jul 29 '12 at 21:17
  • Yup you are right, it was a mistake from me. i edited the post it's down. not `404`. from the error in pastie you would notice that even ROOT is not loaded. thanks – black sensei Jul 30 '12 at 06:18
  • I think that mus be something: – joseluisbz Sep 28 '14 at 04:41