jQuery in Action, 2nd edition*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes JavaBean from a servlet  - still not resolved Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "JavaBean from a servlet  - still not resolved " Watch "JavaBean from a servlet  - still not resolved " New topic
Author

JavaBean from a servlet - still not resolved

H Singh
Ranch Hand

Joined: Apr 03, 2005
Posts: 34
I am trying JavaBean for the first time- should be simple but i am not getting it

My servlet and JavaBean are in the same directory (\MyProjects\Person)

servlet code:

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

Person p = new Person();
p.setName("Evan");
request.setAttribute("person",p);

RequestDispatcher view = request.getRequestDispatcher("result.jsp");
view.forward(request,response);
}
}

Javabean:

public class Person{
private String name;

public void setName(String name){
this.name = name;
}

public String getName(){
return name;
}
}

I compiled Person.java first and class file exists in the same directory but when i try to compile PersonServlet.java, i get

PersonServlet.java:12: cannot resolve symbol
symbol : class Person
location: class PersonServlet
Person p = new Person();
^
PersonServlet.java:12: cannot resolve symbol
symbol : class Person
location: class PersonServlet
Person p = new Person();

Thanks

[ June 12, 2005: Message edited by: HS ]

[ June 12, 2005: Message edited by: HS ]
[ June 14, 2005: Message edited by: HS ]
Sanjay pts
Ranch Hand

Joined: Nov 07, 2000
Posts: 357
Try to define package name at top of both classe using

package name;


Eat JAVA, Drink JAVA, Sleep Java
H Singh
Ranch Hand

Joined: Apr 03, 2005
Posts: 34
I have already tried that, same results.....
Sanjay pts
Ranch Hand

Joined: Nov 07, 2000
Posts: 357
I got it

set classpath till calsses dierctory

means

on command line say

C:\Program Files\Apache Software Foundation\Tomcat 5.5\webapps\testJSP\WEB-INF\classes;

it will definately work
bye
H Singh
Ranch Hand

Joined: Apr 03, 2005
Posts: 34
I specified the classpath, as you have suggested but still it does not work...

Thanks
shiva viswanathan
Ranch Hand

Joined: Aug 12, 2004
Posts: 152
HI HS

Can you please post your exact code with the imports as well as the exact directory structure otherwise it is difficult for us to help
H Singh
Ranch Hand

Joined: Apr 03, 2005
Posts: 34
Hey Shiva:

Code is under : C:\Javacode\MyProjects\Persons

Person.java :-

package Persons;

public class Person{
private String name;

public void setName(String name){
this.name = name;
}

public String getName(){
return name;
}
}

PersonServlet.java :-


package Persons;

import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

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

Person p = new Person();
p.setName("Evan");
request.setAttribute("person",p);

RequestDispatcher view = request.getRequestDispatcher("result.jsp");
view.forward(request,response);
}
}

result.jsp :-

<html><body>
Person is: <%= request.getAttribute("person") %>
</body></html>

Set classpath to C:\Javacode\MyProjects\Persons

Compiled Person.java but get following when compiling PersonServlet.java:

PersonServlet.java:12: cannot resolve symbol
symbol : class Person
location: class PersonServlet
Person p = new Person();

Thanks a lot
shiva viswanathan
Ranch Hand

Joined: Aug 12, 2004
Posts: 152
Hi HS ,
just tried your code . you dont need to set your classpath to classes

Make sure you set the servlet jar in classpath
then run the follwoing commands

javac -d . Persons/Person.java
javac -d . Persons/PersonServlet.java

Make sure you issue this command from C:\Javacode\MyProjects

I have tried this and it compiles properly

Please tell me once it works

Thanks
Shiva
H Singh
Ranch Hand

Joined: Apr 03, 2005
Posts: 34
Hey Shiva:

It worked ..thanks a lot
[ June 14, 2005: Message edited by: HS ]
 
Don't get me started about those stupid light bulbs.
 
subject: JavaBean from a servlet - still not resolved
 
Similar Threads
getProperty question
calling bean method in servlet
JSP - cannot resolve symbol
accessing beans in servlet
Servlet.service() for servlet jsp threw exception java.lang.IllegalStateException