#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    65
    Rep Power
    2

    How to include java file in jsp file?


    i want to use JavaFunctions.java into login.jsp file. my plan is to store all my java functions in one file and i can use them later. ex check for string or number etc functions...

    first my folder tree.

    Code:
    webside_01
        >.settings
        >build
        >src
             >newServlet
                  JavaFunctions.java
                  website_01_Servlet.java
        >WebContent
            >META-INF
            >WEB-INF
            index.jsp
            login.jsp
    JavaFunctions.java
    Code:
    public class JavaFunctions {
        /*** test to see if its a number ***/
        public static boolean isNumber(String str){
            try{
                int num = Integer.parseInt(str);
            }
            catch(NumberFormatException e){
                return false;
            }
            return true;
        }/*** End of isnumber Method***/
    }
    Know in this login.jsp file i want to use 'isNumber()' method. but iam getting error on:
    <%@ include file="JavaFunctions.java" %>

    login.jsp
    Code:
    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%>
     <%@ include file="JavaFunctions.java" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head></head>
    <body>
    
        .....
    </body>
    ....
  2. #2
  3. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,021
    Rep Power
    1285
    What I would do is create a package containing JavaFunctions.java and import that package.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    65
    Rep Power
    2
    newServlet is a package.

    JavaFunctions.java

    Code:
    package newServlet;
    
    public class JavaFunctions {
    ...
    }
    so iam thinking to import package in login.jsp like this:

    Code:
    <%@ page import = "newServlet.JavaFunctions" %>
    ...
    
    //i try calling isnumber method but getting a error.
    boolean c = isNumber("hi");
    Code:
    error on boolean c = isNumber("hi"):
    Multiple annotations found at this line:
    	- The method isNumber(String) is undefined for 
    	 the type 
    	 __2F_website_5F_01_2F_WebContent_2F_login_2E_jsp
    	- The method isNumber(String) is undefined for 
    	 the type 
    	 __2F_website_5F_01_2F_WebContent_2F_login_2E_jsp
    Last edited by game2d; May 18th, 2013 at 09:28 PM.
  6. #4
  7. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,021
    Rep Power
    1285
    Note isNumber is static.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    65
    Rep Power
    2
    Originally Posted by bullet
    Note isNumber is static.

    ah ok so ifmethod is static than you can do this:
    Code:
    isNumber()
    but when its not static than you have to do this:
    Code:
    JavaFunctions.isNumber()
    thanks for you help
  10. #6
  11. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,021
    Rep Power
    1285
    Originally Posted by game2d
    ah ok so ifmethod is static than you can do this:
    Code:
    isNumber()
    but when its not static than you have to do this:
    Code:
    JavaFunctions.isNumber()
    thanks for you help
    If it's not a static method, then you have to call it with an instance.

IMN logo majestic logo threadwatch logo seochat tools logo