Solved

How do you compile and run a Servlet?

Posted on 2004-10-03
17
233 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
Business Impact of IT Communications

What are the business impacts of how well businesses communicate during an IT incident? Targeting, speed, and transparency all matter. Find out more in this infographic.

 

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Why doesn't this text field show up on my Applet frame? 2 43
java imports not found 4 71
IntelliJ and Eclipse Neon 2 installations 4 66
maximize the sum of fractions 33 75
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

739 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