This week's book giveaway is in the OCMJEA forum. We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line! See this thread for details.
<HTML> <HEAD> <TITLE> Test to invoke a Servlet </TITLE> </HEAD> <BODY> <P> Invoking a Servlet from a Hyperlink </P> <P> <A Href="http://localhost:8080/servlet/FirstServlet"> Click here to invoke a Servlet </A> </P> </BODY> </HTML>
In order to execute your servlet you will have to create a web.xml file, which will contain the details about your FirstServlet.class and the URL used to invoke your servlet( in your case http://localhost:8080/servlet/FirstServlet ). Then you need to place your class files in the YourProject/WEB-INF/classes folder for the container to find them. Apart from that you will have to make sure your tomcat is running fine and class paths are set correctly. I suggest you either follow a book or check out the numerous sites on the web for the same. Check Marty Hall
I like your approach towards runing your first servlet. Sure it didn't work. Never mind, most of us had problem runing our first servlet. So you're on course. Let me take you through.
1. Organise the file structure. as follows (using Tomcat); under webapp; create a folder(call is serv - this is your app root dirctory) . 2. Create another, dir ( call it WEB-INF - a most, where your web.xml and classes and lib dir resides).
3. Compile your servlet class. (remember to add the servlet-api.jar in the classpath, if not the servlet class won't compile). put the compile servlet class into .../webapp/serv/web-INF/classes.
4. Create web.xml as follows.
<web-app> <servlet> <servlet-name>any name for servlet</servlet-name> <servlet-class>fully qualify name of servlet class (without class extention) </servlet-class> </servlet> <servlet-mapping> <servlet-name>same as above</servlet-name> <url-pattern>/serv.do</url-pattern> </servlet-mapping> </web-app>
5. Restart the web container. the hit the page using the name specified in <url-pattern>
Hope this clears some of the curious part of your first servlet.