JSP and Servlets on same Jetty server

I'm having a really hard time figuring out how to (programmatically, not via web.xml) configure jetty to serve a webapp (a directory with a bunch of jsp's) from one path and one or more servlets from a different path on the same server.

Ie.:
Calling the URL "http://127.0.0.1/*.jsp" should call the context serving the jsp's
Calling the URL "http://127.0.0.1/hello" or "http://127.0.0.1/hello2" should call the servlets configured at those url's.

I just cant seem to find a way to accomplish this since the whole concept of contexts/webapps and the alike are spinning in my head.

Code examples are more than welcome!
Gustav_Asked:
Who is Participating?
 
mrcoffee365Connect With a Mentor Commented:
Good question.  It's very hard to find good documentation for the complete options in the webapps web.xml file.  This site has pretty good documentation:
http://wiki.metawerx.net/wiki/Web.xml

You can see a sample webapp web.xml file here:
http://tomcat.apache.org/tomcat-6.0-doc/appdev/web.xml.txt
referred from a good webapp setup doc here:
http://tomcat.apache.org/tomcat-6.0-doc/appdev/deployment.html

Sun's dtd (if you can read dtd files) has the canonical list of tags for the webapp web.xml:
http://java.sun.com/dtd/web-app_2_3.dtd

Or the most recent xsd file:
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd

For the dtd and xsd files, look for the web-app tag to see what would go inside it.
0
 
mrcoffee365Commented:
web.xml is the way you're supposed to do this.

To use web.xml, you put something like this:
<servlet>
      <servlet-name>MyServlet</servlet-name>
      <servlet-class>MyServlet</servlet-class>
      <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
      <servlet-name>MyServlet</servlet-name>
      <url-pattern>/hello2</url-pattern>
</servlet-mapping>


If you don't want to use servlet mapping, then you'll need to define a filter for all requests of a certain type (or maybe all requests, then don't change the processing for .jsp requests).  It's more complicated to write, but you could write a filter which would check the incoming url against a list of servlets you have, and forward the request to the appropriate servlet.

Or, you could put Apache in front, and use .htaccess to send each url like /hello2 to the  appropriate servlet.

But the reason people use web.xml is that it's a lot easier -- someone has already written the url request filter for you, and web.xml is the configuration for it.
0
 
Gustav_Author Commented:
Ok that actually works, i wrote a simple web.xml and put it in WEB-INF in the webapps dir. Now I need to write mappings in the web.xml file but am unable to find a reference or some good examples of how it's used, any url's you can point me to?
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
Gustav_Author Commented:
Thanks! I found out that the format of the file is a part of the servlet specification published by Sun.
The xml below does just what I want it to. The se.xx.MyApp servlet is called when the /dss url is requested. When an empty path is entered the main.jsp is loaded.  can be used instead of  for calling a jsp.
<web-app>
	<display-name>Some Name</display-name>
	<description>XXX</description>
	<servlet>
		<servlet-name>DataSourceServlet</servlet-name>
		<servlet-class>se.xx.MyApp</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<url-pattern>/dss</url-pattern>
		<servlet-name>DataSourceServlet</servlet-name>
	</servlet-mapping>
	<welcome-file-list>
		<welcome-file>main.jsp</welcome-file>
	</welcome-file-list>
</web-app>

Open in new window

0
 
Gustav_Author Commented:
Sparse/incomplete code example but a good pointer in the right direction.
0
 
mrcoffee365Commented:
Yes, the servlet specification from Sun has partial doc of the webapp web.xml file.  It's good that the pieces there worked for you.

The "sparse" code sample was not sparse, it was complete for the answer -- so for other people looking at this answer, it was fully specified for a single servlet mapping.  And of course the links I provided above gave the full set of options for the webapp web.xml .
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.