Forums Register Login

Servlet Compilation

+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
let's slide this over to the Servlets forum
+Pie Number of slices to send: Send
Ayanik,
What compiler error do you get?
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
@ayanik:

Can you see if this jar 'sevlet-api.jar' is there in lib directory,please tell us if it is there or not.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
Hemant,Servlet api.jar is in the lib directory.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
Thanks Sir,
Many many thanks!
It worked.
I guess I've been abducted by space aliens. So unprofessional. They tried to probe me with this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1649 times.
Similar Threads
Servlet compiling problem in tomcat
Package does not exist ?
Compiling servlet file
Problem: requested resource not available
please help! classpath not working
JSP - cannot resolve symbol
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 14:43:10.