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>



Here is the currosponding workhorse servlet generated
_hello3.java

/*
* This code was automatically generated at 4:09:15 PM on Feb 15, 2000
* by weblogic.servlet.jsp.Jsp2Java -- do not edit.
*/

package jsp_servlet;

import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;

// User imports


// built-in init parameters:
// boolean             _verbose -- wants debugging

// Well-known variables:
// JspWriter out                  -- to write to the browser
// HttpServletRequest  request    -- the request object.
// HttpServletResponse response   -- the response object.
// PageContext pageContext        -- the page context for this JSP
// HttpSession session            -- the session object for the client (if
any)
// ServletContext application     -- The servlet (application) context
// ServletConfig config           -- The ServletConfig for this JSP
// Object page                    -- the instance of this page's
implementation class (i.e., 'this')

/**
* This code was automatically generated at 4:09:15 PM on Feb 15, 2000
* by weblogic.servlet.jsp.Jsp2Java -- do not edit.
*
* Copyright (c) 2000 by BEA Systems, Inc. All Rights Reserved.
*/
public class _hello3
extends
weblogic.servlet.jsp.JspBase
implements weblogic.servlet.jsp.StaleIndicator
{

    // StaleIndicator interface
    public boolean _isStale() {
        java.io.File f = null; long lastModWhenBuilt = 0L, lastMod = 0L;
        f = new File("C:\\weblogic\\myserver\\public_html\\hello3.jsp");
        lastModWhenBuilt = 948777312000L;
        lastMod = f.lastModified();
        if (!f.exists() || lastMod > lastModWhenBuilt) return true;
        return false;
    }




    public void _jspService(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response) throws java.io.IOException,
javax.servlet.ServletException



        // declare and set well-known variables:
        javax.servlet.ServletConfig config = getServletConfig();
        javax.servlet.ServletContext application =
config.getServletContext();
        Object page = this;
        boolean _needToFlush = true;
        javax.servlet.jsp.JspWriter out;
        PageContext pageContext =
        JspFactory.getDefaultFactory().getPageContext(this, request,
response, null, true, 8192, true);

        out = pageContext.getOut();
        HttpSession session = request.getSession(true);



        try { // begin flush block


out.print("<HTML>\n<HEAD><TITLE>Hello</TITLE></HEAD>\n<BODY>\n<H1>\nHello,
");


          out.print(weblogic.utils.StringUtils.valueOf( getName(request) ));
file://[ hello3.jsp; Line: 5]
            out.print("\n</H1>\n</BODY>\n</HTML>\n\n<SCRIPT
RUNAT=\"server\">\nprivate static final String DEFAULT_NAME =
\"World\";\n\nprivate String getName(HttpServletRequest req) {\n  String
name = req.getParameter(\"name\");\n  if (name == null)\n    return
DEFAULT_NAME;\n  else\n    return name;\n}\n</SCRIPT>\n");
        } finally {
            if (_needToFlush) out.flush();
        }


        file://before final close brace...
    }


}





----- Original Message -----
From: Craig R. McClanahan <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Tuesday, June 20, 2000 8:10 AM
Subject: Re: runat="server"?


> Marc Krisjanous wrote:
>
> > Thats interesting,
> >
> > But in the case of the JSP when compiled by websphere my code is
contained
> > within the service method including the vars.  From my understanding of
your
> > email I would have to make the service method synchronized. What are
your
> > thoughts??
> >
>
> Variables that are local to the service() method -- or any other method in
Java
> classes, for that matter -- are stored on a per-thread stack.  This means
that
> there is no conflict between the local variables when two or more users
are
> executing the same page at the same time.
>
> Instance variables, on the other hand, exist once per page.  This is nice
if you
> really want to share information between all requests that use this page,
but it is
> not what you want when dealing with the variables related to a particular
request.
>
> Note that when you create a bean variable with <jsp:useBean> in JSP 1.x
(or <bean>
> in previous versions), this variable is local.  That's why you don't have
problems
> with multiple users scribbling on it.
>
> Craig McClanahan
>
>
> >
> > -----Original Message-----
> > From: Jim Bailey [mailto:[EMAIL PROTECTED]]
> > Sent: Tuesday, 20 June 2000 9:57 AM
> > To: [EMAIL PROTECTED]
> > Subject: Re: runat="server"?
> >
> > Yes, correct. That is why you shouldn't use that form of declaration for
> > anything except thread safe variables. In the example I gave it is a
static
> > final which is guaranteed thread safe since it is a constant. If you do
use
> > class variables in your JSP/Servlet then you will probably have to use a
> > single thread model of JSP which isn't supported well in Websphere 2.0x
and
> > JSP 0.91 or you will have to synchronize any method that uses it and
> > anyplace in the JSP that uses the variables.
> >
> > It isn't any better with the <%! form in JSP 1.x. As a developer you
have to
> > be careful to make sure your JSP is thread safe. The easy way to do that
is
> > to use the <%@ page isThreadSafe="false" %> switch. The problem with
that is
> > it is very inefficient compared to the normal isThreadSafe="true"
> > implementation. Basically your whole JSP is either one user at a time or
> > each JSP invocation gets a new instance of the servlet, depending on the
> > implementation of the servlet/JSP engine.
> >
> > -----Original Message-----
> > From: Marc Krisjanous [mailto:[EMAIL PROTECTED]]
> > Sent: Monday, June 19, 2000 7:44 PM
> > To: [EMAIL PROTECTED]
> > Subject: Re: runat="server"?
> >
> > but wouldn't that cause data corruption since the variable is an
Instance
> > class variable and all threads could access it, possibly at the same
time.
> > You would have to synchronized all methods that used the vars.
> >
> > Marc
> >
> > -----Original Message-----
> > From: Jim Bailey [mailto:[EMAIL PROTECTED]]
> > Sent: Tuesday, 20 June 2000 3:41 AM
> > To: [EMAIL PROTECTED]
> > Subject: Re: runat="server"?
> >
> > As far as I can tell, the first example shouldn't be valid. It should
create
> > a syntax error: "illegal modifier on local variable" or something like
that.
> >
> > The second example is valid on JSP 0.91 and Websphere 2.0x for sure but
I
> > don't know how "standard" it is. It puts the variable myInt as a class
> > static variable on the servlet generated from the .JSP file. It is
> > equivalent to the following:
> >
> > public class _int_test__jsp extends HttpServlet
> > {
> >   private static final int myInt = 0;
> >
> >   public void
> >   _jspService(HttpServletRequest request, HttpServletResponse response)
> >         throws IOException, ServletException
> >   {... etc
> > }
> >
> >
===========================================================================
> > 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
> >
> >
===========================================================================
> > 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
>
>
===========================================================================
> 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

===========================================================================
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