Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

where to put the servlets

 
aaaa aaaa
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi!

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

madhur
 
Avi Abrami
Ranch Hand
Posts: 1141
1
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
http://www.servlets.com
And you can read the book online at the "Safari" online library:
http://safari.informit.com
They are offering a free, two-week trial subscription.
Hope this has helped you.
Good Luck,
Avi.
 
Jessica Sant
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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!
 
Arun Prasad Namasivayam
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
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"
docBase="/path/where/servlets/are/placed"
crossContext="false"
debug="0"
reloadable="true" >
</Context>
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>
thanx
arun
 
Marty Hall
Author
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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\YourServlet.java
  • 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 http://www.moreservlets.com/Using-Tomcat-4.html
    Cheers-
    - Marty
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic