Solved

How do you compile and run a Servlet?

Posted on 2004-10-03
17
231 Views
Last Modified: 2013-11-24
Hello;

Im new to this so pls. I need some assistance. I have a simple servlet called : HelloWorld.java

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class HelloWorld extends HttpServlet {

  public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {

    PrintWriter out = response.getWriter();
    out.println("Hello World");
  }
}

I put it in a folder classes in my directory :
C:\Tomcat_5.0\webapps\web1\WEB-INF\classes\HelloWorld.java

My question is how do I compile and run it?

Regards : Rajah
0
Comment
Question by:rajah_mohammed
  • 8
  • 4
  • 3
  • +2
17 Comments
 
LVL 92

Assisted Solution

by:objects
objects earned 25 total points
ID: 12214472
0
 

Author Comment

by:rajah_mohammed
ID: 12214482
I had read that already and just gave me complications would you help me
step by step [pls.]
0
 
LVL 92

Expert Comment

by:objects
ID: 12214486
what complications?
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:rajah_mohammed
ID: 12214502
I placed my HelloWorld.java which is a servlet in :
C:\Tomcat_5.0\webapps\web1\WEB-INF\classes\HelloWorld.java

Now how do I compile or run it? is it same as JSP? How?
0
 
LVL 92

Expert Comment

by:objects
ID: 12214540
javac HelloWorld.java

(it is covered step by step in the above tutorials)
0
 
LVL 50

Expert Comment

by:Ryan Chong
ID: 12214545
or with classpath included if necessary:

javac -classpath C:\Tomcat_5.0\webapps\web1\WEB-INF\classes\ C:\Tomcat_5.0\webapps\web1\WEB-INF\classes\HelloWorld.java
0
 

Author Comment

by:rajah_mohammed
ID: 12214595
I got several errors when I compile it :

C:\TOMCAT_5.0\WEBAPPS\WEB1\WEB-INF\CLASSES>c:\javac\bin\javac HelloWorld.java

HelloWorld.java:2: package javax.servlet does not exist
import javax.servlet.*;
^
HelloWorld.java:3: package javax.servlet.http does not exist
import javax.servlet.http.*;
^
HelloWorld.java:5: cannot resolve symbol
symbol  : class HttpServlet
location: class HelloWorld
public class HelloWorld extends HttpServlet {
                                ^
HelloWorld.java:7: cannot resolve symbol
symbol  : class HttpServletRequest
location: class HelloWorld
  public void doGet(HttpServletRequest request, HttpServletResponse response)
                    ^
HelloWorld.java:7: cannot resolve symbol
symbol  : class HttpServletResponse
location: class HelloWorld
  public void doGet(HttpServletRequest request, HttpServletResponse response)
                                                ^
HelloWorld.java:8: cannot resolve symbol
symbol  : class ServletException
location: class HelloWorld
    throws ServletException, IOException {
           ^
6 errors
0
 
LVL 50

Expert Comment

by:Ryan Chong
ID: 12214605
try:

c:\javac\bin>javac -classpath C:\Tomcat_5.0\webapps\web1\WEB-INF\classes\ C:\Tomcat_5.0\webapps\web1\WEB-INF\classes\HelloWorld.java
0
 

Author Comment

by:rajah_mohammed
ID: 12214622
I tried both =-(

C:\Javac\bin>javac -classpath C:\Tomcat_5.0\webapps\web1\WEB-INF\classes\ C:\Tomcat_5.0\webapps\web1\WEB-INF\classes\HelloWorld.java

Or

C:\Tomcat_5.0\webapps\web1\WEB-INF\classes>c:\javac\bin\javac HelloWorld.java

And I got the same error as above. How do I deal about it?

0
 
LVL 50

Assisted Solution

by:Ryan Chong
Ryan Chong earned 70 total points
ID: 12214657
I just now remember compile servlet is a bit different from compile java class file.. try:

C:\Javac\bin>javac -classpath C:\Tomcat_5.0\common\lib\servlet.jar C:\Tomcat_5.0\webapps\web1\WEB-INF\classes\HelloWorld.java
0
 
LVL 50

Expert Comment

by:Ryan Chong
ID: 12214660
Here is a PAQ:

Compiling a Java Servlet
http:Q_20787476.html
0
 

Author Comment

by:rajah_mohammed
ID: 12214741
Thanks Ryancys;

I got it running I just changed the servlet.jar to servlet-api.jar since I'm running Tomcat 5.
It did compile, one last thing how do I run it ?

C:\Javac\bin>javac -classpath C:\Tomcat_5.0\common\lib\servlet-api.jar C:\Tomcat_5.0\webapps\1stweb\WEB-INF\classes\HelloWorld.java
0
 
LVL 8

Expert Comment

by:thomas908
ID: 12214770
>>It did compile, one last thing how do I run it ?
Have u created web.xml file for ur application ?
0
 

Author Comment

by:rajah_mohammed
ID: 12214773
If I'm correct I just have to add the servlet to my web.xml to successfully run it. How do I add it
to my web.xml for me to run it.

My web.xml looks like this and I'm not how to add it :

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee web-app_2_4.xsd"
    version="2.4">
</web-app>

0
 

Author Comment

by:rajah_mohammed
ID: 12214777
How do I add my servlet to my web.xml above ?
0
 
LVL 19

Accepted Solution

by:
ramazanyich earned 85 total points
ID: 12215794
Put following web.xml into C:\Tomcat_5.0\webapps\web1\WEB-INF directory:

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee web-app_2_4.xsd"
    version="2.4">
  <servlet>
    <servlet-name>HelloWorld</servlet-name>
    <display-name>HelloWorld</display-name>
    <servlet-class>
        HelloWorld
    </servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>HelloWorld</servlet-name>
    <url-pattern>/HelloWorld</url-pattern>
  </servlet-mapping>
</web-app>

You should be able access it then using URL:
http://servername:port/web1/HelloWorld
0
 

Author Comment

by:rajah_mohammed
ID: 12222162
Thanks that was great !
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
allswap challenge 6 99
servlet example 11 40
servlet filter example 37 63
hibernate insert example 13 29
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

777 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