File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes compile servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "compile servlet" Watch "compile servlet" New topic
Author

compile servlet

Tina Mills
Greenhorn

Joined: Feb 23, 2012
Posts: 15
I'm following this article, http://www3.ntu.edu.sg/home/ehchua/programming/howto/tomcat_howto.html. I'm on this section:
Write the following source codes called "HelloServlet.java" and save it under your application "classes" directory (i.e., "<TOMCAT_HOME>\webapps\hello\WEB-INF\classes\HelloServlet.java"). Compile the source into "HelloServlet.class". This servlet says "Hello", echos some request information, and prints a random number upon each request.

How do you compile the source into "HelloServlet.class"?
Tina Mills
Greenhorn

Joined: Feb 23, 2012
Posts: 15
I copied Tomcat's Servlet API JAR-file located at "<TOMCAT_HOME>\lib\servlet-api.jar" to JDK's extension directory at "<JAVA_HOME>\jre\lib\ext". I ran "javacee HelloServlet.java" in the command prompt and got
'javacee' is not recognized as an internal or external command, operable program or batch file.

How do you compile HelloServlet.java?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19791
    
  20

All you need to do is add servlet.jar to your class path and use the regular compiler. However, I wouldn't put it inside the lib\ext folder. That will cause that one version to be used for all applications, and may break the servlet container if that's also run on your system. Just use the -cp / -classpath compiler flag instead.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Tina Mills
Greenhorn

Joined: Feb 23, 2012
Posts: 15
I tried putting a copy of the servlet-api.jar file here: C:\Users\Tina\Desktop\java\apache-tomcat-7.0.26\webapps\hello\WEB-INF\classes, then compile the HelloServlet.java file here: C:\Users\Tina\Desktop\java\apache-tomcat-7.0.26\webapps\hello\WEB-INF\classes but I got errors. Do you know what's wrong?

I got it to work by putting a copy of the servlet-api.jar file here: C:\Program Files\Java\jdk1.7.0_03\jre\lib\ext then I compiled the HelloServlet.java file here: C:\Users\Tina\Desktop\java\apache-tomcat-7.0.26\webapps\hello\WEB-INF\classes.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19791
    
  20

Tina Mills wrote:I tried putting a copy of the servlet-api.jar file here: C:\Users\Tina\Desktop\java\apache-tomcat-7.0.26\webapps\hello\WEB-INF\classes

That's a very, very bad place to put the file. I've seen many threads about people with class load problems because they put their own servet-api.jar file inside the web application's lib folder. That causes the web application and the servlet container (Tomcat) to have different copies of the class.

There are two options you should be using:
1) Just add the file in its original location to the class path. That's C:\Users\Tina\Desktop\java\apache-tomcat-7.0.26\lib\servlet-api.jar. You don't need to copy it anywhere.
2) Copy the file to a location that is 100% unrelated to the JDK, JRE or servlet container, and add it to the class path. It should also not be part of the web application itself.

Under no circumstance should you put the file in C:\Program Files\Java\jdk1.7.0_03\jre\lib\ext. This causes the JRE to always load this version, and will cause other problems if your servlet container uses a different version of the JAR file. You should also never put it within your web application for the reasons I mentioned before.
Tina Mills
Greenhorn

Joined: Feb 23, 2012
Posts: 15
I deleted the servlet-api.jar from here: C:\Program Files\Java\jdk1.7.0_03\jre\lib\ext and moved the HelloServlet.java file here: C:\Users\Tina\Desktop\java\apache-tomcat-7.0.26\lib\ The HelloServlet.java file won't compile. ??? It doesn't work when I do this: 1) Just add the file in its original location to the class path. That's C:\Users\Tina\Desktop\java\apache-tomcat-7.0.26\lib\servlet-api.jar. You don't need to copy it anywhere.

I'm so confused please be specific when referring to a file. Which file? What is the full path? I'm following this article, http://www3.ntu.edu.sg/home/ehchua/programming/howto/tomcat_howto.html, which may not be the best example. I don't know enough to know if it's good. Do you know a good article?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19791
    
  20

With "class path" I meant using the -cp or -classpath compiler flags.
Tina Mills
Greenhorn

Joined: Feb 23, 2012
Posts: 15
How do you use this "-cp or -classpath"? I'm not finding anything on -classpath when I Google "-classpath compiler flags" To compile the HelloServlet.java file I change the directory to where the HelloServlet.java file is then type "javac HelloServlet.java" in the console.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19791
    
  20

javac -cp .;C:\Users\Tina\Desktop\java\apache-tomcat-7.0.26\lib\servlet-api.jar HelloWorld.java

The -cp flag takes one or more folders and JAR files, separated with ; on Windows and : on Unix. To add jsp-api.jar you just add it to that value:
javac -cp .;C:\Users\Tina\Desktop\java\apache-tomcat-7.0.26\lib\servlet-api.jar;C:\Users\Tina\Desktop\java\apache-tomcat-7.0.26\lib\jsp-api.jar HelloWorld.java

Note that if any of the folders / JAR files has a space in the name, you should enclose the entire thing in spaces:
javac -cp ".;C:\Users\Tina\Desktop\java\apache-tomcat-7.0.26\lib\servlet-api.jar;C:\Users\Tina\Desktop\java\apache-tomcat-7.0.26\lib\jsp-api.jar" HelloWorld.java
Tina Mills
Greenhorn

Joined: Feb 23, 2012
Posts: 15
When I run this I get javac: file not found: HelloWorld.java
javac -cp .;C:\Users\Tina\Desktop\java\apache-tomcat-7.0.26\lib\servlet-api.jar;C:\Users\Tina\Desktop\java\apache-tomcat-7.0.26\lib\jsp-api.jar HelloWorld.java

I have a web.xml file here: C:\Users\Tina\Desktop\java\apache-tomcat-7.0.26\webapps\hello\WEB-INF Do I need to move it? This is what's in the web.xml file.
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

<!-- To save as "hello\WEB-INF\web.xml" -->

<servlet>
<servlet-name>HelloWorld</servlet-name>
<servlet-class>HelloServlet</servlet-class>
</servlet>

<!-- Note: All <servlet> elements MUST be grouped together and
placed IN FRONT of the <servlet-mapping> elements -->

<servlet-mapping>
<servlet-name>HelloWorld</servlet-name>
<url-pattern>/sayhello</url-pattern>
</servlet-mapping>
</web-app>
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: compile servlet