Solved

How do you compile and run a Servlet?

Posted on 2004-10-03
17
234 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
[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
  • 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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 

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 52

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 52

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 52

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 52

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
A solution for Fortify Path Manipulation.
The viewer will learn how to implement Singleton Design Pattern in Java.
This video teaches viewers about errors in exception handling.

617 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