• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

compile servlet

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Tina Mills
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
With "class path" I meant using the -cp or -classpath compiler flags.
 
Tina Mills
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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>
 
F is for finger. Can you stick your finger in your nose? Doesn't that feel nice? Now try this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic