Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Servlet Compilation

 
Ayanik Sil
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8898
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
let's slide this over to the Servlets forum
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34071
331
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ayanik,
What compiler error do you get?
 
Ayanik Sil
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ayanik Sil
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hemant,Servlet api.jar is in the lib directory.
 
Ayanik Sil
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ayanik Sil
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Sir,
Many many thanks!
It worked.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic