Solved

How to deploy a Servlet on TomCat?

Posted on 2003-11-06
3
1,552 Views
Last Modified: 2008-02-01
Hi
I am new to tomcat web server.

I just wrote a helloworld  servlet and then wrote a .html document to embed the class file.
I dropped the .html file in the d:\tomcat 4.1\webapps\examples\servlets directory.

Then I dropped the class file in the d:\tomcat 4.1\webapps\examples\WEB-INF\classes directory

Then I opened a brower and typed
http://localhost:8080/examples/servlets/MyHelloWorld.html

Ok the html script started. But the class file was not executed as I cannot see the result of that
coming out.

What did I do wrong?

PS. Web Server is running fine. I have a shell showing that.

Here is are my code....

<!-- MyHelloWorld.html -->
<HTML>  
  <HEAD>
     <TITLE>
        Servlet HTTP GET Example
     </TITLE>
     </HEAD>
          <BODY>        
                <FORM          
                  ACTION=http://localhost:8080/webapps/examples/WEB-INF/classes/MyHelloWorld
                  METHOD=<"GET">
                 <P> Click the button to have the servlet send an HTML document</P>
                             <INPUT TYPE="submit" VALUE="Get HTML Document">
               </FORM>
          </BODY>
</HTML>


and the servlet class is....
import java.io.*;
      import javax.servlet.http.*;

      public class MyHelloWorld extends HttpServlet
      {

          public void doGet(HttpServletRequest request,
                     HttpServletResponse response)
                                  throws IOException
           {
          response.setContentType("text/html");
          PrintWriter out = response.getWriter();
          out.println("<html>");
          out.println("<body>");
          out.println("<head>");
          out.println("<title>Hello World, I Just Started Servlets!</title>");
          out.println("</head>");
          out.println("<body>");
          out.println("<h1>Hello World,  I Just Started Servlets!</h1>");
          out.println("</body>");
          out.println("</html>");
         }
       }

0
Comment
Question by:prain
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 1

Accepted Solution

by:
robsmart earned 50 total points
ID: 9701522
You'll have to edit the web.xml file in \examples\WEB_INF\ to add an entry for your servlet

 <servlet>
        <servlet-name>
            MyHelloWorld
        </servlet-name>
        <servlet-class>
            MyHelloWorld
        </servlet-class>

    </servlet>

    <servlet-mapping>
        <servlet-name>
          MyHelloWorld
        </servlet-name>
        <url-pattern>
            /MyHelloWorld
        </url-pattern>
    </servlet-mapping>


also in your HTML change the form submitaction to

ACTION=http://localhost:8080/webapps/examples/MyHelloWorld

you dont actually need an html page to call the servlet youcan just put the url http://localhost:8080/webapps/examples/MyHelloWorld
in your browser
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my time as an SEO for the last 2 years and in the questions I have assisted with on here I have always seen the need to redirect from non-www urls to their www versions. For instance redirecting http://domain.com (http://domain.com) to http…
As Wikipedia explains 'robots.txt' as -- the robot exclusion standard, also known as the Robots Exclusion Protocol or robots.txt protocol, is a convention to prevent cooperating web spiders and other web robots from accessing all or part of a websit…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question