[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

JSP and Servlets on same Jetty server

Posted on 2010-01-08
6
Medium Priority
?
661 Views
Last Modified: 2013-12-22
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!
0
Comment
Question by:Gustav_
  • 3
  • 3
6 Comments
 
LVL 27

Expert Comment

by:mrcoffee365
ID: 26211766
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
 

Author Comment

by:Gustav_
ID: 26212172
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
 
LVL 27

Accepted Solution

by:
mrcoffee365 earned 1500 total points
ID: 26212979
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:Gustav_
ID: 26213275
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
 

Author Closing Comment

by:Gustav_
ID: 31674633
Sparse/incomplete code example but a good pointer in the right direction.
0
 
LVL 27

Expert Comment

by:mrcoffee365
ID: 26213467
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month20 days, 12 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question