• Post Reply Bookmark Topic Watch Topic
  • New Topic

getting data from java classes  RSS feed

 
Muralidhar Dasadhikari
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Ryan McGuire
Ranch Hand
Posts: 1143
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 884
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!