aspose file tools
The moose likes Beginning Java and the fly likes getting data from java classes Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "getting data from java classes" Watch "getting data from java classes" New topic
Author

getting data from java classes

Muralidhar Dasadhikari
Greenhorn

Joined: Apr 12, 2005
Posts: 1
I have successfully set up Tomcat and I am trying to understand how to import the functionality available in a Java class within a JSP.

Using beans I have been able to set and get data. But is there a simple way to connect to classes that contain more sophisticated functionality. For instance, say if I wanted to know how many files existed in a directory on my C drive. If I were to write a class that lists the files in a directory, then from a JSP I pass a message to that class and the Java code lists the directories and sends back a message, "3 files".

Obviously this is easy to do in a JSP itself:
<%
String filename = "c:/Tomcat/webapps/";
File dir = new File(filename);
String[] children = dir.list();
int size = children.length;
%>

But can anyone suggest a simple way to pass in the filename and return the size using JAVA? I know how to do it with beans, but how do I do it with Java classes?
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 3039
    
    4

I am a little confused by this line:
I know how to do it with beans, but how do I do it with Java classes?


JavaBeans are Java Classes.
But here is a way to use a static method in a class to do it...


This is just untested code, but it should come close. It will also only work for directories on the server.

Originally posted by Muralidhar Dasadhikari:
I have successfully set up Tomcat and I am trying to understand how to import the functionality available in a Java class within a JSP.

Using beans I have been able to set and get data. But is there a simple way to connect to classes that contain more sophisticated functionality. For instance, say if I wanted to know how many files existed in a directory on my C drive. If I were to write a class that lists the files in a directory, then from a JSP I pass a message to that class and the Java code lists the directories and sends back a message, "3 files".

Obviously this is easy to do in a JSP itself:
<%
String filename = "c:/Tomcat/webapps/";
File dir = new File(filename);
String[] children = dir.list();
int size = children.length;
%>

But can anyone suggest a simple way to pass in the filename and return the size using JAVA? I know how to do it with beans, but how do I do it with Java classes?


Steve
Ryan McGuire
Ranch Hand

Joined: Feb 18, 2005
Posts: 945
A JSP page gets translated directly into a regular Java file on its way to being comiled into a class file. (The JSP class is a subclass of Servlet, if I recall correctly.) So anything you can do in a "regular" Java file can be done in a JSP.

As for getting data into a JSP, you can do that by using the GET or POST type of HTTP request to your webserver. Then, in your JSP code, use request.getParameter(argname) to get the passed in value.

For instance if you call http://server/blah.jsp?myname=Muralidhar and have the following code in the JSP:


...you should see the input parameter.

Yes, I know that the above code could be consolidated to...

...but I wanted to show getting the URL parameter and generating some output as separate steps.

So you can stick all of Mr. Luke's FileOps code right into your JSP. There is a way to add addtional methods to your JSP/Servlet.

If you wanted to make your JSP look as nice as possible (at the cost of extra Java and XML overhead) you could define a custom tag library. That way your JSP code would just have something like


This would more impressive for your directory example than it is for this getname example.

Ryan
[ April 12, 2005: Message edited by: Ryan McGuire ]
Chengwei Lee
Ranch Hand

Joined: Apr 02, 2004
Posts: 884

Using beans I have been able to set and get data. But is there a simple way to connect to classes that contain more sophisticated functionality. For instance, say if I wanted to know how many files existed in a directory on my C drive. If I were to write a class that lists the files in a directory, then from a JSP I pass a message to that class and the Java code lists the directories and sends back a message, "3 files".


What you need is a feature called JSP custom tags. You could try searching Apache to see if there's one that fits your needs. Are you trying write something that allows users to upload files from their local machine to server? If I'm not mistaken, there're some codes/tags around that does that. Perhaps a Google search would return you the answers.

By using JSP tags, you can eliminate Java codes/scriptlets in your JSPs. This improves readability & maintainability largely. We cannot assume the web designer to be proficient in Java programming as well. Also, you can reuse the tags in any other JSP. So whenever you need to make any changes, you just do it once in the tag files rather than all over the JSPs.

HTH.


SCJP 1.4 * SCWCD 1.4 * SCBCD 1.3 * SCJA 1.0 * TOGAF 8
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: getting data from java classes
 
Similar Threads
Trying to find javax, to compile Tomcat servlet
JSP + bean on Tomcat
Tomcat settings
JSP+bean
Problems with import/package statements