Servlet on Apache 2.0 not loading
Hi everyone. Java is very new to me but have been working with Apache Servers for a while now. Can someone please help me understand the processes involved regarding how to run servlets on web servers?
Basically, I was testing a very basic HelloWorld servlet that I coded and I got to the javac -classpath... HelloWorld.java part and it successfully generated the HelloWorld.class afterwards. What do I do with this class file now to get the HelloWorld servlet to run in Apache?
My directory structure is as follows:
Apache 2
>bin
>cgi-bin
>conf
>error
>htdocs
>icons
>include
>lib
>logs
>manual
>modules
>proxy
Is there any configuration file I need to tweak? Sorry for the seemingly naive question. I am trying to make sense of this vast but exciting language. TIA.
Hi,
You have to configure your servlet file inside web.xml which is resides inside WEB_INF folder of your application.
For Ex:
If your application name is Sample then you have to create Sample folder inside webapps folder of your Tomcat installation directory and then create WEB_INF folder inside your application folder and then create classes folder and place the HelloWorld.class file inside the classes folder.
Creating web.xml File:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
"http://java.sun.com/j2ee/dtds/web-app_2.2.dtd">
<web-app>
<display-name>Sample Application</display-name>
<servlet>
<servlet-name>HelloWorld</servlet-name>
<display-name>HelloWorldServlet</display-name>
<servlet-class>
HelloWorld</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloWorld</servlet-name>
<url-pattern>/Sample/servlet/*</url-pattern>
</servlet-mapping>
</web-app>
Copy this web.xml file and place it inside WEB_INF folder.
Restart the tomcat server and enter the following address in the browser
http://servername:port/HelloWorldServlet. you will see the contents present inside helloworld servlet will be displayed in the browser.