Veena wrote:

> Craig
>
>     In Jason Hunter's Servlet pgmg book there is ex like this.he says,In b/w
> the <script runnat="server"> </script>,u can include any servlet code that
> should be placed outside the servlet's service method.I tried this,but it
> will be within service method.Any comments?
>
> Here is the jsp code
> hello3.jsp
> <HTML>
> <HEAD><TITLE>Hello</TITLE></HEAD>
> <BODY>
> <H1>
> Hello, <%= getName(request) %>
> </H1>
> </BODY>
> </HTML>
>
> <SCRIPT RUNAT="server">
> private static final String DEFAULT_NAME = "World";
>
> private String getName(HttpServletRequest req) {
>   String name = req.getParameter("name");
>   if (name == null)
>     return DEFAULT_NAME;
>   else
>     return name;
> }
> </SCRIPT>
>

In JSP 1.0 and JSP 1.1 (it *really* matters what version of the JSP spec your
server implements), the equivalent syntax would be this:  (note the "!" character):

<%!
    private static final String DEFAULT_NAME = "World";

    private String getName(HttpServletRequest req) {
        String name = req.getParameter("name");
        if (name == null)
            return DEFAULT_NAME;
        else
            return name;
    }
%>

This generates an instance variable DEFAULT_NAME (no problem sharing it because
it's a constant) and a function that becomes part of the generated class.

(Although why I'm helping people learn how to intermix Java code in their JSP pages
-- a style that I personally feel is fraught with problems -- is beyond me :-)

Craig

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
Some relevant FAQs on JSP/Servlets can be found at:

 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.html
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets

Reply via email to