You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Winnie Cheung <wi...@iname.com> on 2000/11/21 11:52:49 UTC

Help.. I cannot use classes in JSP code itself!!

Hello..

Can I write my own simple classes within the JSP code? I mean within the "<%
%>" tags? I have been trying to do this but its giving me an error all the
time!

I want to do this because I would like to avoid the rigmarole of going
through all these directory structures and all that with servlets!

Thanks very much for the patience, I am a very new user.

Best regards
Winnie


Re: Help.. I cannot use classes in JSP code itself!!

Posted by Dave Smith <sa...@home.com>.
Winnie,

Naturally. The following JSP creates an inner class and then counts as the
page is reloaded. Keep in mind
that JSP's are just servlets and servlets are just java classes. There are
no special restrictions. Please
consult a good book on java for more details on constructing Inner classes.

Also, please stop posting general java questions to the list.

Thanks,

Dave

******************************START CODE********************
<%!
 public class TestInner {
  private int count = 0;
  public void increment() { count++; }
  public String toString() { return ""+count; }
 }
 TestInner inner = new TestInner();
 %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
   "http://www.w3.org/TR/html4/strict.dtd">

<HTML>
   <HEAD>
      <TITLE>Test</TITLE>
   </HEAD>
<BODY>

<H1>Count: <% inner.increment(); %><%=inner%></H1>

</BODY>
</HTML>

**************************END CODE*****************



----- Original Message -----
From: "Winnie Cheung" <wi...@iname.com>
To: <to...@jakarta.apache.org>
Sent: Tuesday, November 21, 2000 4:52 AM
Subject: Help.. I cannot use classes in JSP code itself!!


> Hello..
>
> Can I write my own simple classes within the JSP code? I mean within the
"<%
> %>" tags? I have been trying to do this but its giving me an error all the
> time!
>
> I want to do this because I would like to avoid the rigmarole of going
> through all these directory structures and all that with servlets!
>
> Thanks very much for the patience, I am a very new user.
>
> Best regards
> Winnie
>