Import statement in .java files and .jsp files

Guys I have few mysteries about this import statement.
Please correct me if I am wrong.
1)Suppose if I have a folder c:\SourceFolder it has one.java and two.java files.If I compile one.java then one.class files is created in c:\ClassFolder .Now two.java uses one of the method of one.java,and without using import statement I am able to compile two.java, and two.class file is created in c:\ClassFolder.I assume that the path of one.class was taken from the CLASSPATH environment variable,hence I there was no need for import statement .........am i right ?
BUT... in My.jsp,which is in C:\JspFolder, if I want to use one.class,then I have to specifically import the class I want to use.That is i have to say
<%@page import="one"%>
and since the classpath is C:\ClassFolder ,it finds one.class in the C:\ClassFolder.(Note that My.class is created in a folder different from c:\ClassFolder )
But I am wondering why was there no need for me to import one.class in two.java.
(Note I am using JDeveloper and Apache server)
Please help.
In this case the pSo I am wondering in JSP why it doesn't take the class I am looking for from the classpath .
Help.

You have to understand Java scoping rules. In the 1st scenario with the two Java files I am guessing neither one of them begins with a package statement. This means that they belong to the default package. When you compile the compiler uses the classpath to resolve method calls looking in the default package since there was no import statement specified in two.java. JSP is a little different. Now I just read this here in these forums a couple of days ago. Since the classfiles generated from the JSP files are not in the CLASSPATH they are loaded by a different classloader. This is why you have to import all references to all classes in the default package.

