Importing classes in jsp

In jakarta tomcat servlet engine, in which directory should I put a class file so that i can call it from a jsp located in /examples/jsp or where should I put the class file so that it can be called using <jsp:useBean> tag

In jakarta tomcat servlet engine, in which directory
should I put a class file so that i can call it from a
jsp located in /examples/jsp or where should I put the
class file so that it can be called using
<jsp:useBean> tagI don't use Tomcat at the moment, but I think it's the same with resin and other stuff.
You have a path called "/WEB-INF/classes" somewhere under your tomcat root. This is the location where you should store your classes and packages. The servers using only this path to locate classes, not your classpath.
I hope, this helps.

Similar Messages

  • Can't import class into JSP

    Hi,
    I am trying to import a class into my JSP and am getting the following error at runtime:
    org.apache.jasper.JasperException: Unable to compile class for JSP
    An error occurred at line: -1 in the jsp file: null
    Generated servlet error:
    [javac] Compiling 1 source file
    [javac] D:\jwsdp-1.2\work\Catalina\localhost\pe\org\apache\jsp\guestbook_jsp.java:7: '.' expected
    [javac] import MessageGetter;
    My import statement in the JSP is simply:
    <%@ page import="MessageGetter" %>
    and I am placing the class in the WEB-INF/classes directory. This is stumping me - if you've come across this or a similar problem, please let me know. Thanks.
    P

    Try posting your question in the JSP forum (http://forum.java.sun.com/forum.jsp?forum=45)
    cheers,
    Mike

  • ACK! Importing classes in JSP?!?! HELP!!!

    I am REALLY new to the JSP world and just finished creating a java applet that creates a graph and two combo boxes containing information that I need to display on a web page.
    The problem is that I have just been told to convert it to JSP? Is this possible? If it IS possible, how do you import a public class into JSP?
    Thanks sooooo much for your help!!!!!
    Adam

    Answer is yes you can import a java class
    Here is how it works
    use the following
    <%@ page import="com.temp.YourClass*" %>
    Assuming that you classpath is set properly
    Thanks
    Sampath Thummati.

  • Importing class to JSP in websphere (newbie)

    Hi I am trying to figure out how to properly import a class in a JSP.
    The DocumentRoot directory is set in Websphere and that is where my JSP's reside. I have created a folder off of the DocumentRoot directory called classes and placed my .class file in there. I then set the classes directory in the classpath (and restarted the appserver)
    I have an import statement in my jsp for the class.
    I am now getting an error that says it is unable to compile because of null. Any ideas????
    Below is a test piece of JSP code and the class code:
    <%@ page import="java.io.*, java.util.*, java.text.*, IPRStat, java.net.*,java.sql.*"%>
    <HTML>
    <BODY>
    <%
    try {
    String IPRNum = "001-0001";
    String IPRStatus = "OK";
    IPRStat Testing = new IPRStat(IPRNum, IPRStatus);
    } catch (Exception e) {
    out.println("Error detected " + e.getMessage());
    %>
    </BODY>
    </HTML>
    class IPRStat
    public IPRStat(String i, String d )
    iprnum = i;
    status = d;
    public String getIPRNum()
    return iprnum;
    public String getStatus()
    return status;
    private String iprnum;
    private String status;
    -----------------------------------------------------

    DocumentRoot (looks something like: /aibm/Userdata/WebAsAdv/default/mydev) is where my JSP's live (i.e.)
    /aibm/Userdata/WebAsAdv/default/mydev/testidea.jsp
    and I made a directory off of DocumentRoot called classes:
    /aibm/Userdata/WebAsAdv/default/mydev/classes
    And copied IPRStat.class into the classes directory.
    /aibm/Userdata/WebAsAdv/default/mydev/classes/IPRStat.class
    I then put the path (/aibm/Userdata/WebAsAdv/default/mydev/classes) in the classpath and restarted the appserver.
    At this point there is no Web-INF that I can see. I am new to Websphere myself. Is version 3.5.4 standard edition. I am using the websphere admin console to set the classpath.

  • Importing class or jsp:useBean option

    Hi,
    I am bit confused as when to use between "beanClasses" or "tag librery" or "simple java classes useing import". as far as i feel all are capable of doing almost each others task (i.e excuting a method to process something). can anyone help me in understanding this

    Well, first, if I understand correctly,
    "simple java classes useing import"Would be using scriptlets to do the job. It is always best to avoid scriptlets, and the best way to do that is to
    use javabeans. So I would say use "beanClasses" over "simple java classes useing import" whenever humanly possible.
    On the other hand, I use tags and beans for different reasons. I use beans to transport data from one spot to another, such as through pages and requests, or to access non-display logic, like getting data from a database. I use tags mainly for complex display operations.
    For instance, lets use the oft-asked Paging question. I have a database with 500 records. I want to display 20 at a time in a JSP. I would use a Bean to get the information (and store it in a List of another Bean type). Then I would use a tag to do the [prev] 1 2 3 4 5 6 7 8 9 10 ... [next] display and links.
    Hi,
    I am bit confused as when to use between
    "beanClasses" or "tag librery" or . as far as i feel all are
    capable of doing almost each others task (i.e
    excuting a method to process something). can anyone
    help me in understanding this

  • Importing classes into JSP page

    Hi There,
    I am new to this and here is my problem.
    I am doing a shopping cart tutorial and trying to get a JSP page to import two classes :
    <%@ page language="java" contentType="text/html"
    import="ShoppingBasket,Product"
    errorPage="errorpage.jsp" %>
    I get :
    C:\jakarta-tomcat-4.1.30\work\Standalone\localhost\_\shop_0002dbasket_jsp.java:7: '.' expected
    import ShoppingBasket;
    ^
    C:\jakarta-tomcat-4.1.30\work\Standalone\localhost\_\shop_0002dbasket_jsp.java:8: '.' expected
    import Product;
    ^
    C:\jakarta-tomcat-4.1.30\work\Standalone\localhost\_\shop_0002dbasket_jsp.java:50: cannot resolve symbol
    symbol : class ShoppingBasket
    location: class org.apache.jsp.shop_0002dbasket_jsp
    ShoppingBasket basket = null;
    The classes compile ok . I read that instead of comma seperating the classes it might be better to put them in a package but I do not know the correct syntax or where to put the package if this is correct.
    The tutorial is from this book and the shopping cart source code is here.
    http://www.ineasysteps.com/books/?1840781971
    Any help appreciated
    Jim Ascroft

    A little beginners explanation of classpaths and packages is in order here. A package works like an extension of the name to keep class names unique. For example, if you have a ShoppingBasket.class and you want to use my ShoppingBasket.class along with it the two class names must be unique or different from one another in some way. Yours could be in com.yourcompany package and mine could be in org.mycompany package. When you reference you Shopping backet in your code you would use com.yourcompany.ShoppingBasket and when you reference mine in the same code you would use or.mycompany..ShoppingBasket. Shorthand for the class can be used by adding an import at the top of the referencing java file. An import is like saying "hay compiler, whenever you see ShoppingBasket look for it in the com.mycompany package!" That brings us to the next point. The package name must always be reflected in the directory structure of the .class file location in order to be found. Your shopping basket must be stored under the directory com/yourcompany if it is in package com.yourcompany. That directory structure should extend off of a classpath entry in order to be made available to the JVM. Classpath entries point to folders, jar or zip files that contain packaged and unpackaged classes. So If you have a classpath entry that points to C:\jakarta-tomcat-4.1.30\webapps\ROOT\WEB-INF\classes then all unpackaged classes (classes compiled from a .java file without a package statement) will be pulled directly out of this folder. Also, packaged classes will be pulled out of they're packaging directory structure, which should be located in this folder. You can have as many classpath entries as you want but the classes will be pulled out of the first entry that they can be found under. Now with a web app you don't normally fuss with the setting of classpaths since they are already predefined and configured for you. You just need to know to put all of your class files under the WEB-INF/classes directory in the root of your web app. You do, however, still need to know how they work. You also should know that any jar files should be placed in the lib folder of your web app and not under the classes folder.
    Getting back to your immediate problem, why are you getting class not found errors? Well this stems from what was said above. To fix these kind of problems follow these steps:
    1st off, find out exactly which .java file the .class file was compiled from. (This can be a problem in some environments where multiple copies of source hang around.) This step may involve a recompile and paying close attention to the compiler arguments. In an IDE there should be a setting or dialog that lets you find out exactly where it's placing the .class files created from you source.
    Next, determine what package the .class file should be in by examining (and maybe changing) the package statement at the top. For example, if you intended to put ShoppingBasket in the com.bloodoo package make sure it has a package "com.bloodoo;" statement at the top.
    Next recompile the .java source and move the packaging directory structure into the classes folder of your app. (This would be WEB-INF/classes under the root of the web-app.) For example if ShoppingBasket.class was compiled into the com/bloodoo folder under C:\Program Files\YourIDE\projects\yourproject\output then copy the com folder out of the output folder and into the WEB-INF\classes folder.
    Next examine the calling code (JSP or servlet .java code) and make sure it references the problem class out of the right package. For example, yourpage.jsp should reference the class com.bloodoo.ShoppingBasket and not ShoppingBasket if the shopping basket is deployed in package com/bloodoo. This gets tricky with jsp usebean tags because the desire is to use shorthand but I'm not sure if that works with the use bean tag. Make any modifications and recompile if necessary.
    Lastly, as a rule of thumb avoid wild-card imports (eg. import com.package.*) because they are a common cause of confusion especially when refactoring package names and resolving these kind of issues.Those steps should allow you to resolve 85-90% of your class not found issues. The other 10-15% are rare cases involving the use of custom classloaders, the Java extension mechanism and/or classpath trickery. If you still have questions/problems post back.
    Cliff

  • Problem importing classes and beans

    Hey there. Im having one major fustrating problem! When I code supporting classes and beans for my JSPs I get a code 500 internal server error when trying to import (via <%@ page import="class" %> and <jsp:useBean/>) Im storing my classes and beans in the WEB-INF folder and the calling JSPs are located in /ROOT/tests/8/jsp.jsp. Im using the following to import a class or bean:
    <%@ import="aClass" %>
    Seen as tho its in the WEB-INF folder I won't have to explicitly refer to where the class is located, just the class name.
    I never had this problem when I was using my hosting service. Its only on my localhost server in which I get the Internal Server error.
    Help appreciated, thx.
    PS: Im quite new to JSP/Java Servlet.

    import (via <%@ page import="class" %> and
    <jsp:useBean/>) Im storing my classes and beans in the
    WEB-INF folder try put your class file in WEB-INF/classes.
    or first put bean in the package, like WEB-INF/classes/packagename/beanclass
    in jsp page:
    <jsp:useBean id="Mybean" class="packagename.beanclass" scope="request" />
    Question: is /ROOT a context entry in your server.xml?
    Which JSP Container (version) you use? Maybe your localhost server's set up is different with your hosting.

  • How to import  my own class in jsp file

    I have a jsp file in jsp\ directory and a class in web-inf\classes\ directory. How do I import the class in jsp so i can use it in my jsp file. Thanks,

    This is your lucky day, wennie.
    The correct forum to post this is:
    [http://forums.sun.com/forum.jspa?forumID=45]

  • Jsp :: Importing Classes

    I have followed a jsp tutorial with regards to a simple set of scripts.
    I have stored a class in the /cyb/WEB-INF/NameHandler folder that is called NameHandler.class
    When I call the import statement in the index.jsp file:
    <%@page import="hello.NameHandler"%>
    I get the following error:
    "Only a type can be imported. hello.NameHandler resolves to a package"
    After some research, I have ensured that the NameHandler is compiled, and stored in the apprpriate WEB-INF/Classes/NameHandler directory within the tomcat system.
    Does this have anything to do with the tomcat's classpath directive?
    I am using a windows xp platform.
    Edited by: Cyberconian on Feb 11, 2009 4:49 AM

    Hi,
    Your class name is NameHandler and you are trying to import the class in your JSP page as hello.NameHandler.
    Syntax of page directive to import the class in JSP looks fine (still use spaces, like <%@ page import="java.util.*" %>). Please make sure hello.NameHandler (hello is the package) class is available in WEB-INF/classes directory of your context root.
    Thanks,
    Mrityunjoy

  • Importing class in a jsp using Tomcat

    Hello All,
    Using Tomcat:
    I have class here:
    examples/WEB-INF/classes/ package/myclass.class
    I have a .jsp here:
    examples/JSPFolder/myjspfile.jsp
    I want to import the class into the jsp.
    I have tried the following combinations of importy lines based on previous posts on these boards and each have failed with same error:
    Unable to load class for JSP
    <%@ page import="myclass" %>
    <%@ page import="myclass.*" %>
    <%@ page import="package.myclass" %>
    <%@ page import="package.myclass.*" %>
    <%@ page import="WEB-INF/classes/package/myclass%>
    I have seen a number of posts for this same problem, and have tried those solutions as well, but none work. I am sure that this is simply a problem of finding the right directory, as I can successfully run the .jsp file iwhen I remove the import line.
    Can someone please help?

    There is a folder for shared packages
    tomcat_root\lib; or
    tomcat_root\common
    also most versions have a lib or common folder for each webapp for packages that are not to be shared.
    tomcat_root\webapps\appname\web-inf\lib
    Agreed that it is strange to allow servlets to import from the classes foler but not JSPs. It should make no difference but it is convension to place single classes in the same folder as the app's directory and packages (especially jar's) in the lib directory.

  • Using an imported class in a JSP file

    When you import a class you wrote into a JSP file, is there a way to submit values from forms to the entire class, or can you only use the methods within the class??

    When you import a class you wrote into a JSP file, is
    there a way to submit values from forms to the entire
    class, or can you only use the methods within the
    class??Yes, there is a way in JSP. You can use,
    <jsp:useBean id="your_bean_id" scope="page or request etc" class="your_class">
    <jsp:setProperty name="your_bean_id" property="*"/>
    </jsp:useBean>
    So, <jsp:setProperty> sets all the values inside your request object into the bean, if the class is having the properties (variables) with same name as of request object's parameters. You must have coded set method for all the properties whatever you want your jsp to set the values in the bean.
    if your request is .../yourjsp?name=xxxx&age=25
    then <jsp:setProperty> method will set the name and age inside bean if you have,
    public class YourBean {
    String name;
    int age;
    public void setName(String name) {
    this.name=name;
    public void setAge(int age) {
    this.age=age;
    Hope this helps.
    Sudha

  • NullPointerException - importing my own class to JSP

    Hi.
    I'm writing a JSP page. I also made a class in WEB-INF/classes/org/MyXml.class. When I try to run this class from console, there is no problems at all. But if i try to use function from that class in JSP, i get this (the same error i get whichever function i try to use (btw, all functions are static, as they should be, right?)):
    java.lang.NullPointerException
         org.MyXml.CountProducts(MyXml.java:46)
         org.apache.jsp.index_jsp._jspService(index_jsp.java:75)
         org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
         javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
         org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:324)
         org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
         org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
         javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
    I used this class like this:
    <%@ page import="org.MyXml" %>
    <%
    MyXml.CountProducts();
    %>
    I'm also a bit confused, it says that error is on line 46. Well, there's just a comment!
    Please help. Thanks.

    pqeuens,
    MyXml is his class. his methods are static, so, import="org.MyXml.*" won't work. It would either be import="org.*" or import="org.MyXml" (which he has)
    paull1911
    "What file are you looking at to find line 46? It is the java file (a servlet) corresponding to the jsp file."
    It should actually be in the org.MyXml.java file. The stack trace line is:
    java.lang.NullPointerException
    org.MyXml.CountProducts(MyXml.java:46)
    someone_
    You know from the above error line that the error is in MyXml.CountProducts method. After making sure you are working with the most up-to-date version of the class, (if that doesn't help you get a more accurate line number) walk through the CountProducts method and see where you might be trying to access an object that has not yet been instantiated (either using someObject.someMethodOrMember, or someArray[index])

  • HELP: Import my own class in JSP ???

    Hi, all!
    I read many previous topics in this forum but no one works for me!
    Please someone help me!
    I'm using Tomcat 4.1.12 and my JSP scripts work just fine, but I need to import my own .class file. I just don't know where to put it so Tomcat can find it!?
    I have this environment variable:
    JAVA_HOME=.;c:\jdk1.3.1
    And in my code I'want to place something like this:
    <%@ page import="myClass" %>
    But I get a "can not resolve symbol" exception.
    Thanks for readind this, and please help if you can!
    Bye.
    adriano

    Within the Tomcat directory, under your application directory place the class file/s into the WEB-INF/classes directory. If your classes are part of a package place the entire directory structure of the package under the WEB-INF/classes directory.
    The import in the JSP is the same as for any Java class.
    Hope this solves your problem.

  • Importing classes for a jsp

    Hi,
    I'm trying to get a jsp working. This is part of the code of test.jsp:
    <%@page import="First, Second" %>
    <HTML>
    <HEAD>
    <TITLE>JSP Example 3</TITLE>
    </HEAD>
    <BODY BGCOLOR="#FFFFCC">
    <CENTER>
    <H2>Date and Time</H2>
    First.hello() is: <% First.hello(); %>
    Second.hi() is:  <% Second.hi(); %>
    </CENTER>
    </BODY>
    </HTML>This is the output of the compilation in Tomcat:
    org.apache.jasper.JasperException: Unable to compile class for JSP:
    An error occurred at line: 6 in the generated java file
    The import First cannot be resolved
    An error occurred at line: 7 in the generated java file
    The import Second cannot be resolved
    An error occurred at line: 9 in the jsp file: /date3.jsp
    First cannot be resolved
    6: <BODY BGCOLOR="#FFFFCC">
    7: <CENTER>
    8: <H2>Date and Time</H2>
    9: First.hello() is: <% First.hello(); %>
    10: Second.hi() is:  <% Second.hi(); %>
    11: </CENTER>
    12: </BODY>First.class and Second.class are the two files who share the directory with test.jsp
    I'm flabergasted with this error. As I am using very basic code for this example you would think it works (or is this error one of the NullPointerException kind?).
    Abel
    Edited by: Abel on Dec 12, 2007 6:21 AM
    Changed to a more basic type of example

    I found the answer myself ~:-).
    Add the files you want to import to a jar file
    * To be more precise, in the directory I have the jsp, I created first a directory WEB-INF, and in that directory created a directory lib, and finally created a directory test. I moved the two Java files to the test directory. I changed the Java files (added a package statement). So the configuration is now:
    <%@page import="test.*" %>
    <HTML>
    <HEAD>
    <TITLE>JSP Example 3</TITLE>
    </HEAD>
    <BODY BGCOLOR="#FFFFCC">
    <CENTER>
    <H2>Date and Time</H2>
    <%
       First first = new First();
       out.println ("First says: " + first.hello());
    %>
    </CENTER>
    </BODY>
    </HTML>And the two java files:
    package test;
    public class First {
         public String hello() {
              return ("Hello from First");
    package test;
    public class Second {
         public String hi() {
              return ("Hi from Second");
    }And finally I added the two classes I got after compilation to a jar file in the WEB-INF\lib directory:
    jar -cvf test.jar test\First.class test\Second.classI just explain what I did wrong, as I saw many comparable posts without an answer. And this basic problem can keep you away from the real work.
    Abel.

  • Importing classes into a jsp

    I have created a class file called BindListener.java and put it in my root directory in my web module. I cannot however get the import statement for the jsp right, my root directory is called JBuilder. I have tried putting it in the WEB-INF classes folder also but still when I try to execute thr jsp it dosent work.
    There is no package declared in the classs file and it does not extend any other class, however it implements the HttpSessionBindingListener interface.
    I can call servlets from my JSP in the WEB-INF classes dir using /JBuilder/ServletName
    I am mondo confused please help

    I have created a class file called BindListener.java
    and put it in my root directory in my web module. I
    cannot however get the import statement for the jsp
    right, my root directory is called JBuilder. I have
    tried putting it in the WEB-INF classes folder also
    but still when I try to execute thr jsp it dosent
    work.
    There is no package declared in the classs file and
    it does not extend any other class, however it
    implements the HttpSessionBindingListener interface.You have to put the class in a package, then put the .class file in a package directory under WEB-INF/classes. So for example:
    package beans;
    public class BindListener ...
    //goes into this directory structure
    <APP_ROOT>/
      *.jsp
      WEB-INF/
        web.xml
        classes/
          beans/
            BindListener.class
    //And is imported into your JSP using one of the following
    // -- EITHER --
    <%@ page import="beans.BindListener" %>
    <%
      BindListener bl = new BindListener();
    %>
    // -- OR --
    <jsp:useBean id="bl" class="beans.BindingListener"/>>
    I can call servlets from my JSP in the WEB-INF
    classes dir using /JBuilder/ServletName
    I am mondo confused please help

Maybe you are looking for