Solved

Directly executing a servlet

Posted on 2003-11-20
21
690 Views
Last Modified: 2013-11-24
Hi, can anyone tell me why does the "servlet" path come in the URL when directly exceuting a servlet. For eg:- <ipaddress>/servlet/<package>/<servlet-name> . What i need to know is that since we do not have "servlet" directory, how does this word appear in the URL???
I have tried this in both Tomcat and WebSphere, without having any entry for the servlet in the web.xml deployment descriptor.
0
Comment
Question by:ashwinsrao
  • 11
  • 6
  • 4
21 Comments
 
LVL 15

Expert Comment

by:jimmack
ID: 9787369
In Tomcat, you need to define the servlet mapping in web.xml (it should be something similar in WebSphere):

<servlet>
   <servlet-name>MyServlet</servlet-name>
   <servlet-class>com.mypackage.AServlet</servlet-class>
</servlet>

<servlet-mapping>
   <servlet-name>MyServlet</servlet-name>
   <url-pattern>/path_in_url</url-pattern>
</servlet-mapping>

This should let you access it using:

http://hostname/path_in_url/MyServlet

0
 

Author Comment

by:ashwinsrao
ID: 9794706
Hi Jim, thx for the input. But as mentioned in my question, i need to run the servlet directly without defining any servlet mapping in web.xml. It is in this scenario that the "servlet" name comes in the URL path. What i need to know is why this happens even if i dont have a directory by that name in my workspace. Any help regarding this will be appreciated.

Ashwin
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 9795096
It tells the server that its a Servlet, whose mapping might not be given in the web.xml file, and then you can specify the path/ name of the servlet.

rgds,
Mayank.
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 9795100
Though I would say that its not a good way of doing it. You should specify a Servlet-mapping in web.xml. I am also not sure if it works on all servers.
0
 

Author Comment

by:ashwinsrao
ID: 9795352
Alrt, we seem to getting some where. But can anyone tell me where this particular setting is specified ?
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 9795370
Well, I'm not sure how that is done. BTW, why do you want to do it without specifying in web.xml? I don't see any advantages with that.
0
 

Author Comment

by:ashwinsrao
ID: 9795486
Neither can i !!!! Its just an observation, which i had difficulty understanding. Hope that's gud enough. :)
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 9795553
Yup, buddy, so can we close this question now?
0
 

Author Comment

by:ashwinsrao
ID: 9795735
I think i'll wait till some one can completely answer my question
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 9795852
I'm not sure which version of Tomcat you're using, but I'll assume it's version 4.1.x. Previous versions of Tomcat did allow for routing requests to servlets without a web.xml file. The syntax was:

http://host[:port]/[webapp]/servlet/[ClassName]

However, at some point, this feature was removed, as it no longer functions in Tomcat 4.1.  I'm not in the mood right now to go through all the versions and see where it disappeared :-), but I'm sure plenty of folks know when and why. Perhaps its removal was security-related.

This is probably for the best, however, as this feature was *not* a standard Servlet 2.3 feature, and could lead to confusion if you migrated to another servlet container. The Servlet 2.3 spec makes no provision for URL-servlet mapping that isn't defined in web.xml. It does, however permit containers to add their own implicit mapping features, such as the one above, but as I stated, such mappings would indeed be non-standard.

These are the steps that you take to deploy a Servlet:

1. Create the source code for your servlet and compile it. You can do this using a third-party development tool, or simply use javac included in JDK 1.3. Note that you need to include the servlet API library in your classpath when you compile your servlet.

2. Create a class directory: <HOME>/defaultWebapp/WEB-INF/classes (or whatever it is). The directory is used to store your servlet classes and related Java classes as specified in the Servlet specification.

3. Copy your Servlet class file into this class directory. If your servlet class belongs to a package, make sure you have also created sub-diretories that corresponds to the package. For example, if your servlet belongs to com.tcc.abc, you have to create:

<HOME>/defaultWebapp/WEB-INF/classes/com/tcc/abc and copy your Servlet class into this directory.

