Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to compile a Java file to Servlet?

Posted on 2002-05-28
10
Medium Priority
?
340 Views
Last Modified: 2010-04-01
Hello,

Currently need to developer my java code to a Servlet. (.java => .class). So, what is the exact method of doing this? I'm using Tomcat 4.0 & JDK 1.3.

and what is the advantages of using Servlet?
0
Comment
Question by:ikan
[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
  • 4
  • 3
  • 3
10 Comments
 
LVL 92

Expert Comment

by:objects
ID: 7038640
You compile a servlet in exactly the same way you compile any Java file. You'll need to have j2ee installed, or alternatively you can just include servlet.jar from tomcat in your compile classpath.
0
 
LVL 92

Expert Comment

by:objects
ID: 7038645
A servlet needs to implement the javax.servlet.Servlet interface, and generally you'll extends HttpServlet.

> and what is the advantages of using Servlet?

Advantage over what?
Generally faster than CGI at it doesn't need to load for each request.
0
 
LVL 1

Expert Comment

by:raid999
ID: 7040773
What i know is that you have your servlet in the servlets folder like WEB-INF/CLASSES  and then you call the servlet like this

http://www.sitename.com/serlvet/nameofservlet

the .java file will automatically compile if you have the servlet.jar in your classpath
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:ikan
ID: 7046324
Hi,

I already put the servlet.jar in the WEB-INF/CLASSES folder but still nothing working. I tried using the javac.exe from {JDK} folder to compile it but get error:

Here are some of the output in DOS screen:
_____________________________________

C:\jdk1.3.1_02\bin>dir/w
 Volume in drive C has no label.
 Volume Serial Number is 2141-16E6

 Directory of C:\jdk1.3.1_02\bin

[.]                      [..]                     HtmlConverter.bat
appletviewer.exe         dt_shmem.dll             dt_socket.dll
extcheck.exe             idlj.exe                 jar.exe
jarsigner.exe            java.exe                 javac.exe
javadoc.exe              javah.exe                javap.exe
javaw.exe                jdb.exe                  jdwp.dll
keytool.exe              native2ascii.exe         oldjava.exe
oldjavac.exe             oldjavaw.exe             oldjdb.exe
policytool.exe           rmic.exe                 rmid.exe
rmiregistry.exe          serialver.exe            tnameserv.exe
unregbean.exe            HelloWorldExample.java
              30 File(s)        655,771 bytes
               2 Dir(s)   5,353,418,752 bytes free

C:\jdk1.3.1_02\bin>javac HelloWorldExample.java
HelloWorldExample.java:8: package javax.servlet does not exist
import javax.servlet.*;
^
HelloWorldExample.java:9: package javax.servlet.http does not exist
import javax.servlet.http.*;
^
HelloWorldExample.java:17: cannot resolve symbol
symbol  : class HttpServlet
location: class HelloWorldExample
public class HelloWorldExample extends HttpServlet {
                                       ^
HelloWorldExample.java:20: cannot resolve symbol
symbol  : class HttpServletRequest
location: class HelloWorldExample
    public void doGet(HttpServletRequest request,
                      ^
HelloWorldExample.java:21: cannot resolve symbol
symbol  : class HttpServletResponse
location: class HelloWorldExample
                      HttpServletResponse response)
                      ^
HelloWorldExample.java:22: cannot resolve symbol
symbol  : class ServletException
location: class HelloWorldExample
        throws IOException, ServletException
                            ^
6 errors

C:\jdk1.3.1_02\bin>

So, how to solve this problem exactly? Any references?
0
 
LVL 92

Expert Comment

by:objects
ID: 7046335
> I already put the servlet.jar in the WEB-INF/CLASSES
>  folder but still nothing working.

Don't believe that necessary.
It's already available in your Tomcat distribution.

> I tried using the javac.exe from {JDK} folder to compile
> it but get error:

You need to make servlet.jar available to javac.
You can do this by adding it yto your classpath, or simply copying it to the 'ext' folder of your JDK.


0
 
LVL 1

Accepted Solution

by:
raid999 earned 180 total points
ID: 7046644
ok let us say that your servlet.jar is in c:/JAR
you go to CMD if you are using Windows and then type
CLASSPATH=%CLASSPATH%,C:/JAR/servlet.jar

that should work.

and also have a look at this site
http://jakarta.apache.org/tomcat/tomcat-4.0-doc/index.html
0
 

Author Comment

by:ikan
ID: 7065008
Hi guys,

Sorry for the late reply, I was able to compile a java file by using this:

C:\jdk1.3.1_02\bin\javac.exe -classpath C:\jakarta-tomcat-4.0.3\common\lib\servlet.jar C:\jakarta-tomcat-4.0.3\webapps\test\WEB-INF\classes\HelloWorld.java

Can this be simplify?

and i'm very confuse on the difference between Servlet and Java Bean, can Servlet return some value? How can i call a servlet in jsp, then display the return value from servlet in HTML?

Lot of question here but hope someone can explain them to me, thanks.
0
 
LVL 1

Expert Comment

by:raid999
ID: 7065089
windows 2000 or what?
0
 

Author Comment

by:ikan
ID: 7066316
Windows 2000 Advanced Server, but i don't think it's related with OS.
0
 
LVL 92

Expert Comment

by:objects
ID: 7077616
Sometimes wonder why I bother :-(
0

Featured Post

Industry Leaders: 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

We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

688 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