Solved

How do you compile and run a Servlet?

Posted on 2004-10-03
17
227 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
 

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 49

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 49

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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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 49

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 49

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

759 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now