Similar Messages

  • Import statement in .java files and .jsp files ..correction

    Guys I have few mysteries about this import statement.
    Please correct me if I am wrong.
    1)Suppose if I have a folder c:\SourceFolder it has one.java and two.java files.If I compile one.java then one.class files is created in c:\ClassFolder .Now two.java uses one of the method of one.java,and without using import statement in two.java I am able to compile two.java, and two.class file is created in c:\ClassFolder.I assume that the path of one.class was taken from the CLASSPATH environment variable,hence I there was no need for import statement .........am i right ?
    BUT... in My.jsp,which is in C:\JspFolder, if I want to use one.class,then I have to specifically import one.class in My.jsp .That is i have to say
    <%@page import="one"%>
    in My.jsp.
    and since the classpath is C:\ClassFolder ,it finds one.class in the C:\ClassFolder and MyJsp is compiled sucessfully.(Note that My.class is created in a folder different from c:\ClassFolder )
    But I am wondering why was there no need for me to import one.class in two.java.
    (Note I am using JDeveloper and Apache server)
    Please help.

    It has to do with packages. Most java classes are in a package, the name of which must conform to its place on the filesystem relative to the classpath. By that I mean that if you have com.mystuff.One.java, it must be in a folder com/mystuff where com is located somewhere in the classpath.
    What you've done is a little different. I'm assuming a couple of things:
    1. you have no package declaration at the top of one.java or two.java
    2. you have the current directory "." in your classpath.
    Java has the concept of the "default package", which covers classes without a declared package, and in your case is the current directory.
    So when you're in c:\sourcefolder and run the compiler, then "."="c:\sourcefolder", and that directory is part of the default package. No import statements are necessary for classes that are in the same package. This is why two.java can call methods in one.java without an import statement.
    When you run your jsp, the "current directory" part of your classpath is not c:\sourcefolder, but some other value (probably the directory you start your jsp engine from) You will have to import all non-java-library classes because the jsp itself becomes a java class, with a package that is determined by the jsp engine.

  • How to organize the import statements in FXML Files?

    Hello,
    I'm a user of NetBeans IDE and it can organize the import statements in *.java files. Now, I'm using JavaFX and editing the *.fxml files as well. I want to know how to organize the import statements in *.fxml files?
    Thanks a lot.

    You should use Scene Builder, it will make all these import statements for you.
    Moreover, it will greatly simplify the writing of your FXML files, since you only have to Drag&Drop JavaFX objects on the scene. Then, it automatically generates FXML files.

  • Hi frnds i want to help in servlet,java bean and JSP

    hi friends i'm right now in M.SC(IT) and i want to do project in SERVLET,,JAVA BEANS and JSP ,
    so pls give me a title about that
    NOTE: I develop a project in group(2 persons there)
    my email id is : [email protected] , [email protected] , [email protected]

    You cannot pair your iPod to a cell phone, so forget about it.
    The only way you can get free WiFi is to hang out at a Denny's, a Starbucks, or a truck stop, and I don't think your parents would approve....

  • Java code and jsp interaction

    Hi,
    How does one interact between java code and jsp page.That is, if the java code does say database connection, the web page should tell that the connection has been made,just like an application but this will work on the web server not in the console.Kindly let me know.
    As

    Just curious but where do these questions come from? A course you are taking or you are building something for a hobby?
    If you are building some small simple web-app it's probably quicker to embed code right onto your JSP page.
    There are various levels of abstracting your web-app separating display from logic from the model.
    The following are techniques from simple web-apps to more complicated ones.
    -Embedding script right into your JSP
    -Using beans
    -Using custom tags ( I think there are some standard ones now, struts seems to be a popular buzzword)
    -Including a servlet to handle your requests redirect, update bean content grab info form your database and forward to a JSP. (Read up on model 2 architecture.)
    To include as much detail as possible would be to write a book. Fortunately many have so I don't have to fit one in this tiny reply box.
    Hope that was somewhat useful.

  • Serving Java Servlets and JSP

    I have a small hosting company and was wondering what is required to be installed on a Win2k Server to host Java Servlets and JSP pages for a client of mine?

    Ah, so you just want to add a servlet engine to IIS5?
    Tomcat can be used as a plugin for IIS. Check out the Tomcat FAQs - somewhere in there you should find one relating to using Tomcat as an IIS plugin. They're far more comprehensive than I could ever hope to be on the matter!

  • Cannot import self-defined .java files?

    Hi,
    I created a MouseHandler.java file that contains a class (without main() ). and I have another appletcode.java file that needs to import it:
    import MouseHandler;
    I tried to javac appletcode.java and it said it couldn't find the package MouseHandler but I didn't package it in the first place.
    I tried JBuilder to compile it and it too gave:
    "Class required but identifier found at line 1, column 1"
    How do I import my source file(if it is possible)?
    Thanks.

    As it stands, if the compiled .class file is in the
    same directory as your program, it should run fine
    without the import statement. Notice, the import is
    failing, but your references to it aren't?It's in the same directory. I took out the import statement and it gave error:
    class MouseHandler not found in class appletcode at line ..., column ..
    PS: this code is actually from Beginning Java 2 by Wrox. Since mine couldn't work, I downloaded their code. Their code didn't contain the import statement (which should work like you said), but when I compile their code, it gave the same error as above too.
    Assuming that code and directory is correct, is there any other possible reasons why this is happening?
    Thank you

  • JAVA PACKAGES and JSP

    i Have two problems one is i am designing a website which consists of a form. I am trying to write the Connectiviy and all other insert statements in seperate java packages and include them in JSP files.
    i Have two java packages one is DBUtil(having two java files both dealing with connectovity) and another one is StudentUtIl which has java files that open the connections with database connection and insert data into DB., I am having a big Problem here. How can i call the function in the other java package in the files in this java package. i have openDB()and Close() in the other Db and i want to call these functions in JAVA classes in the other package. How to include a package. Both these packages are in the same File system mounted on a Local directory

    Thank you for the reply. I am giving the code i have given the full class name . and now it is giving the following error :
    Cannot reference a non-static method connectDB() in a static context.
    I am also giving the code. Please do help me on this. i am a beginner in java.
    import java.sql.*;
    import java.util.*;
    import DButil.*;
    public class StudentManager {
    /** Creates a new instance of StudentManager */
    public StudentManager() {
    Connection conn = null;
    Statement cs = null;
    public Vector getStudent(){
    try{
    dbutil.connectDB();
    String Query = "Select St_Record, St_L_Name, St_F_Name, St_Major, St_Email_Address, St_SSN, Date, St_Company, St_Designation";
    cs = conn.createStatement();
    java.sql.ResultSet rs = cs.executeQuery(Query);
    Vector Studentvector = new Vector();
    while(rs.next()){
    Studentinfo Student = new Studentinfo();
    Student.setSt_Record(rs.getInt("St_Record"));
    Student.setSt_L_Name(rs.getString("St_L_Name"));
    Student.setSt_F_Name(rs.getString("St_F_Name"));
    Student.setSt_Major(rs.getString("St_Major"));
    Student.setSt_Email_Address(rs.getString("St_Email_Address"));
    Student.setSt_Company(rs.getString("St_Company"));
    Student.setSt_Designation(rs.getString("St_Designation"));
    Student.setDate(rs.getInt("Date"));
    Studentvector.add(Student);
    if( cs != null)
    cs.close();
    if( conn != null && !conn.isClosed())
    conn.close();
    return Studentvector;
    }catch(Exception ignore){
    return null;
    }finally {
    dbutil.closeDB();
    import java.sql.*;
    import java.util.*;
    public class dbutil {
    /** Creates a new instance of dbutil */
    public dbutil() {
    Connection conn;
    public void connectDB(){
    conn = ConnectionManager.getConnection();
    public void closeDB(){
    try{
    if(conn != null && !conn.isClosed())
    conn.close();
    }catch(Exception excep){
    The main error is occuring at the following lines connectDB() and closeDB() in the class student manager. The class dbutil is in an another package.with an another file called connectionManager which establishes the connection with DB. The dbutil has the openconnection and close connection methods. I have not yet written the insert statements in StudentManager. PLease do Help me

  • Problem with java beans and jsp on web logic 6.0 sp1

              HI ,
              I am using weblogic6.0 sp1.
              i have problem with jsp and java beans.
              i am using very simple java bean which stores name and email
              from a html form.
              but i am getting following errors:
              Full compiler error(s):
              D:\bea4\wlserver6.0sp1\config\mydomain\applications\DefaultWebApp_myserver\WEB-INF\_tmp_war_myserver_myserver_DefaultWebApp_myserver\jsp_servlet\_savename2.java:89:
              cannot resolve symbol
              symbol : class userbn
              location: class jsp_servlet._savename2
              userbn ud = (userbn) //[ /SaveName2.jsp; Line: 7]
              ^
              D:\bea4\wlserver6.0sp1\config\mydomain\applications\DefaultWebApp_myserver\WEB-INF\_tmp_war_myserver_myserver_DefaultWebApp_myserver\jsp_servlet\_savename2.java:89:
              cannot resolve symbol
              symbol : class userbn
              location: class jsp_servlet._savename2
              userbn ud = (userbn) //[ /SaveName2.jsp; Line: 7]
              ^
              D:\bea4\wlserver6.0sp1\config\mydomain\applications\DefaultWebApp_myserver\WEB-INF\_tmp_war_myserver_myserver_DefaultWebApp_myserver\jsp_servlet\_savename2.java:94:
              cannot resolve symbol
              symbol : class userbn
              location: class jsp_servlet._savename2
              ud = (userbn) java.beans.Beans.instantiate(getClass().getClassLoader(),
              "userbn"); //[ /SaveName2.jsp; Line: 7]
              ^
              3 errors
              in which directory should i place java bean source file(.java file)
              here is my jsp file:
              <%@ page language = "java" contentType = "text/html" %>
              <html>
              <head>
              <title>bean2</title>
              </head>
              <body>
              <jsp:usebean id = "ud" class = "userbn" >
              <jsp:setProperty name = "ud" property = "*" />
              </jsp:usebean>
              <ul>
              <li> name: <jsp:getProperty name = "ud" property = "name" />
              <li> email : <jsp:getProperty name = "ud" property = "email" />
              </ul>
              </body>
              <html>
              here is my bean :
              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
              import java.io.*;
              public class userbn implements Serializable
                   private String name ;
                   private String email;
                   public void setName(String n)
                        name = n;
                   public void setEmail(String e)
                        email = e;
                   public String getName()
                        return name;
                   public String getEmail()
                        return email;
                   public userbn(){}
              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
              pls help me.
              Thanks
              sravana.
              

              You realy can do it like Xiang says, but the better way is to use packages. That's
              the way BEA is designed for. If you use packages you can but your bean classes
              in every subfolder beneath Classes. Here for example we have the subfolders test
              and beans:
              You have to declare the package on top of your Bean Source Code:
              package test.beans;
              In your JSP you don't need the import code of Xiang. You only have to refer the
              path of your bean class:
              <jsp:useBean id="testBean" scope="session" class="test.beans.TestBean" />
              There are some other AppServers that only can deploy Java Beans in packages. So
              if you use packages you are always on the right side.
              ciao bernd
              "sravana" <[email protected]> wrote:
              >
              >Thank you very much Xiang Rao, It worked fine.
              >Thanks again
              >sravana.
              >
              >"Xiang Rao" <[email protected]> wrote:
              >>
              >><%@ page import="userbn" language = "java" contentType = "text/html"
              >>%> should
              >>work for you.
              >>
              >>
              >>"sravana" <[email protected]> wrote:
              >>>
              >>>HI ,
              >>>
              >>>I am using weblogic6.0 sp1.
              >>>
              >>>i have problem with jsp and java beans.
              >>>
              >>>i am using very simple java bean which stores name and email
              >>>
              >>>from a html form.
              >>>
              >>>but i am getting following errors:
              >>>
              >>>________________________________________________________________
              >>>
              >>>Full compiler error(s):
              >>>D:\bea4\wlserver6.0sp1\config\mydomain\applications\DefaultWebApp_myserver\WEB-INF\_tmp_war_myserver_myserver_DefaultWebApp_myserver\jsp_servlet\_savename2.java:89:
              >>>cannot resolve symbol
              >>>symbol : class userbn
              >>>location: class jsp_servlet._savename2
              >>> userbn ud = (userbn) //[ /SaveName2.jsp; Line: 7]
              >>> ^
              >>>D:\bea4\wlserver6.0sp1\config\mydomain\applications\DefaultWebApp_myserver\WEB-INF\_tmp_war_myserver_myserver_DefaultWebApp_myserver\jsp_servlet\_savename2.java:89:
              >>>cannot resolve symbol
              >>>symbol : class userbn
              >>>location: class jsp_servlet._savename2
              >>> userbn ud = (userbn) //[ /SaveName2.jsp; Line: 7]
              >>> ^
              >>>D:\bea4\wlserver6.0sp1\config\mydomain\applications\DefaultWebApp_myserver\WEB-INF\_tmp_war_myserver_myserver_DefaultWebApp_myserver\jsp_servlet\_savename2.java:94:
              >>>cannot resolve symbol
              >>>symbol : class userbn
              >>>location: class jsp_servlet._savename2
              >>> ud = (userbn) java.beans.Beans.instantiate(getClass().getClassLoader(),
              >>>"userbn"); //[ /SaveName2.jsp; Line: 7]
              >>> ^
              >>>3 errors
              >>>
              >>>____________________________________________________________
              >>>
              >>>in which directory should i place java bean source file(.java file)
              >>>
              >>>here is my jsp file:
              >>>--------------------------------------------------------
              >>>
              >>><%@ page language = "java" contentType = "text/html" %>
              >>><html>
              >>><head>
              >>><title>bean2</title>
              >>></head>
              >>><body>
              >>><jsp:usebean id = "ud" class = "userbn" >
              >>><jsp:setProperty name = "ud" property = "*" />
              >>></jsp:usebean>
              >>><ul>
              >>><li> name: <jsp:getProperty name = "ud" property = "name" />
              >>><li> email : <jsp:getProperty name = "ud" property = "email" />
              >>></ul>
              >>></body>
              >>><html>
              >>>
              >>>-------------------------------------------------------------
              >>>
              >>>here is my bean :
              >>>
              >>>~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
              >>>
              >>>import java.io.*;
              >>>
              >>>public class userbn implements Serializable
              >>>{
              >>>
              >>>     private String name ;
              >>>
              >>>     private String email;
              >>>
              >>>     public void setName(String n)
              >>>     {
              >>>
              >>>          name = n;
              >>>     }
              >>>
              >>>     public void setEmail(String e)
              >>>     {
              >>>
              >>>          email = e;
              >>>     }
              >>>
              >>>     public String getName()
              >>>     {
              >>>
              >>>          return name;
              >>>     }
              >>>
              >>>     public String getEmail()
              >>>     {
              >>>
              >>>          return email;
              >>>     }
              >>>
              >>>     public userbn(){}
              >>>}
              >>>~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
              >>>
              >>>pls help me.
              >>>Thanks
              >>>sravana.
              >>>
              >>
              >
              

  • Java Classes and JSP`s (urgent)

    hi, my question is:
    How can i invoke normal java classes` methods using Jsp`s for this??, is it possible??, if it is, then how do you do this, and where do you have to put the java classes in order to find them from a jsp.
    thanks

    Hi,
    You can access normal java classes in JSP pages. You have to import them in the header like this:
    <%@ page language="java" import="//your java class file with package"%>
    or if u r using java beans, then use the <usebean> tag. Put the java classes in the classes directory. Which server u r using? If it is JWS, then u have a specified classes directory, where u have to put all the class files along with their packages. Hope this will help.
    atanu.

  • Questions about Java Servlets and JSP

    Hi,
    I'm a confident Java Programmer (and really enjoy using this language) but am very new to Java servlets and Java Server Pages.
    I have previously worked with Perl on my web projects (simple 'league' style voting pages). I read in my 'Core Java' book that I should no longer use perl or even cgi.
    I need to know more about Java servlets and Java Server Pages so I can make the switch to a 'real' programming language.
    I have a few questions:
    How should I start to learn JS and JSP?
    How applicable will the java knowlegdge I have already be?
    Are JSP common on the world wide web?
    What tools do I need to start? (I currently develop in JBuilder and have Java 1.4.1 Standard Edition)
    Is it likey my web host (and others) will support JSP?
    Thank-you very much for helping a novice get started,
    Regards,
    Paul

    Hi, Steve ...has to be frustrating! But do not despair.
    Let's suppose the servlet it's named MyServlet on package org.servlets
    WEB-INF should look:
    WEB-INF
    classes
    org
    servlets
    MyServlet.class
    web.xml
    web.xml file should have this two declarations:
    <web-app>
      <servlet>
        <servlet-name>MyServlet</servlet-name>
        <servlet-class>org.servlets.MyServlet</servlet-class>
      </servlet>
      <!-- other servlets -->
      <servlet-mapping>
        <servlet-name>MyServlet</servlet-name>
        <url-pattern>/MyServlet</url-pattern>
      </servlet-mapping>
      <!-- other servlets mappings -->
    </web-app>Now, once the container starts (Tomcat?), you should be able to see that servlet in:
    http://localhost:8080/[my-context/]MyServletAnd what my-context is? The web application context. This string should be empty if your're deploying to the root context, otherwise should the context name. In Tomcat, deploying to root context defaults to using webapps/ROOT.
    Sorry for my English, but I felt the need to answer your request. I hope it helps despite my writing.

  • Why the need of "import" statements for Java base code?

    If for documentation purposes, a compiler option to produce an "import list" would have been sufficient.

    import statements act as really simple namespaces.
    Instead of the programer having to specify:
    java.io.File f = new java.io.File("c:\\");
    the code can have a 'namespace' added to do the following:
    import java.io.file;
    File f = new File("c:\\);
    it's a way of getting around similiarly named classes, but still have the ability to drop the 'java.io.' part.

  • Executing db2 import statement from java

    we have developed an web application using J2EE technology on websphere and DB2 as a backend.
    I have to now develop a utility for data uploading from Excel to DB2. we can do it using IMPORT statement now i want to know is it possible to execute IMPORT statement from JSP/ Servelet and using javabeans.

    Yes, it is possible :-)

  • Import Statement in Java

    I have one question about import of packages and classes. If I import java.awt.** why can't all the packages starting with java.awt get imported.For example java.awt.color.* or java.awt.font doesn't get imported. I didn't get why so and if so then why these classes were prefixed with java.awt. Same case happened to me when I imported java.util.* and didn't find java.util.concurrent.locks.Lock get imported. There must be some reason behind this. But I don't know. Can anyone please let me know ?
    --Prasad
    Edited by: Prasad_Kumbhare on Oct 1, 2007 7:55 PM

    flounder wrote:For me:
    Boredom, it gives me something to do.
    Reward at imparting my knowledge onto others. When I was at uni I got some part time work as a tutor helping 1st and 2nd year students with their prac sessions and tutorials. I enjoyed being able to mould young minds. Plus, with all the subliminal messages I planted, one day I will rule the world. OK that last bit wasn't true. I want to rule the universe!That's very noble Mr Flounder, so thanks again.
    I imagine if you want to rule the universe it makes sense to achieve this goal via a platform independant language. But does Java run on differant planets?
    S

  • Java beans and JSP - Training

    Hi
    Does Sun or some other organization conduct any good workshops on Javabeans and jsp technology - hands on training like Microsoft products usually have. - Something like "devcon" ??
    Some people from our workplace have to have some urgent training on these technologies. Thanks in advance.
    Sue

    They sure do... http://courses.coreservlets.com/
    Don't really know how much or how often they offer these courses though.. looks like the guy might be free to do something soon. But to be honest, these courses basically run through the book. If your programmers have a decent background in Java, I'd say you could just as easily get away with getting some books (Profession JSP - Wrox press is amazing). I learned JSP in about 3 days of solid cramming.

Maybe you are looking for

  • Error starting WebLogic rmic

    Hi , I unable to generate the stub/skeleton in my java classes using rmic. With help of below code in build.xml file <taskdef name="rmic" classname="org.apache.tools.ant.taskdefs.Rmic"/>    <rmic  base="${build.dest}" includes="**/*.class"  classpath

  • Installer App no longer works

    My installer app was broken by the Safari 3 beta. All cocoa apps were broken too but thankfully I got that fixed by restoring some frameworks that got deleted when the installer froze while installing the Safari beta. However, Installer still doesn't

  • Issues with importing from excel

    I have been running into a several issues with importing from Excel. First my configuration I am running SQL Developer ver 1.5.5 Build MAIN-5969 I am on a Windows XP Professional Version 2002 with Service Pack 3 I am importing into an Oracle 10g data

  • IPod keeps restarting :(....HELP pliz

    itunes wont see it...now it keeps restarting so i cant even apply factory settings....what do i do ????

  • How to Copy Standard Business Areas in Oracle apps

    Does anyone knows how we can copy new Standard BUsiness Areas in Oracle Apps for fresh implementations. Thanks