Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Servlet calling a class in a package

Status
Not open for further replies.

weaver12

Programmer
Jul 1, 2005
5
US
I have a simple class asdf in package foo.bar:
Code:
package foo.bar;

public class asdf
{
    private String val;

    public asdf()
    {
        val = "";
    }

    public void setValue(String v)
    {
        val = v;
    }

    public String getValue()
    {
        return val;
    }
}

I'm trying to write a servlet to call the class
Code:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

import foo.bar;

public class servletTest extends HttpServlet {

    public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
        throws IOException, ServletException
    {
        response.setContentType("text/html");

        PrintWriter out = response.getWriter();

        asdf testing = new asdf();

        testing.setValue("hello from servlet!");

        out.println(test.getValue());
    }

    public void doPost(HttpServletRequest request,
                      HttpServletResponse response)
        throws IOException, ServletException
    {    
        doGet(request, response);
    }
}

When i import foo.bar into the servlet i get: "package foo does not exist".

my dir structure is:
webapps/site/WEB-INF/classes/servletTest
webapps/site/WEB-INF/classes/foo/bar/asdf

Is this not possible? What am i doing wrong?

My goal is to have a class that both my jsp and servlets can call.
 
Hello,

Try this...
Code:
[blue]import foo.bar.*;[/blue]

I believe it is not seeing the class because you only have foo.bar;

Tell me it that works..

Thanks,
Ron


typedef map<GiantX,gold, less<std::shortestpathtogold> > AwesomeMap;
 
ugh, i hate typo's. thanks ron that plus setting my CLASSPATH=webapps/site/WEB-INF/classes worked. Sadly I don't think this is going to work for me. I was hoping it init the class in the servlet and have it set for the entire session so i can call the class and values from jsp.

example::
servlet: setValue("hello world");
jsp: String v = getValue();

thanks!
 
Your dir structure (if it really is as you agve said) should not be :

webapps/site/WEB-INF/classes/foo/bar/asdf

but should be :

webapps/site/WEB-INF/classes/foo/bar/

where "asdf.class" is in that directory. (nice class name BTW !).

Also, you should look into bundling your classes into a jar file and putting it in WEB-INF/lib.

--------------------------------------------------
Free Database Connection Pooling Software
 
sorry i didn't think before i posted, the filename is asdf.class in the directory /.../foo/bar/. Eventually i'll put it all into a jar but I wanted to see if it was possible to call class values set in a servlet from a jsp page. So far no luck.

What i'm really trying to do is have a servlet login a user and set his security for what "sections" and "features" he has access to. Then the jsp pages would actually serve the pages up using the security set by the class...

hope that makes some sense... :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top