0

from the search I made I understood that I must declare the class in a package then put it in WEB-INF/classes. and call it from the jsp page.
SO. my class is:

package x.com;
import java.io.*;

public class X
{    
	public static void main(String[] args ) throws IOException
	{
	File myFile = new File("/usr/local/tomcat/webapps/CMMAC/web/temp");
	myFile.deleteOnExit();
	}
}

then I created classes ----> x ----> com ----> and then I put X.class in it.

then I'm calling the class from the JSP page as follows:

<%@ page import="x.com.*" %>

if(line.startsWith("0005"))
X();

the error returned is as follows:

org.apache.jasper.JasperException: Unable to compile class for JSP:

An error occurred at line: 26 in the jsp file: /web/CMMAC.jsp
The method X() is undefined for the type CMMAC_jsp
23: line=line+normal;
24: }
25: if(line.startsWith("0005"))
26: X();
27: else
28: {
29: line=line.toLowerCase();

okay. so I suspect the problem is in the way I call it. I think I'm calling it the wrong way. so given all the information that I attached, where is the problem?

is it the package place? wrong code? wrong way of call? bad import?

and thx

Edited by ElieM: n/a

3
Contributors
10
Replies
12
Views
7 Years
Discussion Span
Last Post by javaAddict
Featured Replies
  • It is true that you can put java code inside jsp and it would work. It is not a problem if you have the minimum code (very few lines). But you cannot put entire logic inside the jsp. All the logic needs to be done outside the jsp and inside … Read More

0

1) you should never use Java code inside a JSP. It's possible, it's there for legacy reasons, but that doesn't mean it's the way to go. Use jstl or jsf instead and handle all business logic in servlets or backing beans.
2) There is no "method X", there is a class X (if you put it in the right place, and it is correct).
Your class X has no public methods except a main method, which is utterly useless in this context.

Learn Java before trying JSP, and learn JSP properly without using Java code in it.

0

I dont want to use servlets I want to work java inside jsp with html. that's the way I chose that's the way my web app program is working completly and flawlessly. the X class is just a bonus class to just prevent my prog from being stolen. just a luxury. I know that classes need to be inside main but there's no main here that's why I'm trying to call it from there. okay I will call methods I got it. but I disregard the first advice and thxx

0

I know that classes need to be inside main but there's no main here that's why I'm trying to call it from there.

That statement alone proves that you must stop right now working with JSPs. How much experience do you have with core java? Do you have any books about JSPs or are you just going blind?

0

nah actually I tried to learn it from online tutorials just as a tryout, and actually I made a nice program for the company using the way I chose.. a 250 line web application that works with sockets, clients and server and such u know.. and yes I know, jsps aren't meant to use sockets and such but it's there and it's working absolutely wonderful and nice, and that's why I will continue using my way..

so now is a point when I just had to use an external method so I'm learning how to integrate that into my page.

I hope u got my point

0

No, we're not going to tell you how to do things we know are wrong.
If anyone turned in code like you're trying to make for a review he'd get told to start again from scratch.
If he did it twice, he'd be fired.

0

no, as long as it is there, working as intended, I'm not gonna change my mind ever, until proven wrong.

0

in that case, do it without help from people who actually know what they're talking about and have been using JSP for over a decade.

2

It is true that you can put java code inside jsp and it would work. It is not a problem if you have the minimum code (very few lines).

But you cannot put entire logic inside the jsp. All the logic needs to be done outside the jsp and inside the jsp you will simply print the results.

This is the comment I read in a book about java code inside jsp:
"Just because having java code inside jsp works, doesn't mean it is correct. With your logic someone might go and write the entire web application in assembly and it would work. That doesn't make it correct nor efficient."

0

It is true that you can put java code inside jsp and it would work. It is not a problem if you have the minimum code (very few lines).

But you cannot put entire logic inside the jsp. All the logic needs to be done outside the jsp and inside the jsp you will simply print the results.

This is the comment I read in a book about java code inside jsp:
"Just because having java code inside jsp works, doesn't mean it is correct. With your logic someone might go and write the entire web application in assembly and it would work. That doesn't make it correct nor efficient."

yeah man, most of replies refusing the idea made me do searches to find out why, and what u said is correct, but I just refused the idea of some people just coming out and saying "u don't know what u do that's wrong end of discussion". Yes I do need help cz I lack some stuff, If I didn't help I wouldn't have come and post here, but I refuse the 1 idea end of discussion way, bcz I see my code working very fine. I need to know "WHY" it isn't that good. for that I did do my search and found most of the disadvantages not disadvantages for me, except 1 point. that point will make me think about separating the two.

thx for this reply, I respect it and I got ur idea :)

0

The reason we assumed that you didn't know what you were doing is because you tried to find a way to call a main method:

public static void main(String [] args)

From a jsp. After seeing that most people assumed that have never written a jsp in your life!

But apparently you understood on your own, that writing lots of java code inside jsp is not a good idea, otherwise you wouldn't have tried to call separate methods (the main method) that had the logic inside it.

But in your second post you insisted in doing things your way instead of asking why you shouldn't do that. That is why most people didn't bother to explain since you didn't gave the impression that you were ready to listen:

I dont want to use servlets I want to work java inside jsp with html. that's the way I chose that's the way my web app program is working completly and flawlessly. okay I will call methods I got it. but I disregard the first advice and thxx

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.