File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes where to put the servlets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "where to put the servlets" Watch "where to put the servlets" New topic

where to put the servlets

aaaa aaaa

Joined: Dec 29, 2002
Posts: 6

i have created a new dir. c:\Servlets
and have placed the class files there
now how do i access the servlet in the browser i.e.what URL to type in in the browser

Avi Abrami
Ranch Hand

Joined: Oct 11, 2000
Posts: 1141

Hi Madhur,
The URL to use depends on how you have configured your Web Server/Servlet Engine -- which you failed to mention (or if you did, then I missed it -- sorry).
I'll assume that you are using Tomcat as your servlet engine -- since it is the most popular (I believe) and is also the reference implementation for the servlet specification (from SUN Microsystems).
If that is the case, then I'll have to disappoint you again -- because I don't use Tomcat.
With the servlet engine that I use, there is a configuration file where you map the URL's to actual directories, so using your example, I'd have to map "C:\Servlets" to some URL and then I'd use that URL to access my servlet.
I don't know how much you know about servlets (or where you got your knowledge from), but I learned about servlets from the book "Java Servlet Programming" by Jason Hunter (with William Crawford) and I highly recommend it. The book's companion web site is:
And you can read the book online at the "Safari" online library:
They are offering a free, two-week trial subscription.
Hope this has helped you.
Good Luck,
Jessica Sant

Joined: Oct 17, 2001
Posts: 4313

"aaaa aaaa"-
Welcome to the JavaRanch! Please adjust your displayed name to match the JavaRanch Naming Policy.
You can change it here.
Thanks! and again welcome to the JavaRanch!

- Jess
Blog:KnitClimbJava | Twitter: jsant | Ravelry: wingedsheep
Arun Prasad Namasivayam

Joined: Jan 02, 2003
Posts: 5
if u r using tomcat then follow the steps
1. open server.xml in the conf dir
2. create new context path tag for mapping
<Context path="/path/tobe/given/inurl"
reloadable="true" >
3.And then in ur case create "Web-inf" dir inside "C:\servlets"
4.then create "classes" dir inside "web-inf"
5.compile all the java servlets and put the class files inside the classes dir
6.then start the tomcat server from the classes dir
7.check in the url
Ex: http://<hostname>:<port>/<path>/<filename>
Marty Hall
Ranch Hand

Joined: Jan 02, 2003
Posts: 111

i have created a new dir. c:\Servlets
and have placed the class files there
now how do i access the servlet in the browser i.e.what URL to type in in the browser

Well, as the previous reply said, some details are server-dependent. However, there are some general principles that apply to all servers:
  • You almost certainly want to develop and compile in one directory, then deploy the .class files to another directory for testing with your server.

  • I recommend that you do not try to register your development directory as a Web application directory on the server.
  • Since you said you created a C:\Servlets directory, for development, your source code should go in C:\Servlets\directoryMatchingPackageName\. For instance, if your servlet is in package "foo", it should go in C:\Servlets\foo\
  • For deployment, your .class files should go in

  • something\WEB-INF\classes\directoryMatchingPackage
    What the "something" is is server-dependent, but most servers have a directory for Web applications, so it is usually servers-web-app-dir\your-web-app-name\. With Tomcat, it is tomcat_install_dir\webapps\your-web-app-name. You can also use the server's default Web application (install_dir\webapps\ROOT with Tomcat). JRun, Resin, and ServletExec have default Web apps too.
  • Most servers have an "invoker" servlet that lets you run the servlet without bothering with servlet-mapping entries in web.xml (Tomcat, JRun, ServletExec, and Resin do, at least). So, your URL would be http://hostname/web-app-name/servlet/packageName.ServletName(Use "localhost" for the hostname if running on your desktop). If you use the server's default Web app instead of a custom one, you would use

  • http://hostname/servlet/packageName.ServletName

    For specific examples of all this with Tomcat, please see
    - Marty

    Java training and consulting
    I agree. Here's the link:
    subject: where to put the servlets
    It's not a secret anymore!