Directly executing a servlet

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.
ashwinsraoAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jimmackCommented:
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
ashwinsraoAuthor Commented:
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
Mayank SAssociate Director - Product EngineeringCommented:
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
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Mayank SAssociate Director - Product EngineeringCommented:
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
ashwinsraoAuthor Commented:
Alrt, we seem to getting some where. But can anyone tell me where this particular setting is specified ?
0
Mayank SAssociate Director - Product EngineeringCommented:
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
ashwinsraoAuthor Commented:
Neither can i !!!! Its just an observation, which i had difficulty understanding. Hope that's gud enough. :)
0
Mayank SAssociate Director - Product EngineeringCommented:
Yup, buddy, so can we close this question now?
0
ashwinsraoAuthor Commented:
I think i'll wait till some one can completely answer my question
0
Mayank SAssociate Director - Product EngineeringCommented:
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
jimmackCommented:
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
ashwinsraoAuthor Commented:
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
Mayank SAssociate Director - Product EngineeringCommented:
I think you can split between Jim and me now....
0
Mayank SAssociate Director - Product EngineeringCommented:
Sorry I posted that comment without seeing your last one.
0
Mayank SAssociate Director - Product EngineeringCommented:
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
jimmackCommented:
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
ashwinsraoAuthor Commented:
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
Mayank SAssociate Director - Product EngineeringCommented:
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
Mayank SAssociate Director - Product EngineeringCommented:
>> I think its a standard feature

Typo.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jimmackCommented:
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
Mayank SAssociate Director - Product EngineeringCommented:
Thanx :-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java EE

From novice to tech pro — start learning today.