JSP problem

U

unlikeablePorpoise

I am new to JSP (and Java) and I would like to implement a simple page
that invokes a Java class (not using JavaBeans). Below is the code for
the class and JSP page:

//-----------CLASS FILE----------
package print_string_pack;

public class PrintString
{
public static void main(String[] args)
{

}

public void outputIt()
{
System.out.println("!!!!!!!!");
}
}

//------------JSP PAGE-------------
<HTML>
<BODY>

<%@ page import = "print_string_pack.PrintString" %>
<% PrintString ps = new PrintString(); %>
<% String result = ps.outputIt(); %>

</BODY>
</HTML>
--------------END---------------

//---------------TOMCAT ERROR--------
HTTP Status 500 -

type Exception report

message

description The server encountered an internal error () that prevented
it from fulfilling this request.

exception

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

org.apache.jasper.servlet.JspServletWrapper.getServlet(JspServletWrapper.java:
154)

org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:
320)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:
320)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)


root cause

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

org.apache.jasper.JspCompilationContext.load(JspCompilationContext.java:
600)

org.apache.jasper.servlet.JspServletWrapper.getServlet(JspServletWrapper.java:
142)

org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:
320)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:
320)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)


root cause

java.lang.ClassNotFoundException: org.apache.jsp.test_jsp
java.net.URLClassLoader$1.run(Unknown Source)
java.security.AccessController.doPrivileged(Native Method)
java.net.URLClassLoader.findClass(Unknown Source)
org.apache.jasper.servlet.JasperLoader.loadClass(JasperLoader.java:
134)
org.apache.jasper.servlet.JasperLoader.loadClass(JasperLoader.java:
66)

org.apache.jasper.JspCompilationContext.load(JspCompilationContext.java:
598)

org.apache.jasper.servlet.JspServletWrapper.getServlet(JspServletWrapper.java:
142)

org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:
320)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:
320)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)


--------------------------------------------------------------------------------

The 'page import' works and I have my class file in WEB-INF\classes.
The class also compiles okay. Can anybody spot the problem?


Thanks,
--Sarah
 
L

Lew

I am new to JSP (and Java) and I would like to implement a simple page
that invokes a Java class (not using JavaBeans). Below is the code for
the class and JSP page:

//-----------CLASS FILE----------
package print_string_pack;

public class PrintString
{
// no reason at all to have a main() in this class.
public void outputIt()
{
System.out.println("!!!!!!!!");
}
}

//------------JSP PAGE-------------
<HTML>
<BODY>

<%@ page import = "print_string_pack.PrintString" %>
<% PrintString ps = new PrintString(); %>
<% String result = ps.outputIt(); %>

You cannot assign the return value of a method that has a void return value.

I recommend that you eschew scriptlet in your JSPs and underscores in your
package or variable names.

If you must have scriptlet, it is not necessary to enclose each individual
line in <% ... %>. You can use those delimiters for a block of code.

It makes very little sense to have "System.out.println()" in Web code.

I recommend that you use lower-case tags in your HTML. Get ready for XHTML.

-- Lew
 
C

Chris Smith

I am new to JSP (and Java) and I would like to implement a simple page
that invokes a Java class (not using JavaBeans). Below is the code for
the class and JSP page:

Lew pointed out your problem.

In addition, you seem to be looking in the wrong place for error
messages. Try looking in your log files. Somewhere, you should see the
compile error for the JSP. Everything you've included here is indirect
consequences that happened way later along the road.

Chances are that if you find the original error message, it will flat
out tell you that the problem is that outputIt is void, and so you can't
assign the result.
 
N

nupul

I am new to JSP (and Java) and I would like to implement a simple page
that invokes a Java class (not using JavaBeans). Below is the code for
the class and JSP page:

//-----------CLASS FILE----------
package print_string_pack;

public class PrintString
{
public static void main(String[] args)
{

}

public void outputIt()
{
System.out.println("!!!!!!!!");
}

}

//------------JSP PAGE-------------
<HTML>
<BODY>

<%@ page import = "print_string_pack.PrintString" %>
<% PrintString ps = new PrintString(); %>
<% String result = ps.outputIt(); %>

</BODY>
</HTML>
--------------END---------------


As pointed out, a void function can't be assigned!
Secondly, if you write System.out.println("..."); the code will appear
at the server not at the client! Just see the server console after
executing the correct code.

To see the output on the webpage do this

<% PrintWriter out;

out.print("hello");

%>

it makes no sense to say out.println("..."); because the browser
doesn't understand the newline character. enclose <BR> tag within
"..." if you want a newline.


I suggest you do some reading on JSPs. You'll gain quite a clear
understanding of the same!

FYI: use JSPs for presentation purposes only. Avoid putting any code
in them as it will tightly couple your web application

Cheers

Nupul
 
U

unlikeablePorpoise

Thanks for your replies. I changed the obvious error (returning from a
void...duh) but it doesn't solve my problem. The log file contains the
following error:

"...The method outputIt() is undefined for the type PrintString..."

The class is defined and it compiles/runs without errors. At this
point all I want to do is get a JSP page to invoke a class method,
even if the method does absolutely nothing. The JSP page now looks
like this:
 
D

dweesie

Thanks for your replies. I changed the obvious error (returning from a
void...duh) but it doesn't solve my problem. The log file contains the
following error:

"...The method outputIt() is undefined for the type PrintString..."

The class is defined and it compiles/runs without errors. At this
point all I want to do is get a JSP page to invoke a class method,
even if the method does absolutely nothing. The JSP page now looks
like this:

----------
<%@ page import = "print_string_pack.PrintString" %>
<% PrintString ps = new PrintString(); %>
<% ps.outputIt(); %>
-----------

Thanks,
--Sarah




- Show quoted text -

If you change your method signature to public String outputIt(), and
have it return your text, you can use the following in your jsp:

<%= ps.outputIt() %>

note the = and the missing semicolon

Greg
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top