4. Open web.xml in <HOME>/defaultWebapp/WEB-INF. Insert the following xml fragment into web.xml between the <web-app> and </web-app>tags.

<servlet>
  <servlet-name>myServlet</servlet-name>
  <servlet-class>package.name.myServlet</servlet-class>
</servlet>
<servlet-mapping>
  <servlet-name>myServlet</servlet-name>
  <url-pattern>/quickStartServlet</url-pattern>
</servlet-mapping>

In the above, package.name.myServlet is the full class name of your servlet class (e.g., com.tcc.abc.myServlet)

5. Start the default server.

6. Open a web browser and point it to http://localhost:8003/quickStartServlet

Note: Most servers also support invocation of a Servlet using its classname directly, without pre-registration of the Servlet in web.xml (step 4 above). However, it is not covered by the Servlet specification and is considered *a shortcut for convenience during development*. To invoke your Servlet without doing step 3 above, use http://localhost:8003/servlet/myServlet in step 6.

Hope that helps!

Mayank.
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 15

Expert Comment

by:jimmack
ID: 9795973
From "Core Servlets and Java Server Pages, Vol 1, 2nd Edition - Marty Hall & Larry Brown":

Using the URL Prefix:

When you use Web applications, a special prefix is part of all URLs.  For example:

-  Unpackaged servlets are invoked with a default URL of
    http://host/webAppPrefix/servlet/ServletName

-  Packaged servlets are invoked with
    http://host/webAppPrefix/servlet/packageName.ServletName

-  Registered servlets (in the web.xml) are invoked with
    http://host/webAppPrefix/customName

-  HTML pages from the top-level Web application directory are invoked with
    http://host/webAppPrefix/filename.html

-  HTML pages from subdirectories are invoked with
    http://host/webAppPrefix/subdirectoryName/filename.html

-  JSP pages are placed in the same locations as HTML pages and invoked in the same way (except that the file extension is .jsp instead of .html)
0
 

Author Comment

by:ashwinsrao
ID: 9795985
Mayank, thx for going thru the trouble of writing such a descrptive approach to deploying servlets. But i have been thru all these steps. The "complete" answer i was referring to was an explanation of the setting which requires the "servlet" name to be specified in the URL. I am aware of the methodology for deploying servlets thru entries in web.xml , and as i metioned i raised this question more out of curiosity than anything else.

It wud be of help to me if someone can come up with an explaination of these settings described in the reply chain not about how to deploy servlets.
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 9795986
I think you can split between Jim and me now....
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 9795995
Sorry I posted that comment without seeing your last one.
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 9796004
I told you that it was not a standard feature, and people mostly use it while development, just for testing their Servlets. I guess Jim has something more to say....?
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9796008
The /serlvet/ is included as part of the default (if you don't specify a URL path in the web.xml).

Had you read my last post?
0
 

Author Comment

by:ashwinsrao
ID: 9809303
Jim can u tell me where i can have a look at this default setting u suggested in your comment. I am more or less convinced about the arguments presented and would like to close this thread asap.
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 9809472
I think its a standard feature across many servers, not just something specific to Tomcat. I don't think that you will be able to find that setting somewhere in any Options - because probably either the server version comes with that feature, or it doesn't. You have no option of changing it. But of course, I maybe wrong in this (but I hope I am not). It is not a standard thing.

Mayank.
0
 
LVL 30

Accepted Solution

by:
mayankeagle earned 150 total points
ID: 9809484
>> I think its a standard feature

Typo.

I think its a non-standard feature but it is found across....
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9818523
mayankeagle is basically right.  This default behaviour is how the server manages the URLs internally if no specific options are provided, so this in turn means that there is no setting to look up.
This behaviour should be seen on Tomcat, JRun and Resin, but those are the only servers I can safely comment on.
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 9824180
Thanx :-)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
strDist challenge 35 84
wordlen challenge 3 46
maven example error 3 35
recursion example 16 68
A short article about a problem I had getting the GPS LocationListener working.
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

707 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now