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 Servlet Compilation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Servlet Compilation" Watch "Servlet Compilation" New topic
Author

Servlet Compilation

Ayanik Sil
Greenhorn

Joined: Sep 23, 2007
Posts: 27
Hello friends, my servlet class is like this..
my diretory structure is C:\Program Files\Apache Software Foundation\Tomcat 5.5\webapps\testJsp\WEB-INF\classes
where i have the newBean class


import foo.*;
import javax.servlet.*;
import javax.servlet.http;

public class newBean extends HttpServlet {

public void doPost(HttpServletRequest request,HttpServletResponse response)

throws ServletException,IOException {

Person p=new Person();
p.setName("Evan");
request.setAttribute("Person",p);
RequestDispatcher view=request.getRequestDispatcher("result.jsp");
view.forward(request,response);
}}

Now I have the following as classpath
"C:\Program Files\Apache Software Foundation\Tomcat 5.5\common\lib\servlet-api.jar";

if i change to classes directory, and give javac newBean.java,
my servlet still doesnot compile.
what should i type in the command prompt?
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8815
    
    5
let's slide this over to the Servlets forum


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30586
    
154

Ayanik,
What compiler error do you get?


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Ayanik Sil
Greenhorn

Joined: Sep 23, 2007
Posts: 27
The servlet class isn't compiling.

Here is the response.

newBean.java:2: package javax.servlet does not exist
import javax.servlet.*;
^
newBean.java:3: package javax.servlet does not exist
import javax.servlet.http;
^
newBean.java:5: cannot find symbol
symbol: class HttpServlet
public class newBean extends HttpServlet {
^
newBean.java:7: cannot find symbol
symbol : class HttpServletRequest
location: class newBean
public void doPost(HttpServletRequest request,HttpServletResponse response)
^
newBean.java:7: cannot find symbol
symbol : class HttpServletResponse
location: class newBean
public void doPost(HttpServletRequest request,HttpServletResponse response)
^
newBean.java:9: cannot find symbol
symbol : class ServletException
location: class newBean
throws ServletException,IOException {
^
newBean.java:9: cannot find symbol
symbol : class IOException
location: class newBean
throws ServletException,IOException {
^
newBean.java:11: cannot access foo.Person
bad class file: .\foo\Person.java
file does not contain class foo.Person
Please remove or make sure it appears in the correct subdirectory of the classpa
th.
Person p=new Person();
^
8 errors
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Ayanik Sil,

A servlet is no different than any other Java class that has dependencies.
Those dependencies are listed in this faq.
http://faq.javaranch.com/java/CompilingServlets

Because this is more of a javac/classpath issue than something specific to the servlet spec, I'm going to move it (back) to our Java In General (Beginner) forum where compilation and classpath issues are best discussed.
[ November 26, 2007: Message edited by: Ben Souther ]

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Ayanik Sil
Greenhorn

Joined: Sep 23, 2007
Posts: 27
Dear Bert Bates,
I am a fan of yours.I am reading HeadFirst servlets and JSP.
I have and always had problems with classpath.this is a nagging problem.
Some days ago i compiled servlets,and ran the programs successfully with Tomcat,as written in your book,but this same problem has occured again.I dont know what goes wrong.
Hence i am asking for help.
Regards,
Ayanik
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Ayanik Sil,

You're too quick for me.
You responded before I had a chance to move this thread.

I think your last post re-enforces my point.
Before tackling servlet development, it is important that you are comfortable with the fundamentals of Java itself. Specifically, you should understand javac, packages, and classpaths.
In my opinion, these things are best learned on plain old java objects with a text editor and the command line compiler 'javac'.

Moving back to Java In General
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

I see two problems here.

1.) The javax.servlet... classes (dependencies) are not on your classpath.
How are you passing your classpath to javac?

2.) It looks like you may have a java file with a different name or package than what is indicated in its source

newBean.java:11: cannot access foo.Person
bad class file: .\foo\Person.java
file does not contain class foo.Person
Please remove or make sure it appears in the correct subdirectory of the classpath


Check that the package statement and the class signature are correct.

They should be:

and

respectively.
hemant chaudhary
Greenhorn

Joined: Feb 22, 2007
Posts: 20
@ayanik:

Can you see if this jar 'sevlet-api.jar' is there in lib directory,please tell us if it is there or not.
Ayanik Sil
Greenhorn

Joined: Sep 23, 2007
Posts: 27
Dear Ben,
under WEB-INF/classes,there is a package called foo where there is a class
called Person.java

the source code is
package foo;
public class Person {
private String name;
public String getName() {
return name;
}
public void setName(String name) {

this.name=name;

}}

I tried to import the package in my earlier code.

Also,
n my classpath i have the servlet-api.jar,but it is still not compiling.
when i give echo %classpath%
i have
C:\Program Files\Apache Software Foundation\Tomcat 5.5\common\lib\servlet-api.jar

I corrected the files and here is the error message.
newBean.java:1: package foo does not exist
import foo.*;
^
newBean.java:8: cannot find symbol
symbol : class IOException
location: class newBean
throws ServletException,IOException {
^
newBean.java:10: cannot find symbol
symbol : class Person
location: class newBean
Person p=new Person();
^
newBean.java:10: cannot find symbol
symbol : class Person
location: class newBean
Person p=new Person();
^
4 errors
Ayanik Sil
Greenhorn

Joined: Sep 23, 2007
Posts: 27
Hemant,Servlet api.jar is in the lib directory.
Ayanik Sil
Greenhorn

Joined: Sep 23, 2007
Posts: 27
Hello friends,
the problem is with import statements actually.
if for example,
i write file BeerSelect.java in com/example/web directory
where I import BeerExpert.java from com/example/model directory,
the compiler cannot find it.

the java version is
java version "1.5.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-b64)
Java HotSpot(TM) Client VM (build 1.5.0-b64, mixed mode, sharing)
I have never faced such odd problems before.

here are the source codes
package com.example.web;

import com.example.model.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class BeerSelect extends HttpServlet {
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException {

response.setContentType("text/html");
PrintWriter out=response.getWriter();
out.println("Beer Selection Advice<br>");
String c=request.getParameter("color");
out.println("<br>Got Beer Color "+c);

BeerExpert be=new BeerExpert();
List.getBrands(c);
Iterator it=result.iterator();
while(it.hasNext())
{

out.println("<br> try : "+it.next());
}


package com.example.model;

import java.util.*;

public class BeerExpert {
public List getBrands(String color) {
List brands=new ArrayList();
if (color.equals("amber")) {
brands.add("Jack Amber");
brands.add("Red Moose");
}


else {
brands.add("Jail Pale Ale");
brands.add("Gout Stout");
}
return brands;

}}

Please help.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

You need to also have "." (dot), representing the current directory, on your classpath, or the compiler won't be able to find your other files. So the classpath should be at least

.;C:\Program Files\Apache Software Foundation\Tomcat 5.5\common\lib\servlet-api.jar


[Jess in Action][AskingGoodQuestions]
Ayanik Sil
Greenhorn

Joined: Sep 23, 2007
Posts: 27
Thanks Sir,
Many many thanks!
It worked.
 
GeeCON Prague 2014
 
subject: Servlet Compilation