Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2007

Tip: Looking for answers? Try searching our database.

JSP problem

Thread view: 
unlikeablePorpoise@gmail.com - 13 Mar 2007 00:20 GMT
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
Lew - 13 Mar 2007 00:34 GMT
> 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
[quoted text clipped - 5 lines]
> public class PrintString
> {
// no reason at all to have a main() in this class.

>     public void outputIt()
>     {
[quoted text clipped - 9 lines]
> <% 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
Chris Smith - 13 Mar 2007 04:22 GMT
> 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.

Signature

Chris Smith

unlikeablePorpoise@gmail.com - 13 Mar 2007 21:58 GMT
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

> 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
[quoted text clipped - 4 lines]
> out tell you that the problem is that outputIt is void, and so you can't
> assign the result.
dweesie - 13 Mar 2007 23:27 GMT
On Mar 13, 2:58 pm, unlikeablePorpo...@gmail.com wrote:
> 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
[quoted text clipped - 26 lines]
>
> - 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
nupul - 13 Mar 2007 08:02 GMT
On Mar 13, 4:20 am, unlikeablePorpo...@gmail.com wrote:
> 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
[quoted text clipped - 28 lines]
> </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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.