Garann,
Have you opened the compiled jsp page to see what the servlet looks like?
If you did, I think you would understand a little better what is happening.
First off, a jsp page is just a "short hand" way of writing a servlet. When
your jsp/servlet server (say JRUN, Weblogic, etc) "touches" your jsp page,
it is compiled into a servlet. The servlet is what is actually "run".
Having said that, the <%! %> tags are the short hand way of declaring
methods of your servlet class. The main method of any servlet class is as
follows:
public void _jspService(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, java.io.IOException
ANY method declared in the <%! %> tags is NOT included as part of the
"main" servlet method above. Therefore, those methods cannot "see" the
request or the response objects unless you pass them into your own declared
methods, and pass the data back out to the "main" servlet class above.
Again, go look at your compiled JSP pages (servlets) to see the differences.
When you do NOT include your declarations in the <%! %> tags, the complier
throws an error, because you cannot declare another class method inside the
"main" servlet class. This is a fundamental concept of Object Oriented
Languages, so if you do not have a background in C++ or Java, just in VB,
this will be a little confusing to you.
Bottom line, either rewrite your methods to include "passing" the response
and request objects into the method, and back into the "main" servlet method
if you need your method to see the data, or incorporate the logic directly
into your jsp (ie, remove the logic out of the method).
Hope this helps
Celeste
-----Original Message-----
From: Means, Garann R. [mailto:[EMAIL PROTECTED]]
Sent: Wednesday, January 23, 2002 12:46 PM
To: [EMAIL PROTECTED]
Subject: Re: Where to put methods for a jsp page?
Monte,
I'm only putting them in <%! %> tags because I get errors like this when I
don't:
An error occurred between lines: 51 and 53 in the jsp file:
/LabGenEdit.jsp
Generated servlet error:
C:\Tomcat\jakarta-tomcat-4.0\work\localhost\HepC\LabGenEdit$jsp.java:121:
Type expected.
out.write("\r\n\t\r\n");
^
Is there another way to approach it? (I'm just sort of hacking my way
through this..)
Also, what is the syntax for passing a request? Just:
someVar = brokenMethod(request);
...
public int brokenMethod(HttpServletRequest request) { ... } ?
Thanks for responding so quickly!
Garann
-----Original Message-----
From: Gardner Monte [mailto:[EMAIL PROTECTED]]
Sent: Wednesday, January 23, 2002 10:35 AM
To: [EMAIL PROTECTED]
Subject: Re: Where to put methods for a jsp page?
I may have misunderstood what you're asking, but let me give it a
try. If you are using the <%! %> syntax for declaring JSP methods, then
you have to understand that those methods are seperat from the
main method of your JSP page, which is actually the service() method
of a servlet. The request object is a local variable of the
service method, and therefore does not exist in your other methods.
If you want other methods to be able to access the request object, you
have to pass the request object into the methods via parameters.
--hope that helps.
--Monte Glenn Gardner
On Wed, 23 Jan 2002, Means, Garann R. wrote:
> Hi,
>
> I'm converting a bunch of asp pages to jsp. The asp pages had methods
> running on the server side, so I've left them there in the jsp. When I try
> to compile the jsp, I get errors telling me the compiler doesn't know
> "request.getParameter", but only within the methods. I'll post the code,
and
> maybe someone can give me feedback?
>
> <!-- top of page (there are some declarations up here, superfluous for
these
> methods) -->
> <%! public String mkDate() {
> String sHourHTML = "";
> String sMinHTML = "";
> String sHTML = "";
>
> sHTML = "<select name='selCollHour'>";
>
> // ...
> // ...
> // ... this all compiles fine, and doesn't use
> request.getParameter
>
> return sHTML;
> } %>
>
> <%! public String redChem(String root) {
> float usrIn = 0;
> float lBnd = 0;
> float uBnd = 0;
> String sRed = "";
>
> // these next three lines are throwing the errors
> usrIn = Float.parseFloat(request.getParameter("txtArea" +
> root));
> lBnd = Float.parseFloat(request.getParameter("hdnLowBnd" +
> root));
> uBnd = Float.parseFloat(request.getParameter("hdnUpBnd" +
> root));
>
> if (usrIn != 0) {
> if ((usrIn > uBnd) || (usrIn < lBnd)) {
> sRed = "style='color:red'";
> }
> else {
> sRed = "";
> }
> }
> return sRed;
> } %>
>
>
> Thanks,
> Garann Rose Means
> ITAS1
> WA State Dept. of Corrections
>
>
===========================================================================
> To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff
JSP-INTEREST".
> For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST
DIGEST".
> Some relevant FAQs on JSP/Servlets can be found at:
>
> http://archives.java.sun.com/jsp-interest.html
> http://java.sun.com/products/jsp/faq.html
> http://www.esperanto.org.nz/jsp/jspfaq.jsp
> http://www.jguru.com/faq/index.jsp
> http://www.jspinsider.com
>
===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff
JSP-INTEREST".
For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST
DIGEST".
Some relevant FAQs on JSP/Servlets can be found at:
http://archives.java.sun.com/jsp-interest.html
http://java.sun.com/products/jsp/faq.html
http://www.esperanto.org.nz/jsp/jspfaq.jsp
http://www.jguru.com/faq/index.jsp
http://www.jspinsider.com
===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff
JSP-INTEREST".
For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST
DIGEST".
Some relevant FAQs on JSP/Servlets can be found at:
http://archives.java.sun.com/jsp-interest.html
http://java.sun.com/products/jsp/faq.html
http://www.esperanto.org.nz/jsp/jspfaq.jsp
http://www.jguru.com/faq/index.jsp
http://www.jspinsider.com
===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST DIGEST".
Some relevant FAQs on JSP/Servlets can be found at:
http://archives.java.sun.com/jsp-interest.html
http://java.sun.com/products/jsp/faq.html
http://www.esperanto.org.nz/jsp/jspfaq.jsp
http://www.jguru.com/faq/index.jsp
http://www.jspinsider.com