 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Frances Del Rio Guest
|
Posted: Tue Dec 28, 2004 2:42 pm Post subject: pls explain these methods... |
|
|
from servlet API:
getInitParameter(String) - Method in class javax.servlet.GenericServlet
Returns a String containing the value of the named initialization
parameter, or null if the parameter does not exist.
(what is "initialization parameter"?)
getAttribute(String) - Method in interface javax.servlet.ServletRequest
Returns the value of the named attribute as an Object, or null if
no attribute of the given name exists.
("named attribute" being what? attribute of WHAT?)
thank you... Frances
|
|
| Back to top |
|
 |
Juha Laiho Guest
|
Posted: Tue Dec 28, 2004 3:36 pm Post subject: Re: pls explain these methods... |
|
|
Frances Del Rio <fdr58 (AT) yahoo (DOT) com> said:
| Quote: | getInitParameter(String) - Method in class javax.servlet.GenericServlet
Returns a String containing the value of the named initialization
parameter, or null if the parameter does not exist.
(what is "initialization parameter"?)
|
Initialization parameters are one way of passing external
(configuration) data to your web application. F.ex. you could put
something like address of a database server to an initialization
parameter instead of coding it into the servlet. (actually, database
configuration would be better done at the servlet engine level, but this
should give you an idea)
Parameters are set in your application web.xml file; the servlet
specification has the details.
| Quote: | getAttribute(String) - Method in interface javax.servlet.ServletRequest
Returns the value of the named attribute as an Object, or null if
no attribute of the given name exists.
("named attribute" being what? attribute of WHAT?)
|
Attributes are pieces of data generated and processed during the
handling of a single request. Note that in servlet/JSP world, a single
request can cause invocation of multiple servlets: requests can be
forwarded from one servlet to another, or servlets can call other
servlets to provide part of the final contents (and as JSPs are basically
servlets, this applies to them just as well). So, one part in the
processing chain may produce some information that is needed by another
part, and pass the information as a request attribute. It's good to
note that the attributes are passed as objects, so any object type
can be passed. This is in contrast to the request parameters, which
are limited to be text-only.
--
Wolf a.k.a. Juha Laiho Espoo, Finland
(GC 3.0) GIT d- s+: a C++ ULSH++++$ P++@ L+++ E- W+$@ N++ !K w !O !M V
PS(+) PE Y+ PGP(+) t- 5 !X R !tv b+ !DI D G e+ h---- r+++ y++++
"...cancel my subscription to the resurrection!" (Jim Morrison)
|
|
| Back to top |
|
 |
Frances Del Rio Guest
|
Posted: Tue Dec 28, 2004 5:17 pm Post subject: Re: pls explain these methods... |
|
|
Juha Laiho wrote:
| Quote: | Frances Del Rio <fdr58 (AT) yahoo (DOT) com> said:
getInitParameter(String) - Method in class javax.servlet.GenericServlet
Returns a String containing the value of the named initialization
parameter, or null if the parameter does not exist.
(what is "initialization parameter"?)
Initialization parameters are one way of passing external
(configuration) data to your web application. F.ex. you could put
something like address of a database server to an initialization
parameter instead of coding it into the servlet. (actually, database
configuration would be better done at the servlet engine level, but this
should give you an idea)
|
Juha, thank you very much... connecting to a db is precisely what I'm
trying to do.. just starting out w/db (have mySql at my webhosting), am
very confused...
in example from my book it starts out w/this code:
String driver = getInitParameter("JdbcDriver");
String dbURL = getInitParameter(here put url given to me by webhosting..)
no idea whether what I put in there for JdbcDriver is correct... (and
not sure how to find out..)
you say Parameters are set in web.xml file; but as I understood it
'parameters' is what users fill out in form... (getParameter("name") ...
("name") being name of form element in HTML...
| Quote: | Parameters are set in your application web.xml file; the servlet
specification has the details.
|
not sure what you mean by "servlet specification"...
| Quote: | getAttribute(String) - Method in interface javax.servlet.ServletRequest
Returns the value of the named attribute as an Object, or null if
no attribute of the given name exists.
("named attribute" being what? attribute of WHAT?)
Attributes are pieces of data generated and processed during the
handling of a single request. Note that in servlet/JSP world, a single
request can cause invocation of multiple servlets: requests can be
forwarded from one servlet to another, or servlets can call other
servlets to provide part of the final contents (and as JSPs are basically
servlets, this applies to them just as well). So, one part in the
processing chain may produce some information that is needed by another
part, and pass the information as a request attribute. It's good to
note that the attributes are passed as objects, so any object type
can be passed. This is in contrast to the request parameters, which
are limited to be text-only.
|
ok, so when you 'set' attribute, you set attribute of what, precisely?
sorry, this still confuses me... is this only used when you need to
comm. betw. a jsp and a servlet, for example? (still trying to figure
out how to comm. betw. a servlet and a jsp..) again, thank you very
much.. Frances
|
|
| Back to top |
|
 |
Ryan Stewart Guest
|
Posted: Tue Dec 28, 2004 6:12 pm Post subject: Re: pls explain these methods... |
|
|
"Frances Del Rio" <fdr58 (AT) yahoo (DOT) com> wrote
| Quote: | Juha, thank you very much... connecting to a db is precisely what I'm
trying to do.. just starting out w/db (have mySql at my webhosting), am
very confused...
in example from my book it starts out w/this code:
String driver = getInitParameter("JdbcDriver");
String dbURL = getInitParameter(here put url given to me by webhosting..)
As Juha partly mentioned, a better place for DB parameters is the |
ServletContext. Put this in web.xml:
<context-param>
<param-name>JdbcDriver</param-name>
<param-value>my.driver.Class</param-value>
</context-param>
Then in your servlet:
String driver = this.getServletContext().getInitParameter("JdbcDriver");
"driver" now referes to the String "my.driver.Class"
| Quote: | no idea whether what I put in there for JdbcDriver is correct... (and not
sure how to find out..)
you say Parameters are set in web.xml file; but as I understood it
'parameters' is what users fill out in form... (getParameter("name") ...
("name") being name of form element in HTML...
"Parameter" is a word with many uses. This can be confusing. The parameters |
in web.xml are context initialization parameters and servlet initialization
parameters (there are others, but leave it at that for now). The word
"parameter" also refers to request parameters. i.e.
request.getParameter("name"). What I demonstrated above was a context
initialization parameter. A servlet initialization parameter would look like
this (taken from the Servlet 2.3 spec):
<servlet>
<servlet-name>catalog</servlet-name>
<servlet-class>com.mycorp.CatalogServlet</servlet-class>
<init-param>
<param-name>catalog</param-name>
<param-value>Spring</param-value>
</init-param>
</servlet>
The reason that DB info is better suited to the context, aka ServletContext,
is that it's information that should be available to your entire
application, not just a single servlet.
Servlet 2.4 : http://www.jcp.org/aboutJava/communityprocess/final/jsr154/
They're not actually written for developers, but they are very informative.
| Quote: | ok, so when you 'set' attribute, you set attribute of what, precisely?
sorry, this still confuses me... is this only used when you need to comm.
betw. a jsp and a servlet, for example? (still trying to figure out how to
comm. betw. a servlet and a jsp..) again, thank you very much.. Frances
The following interfaces have or inherict set- and getAttribute methods: |
ServletContext, HttpSession, HttpServletRequest, PageContext. Those
correspond to the implicit JSP objects application, session, request, and
pageContext, respectively. I will assume for now that you are familiar with
these four "scopes". The signatures of all the setAttribute methods are:
public void setAttribute(String name, Object value)
The signatures of all the getAttribute methods are:
public Object getAttribute(String name)
This means that you can store any object in any of the four scopes for later
use and retrieve said object at any time. For example:
session.setAttribute("current.user", user);
This will store whatever object "user" refers to in the session with the
name "current.user". Anywhere within that session now, you may use:
User user = (User) session.getAttribute("current.user");
to retrieve that user (assuming your user class is User). Note that the cast
is necessary as the getAttribute method returns an Object.
|
|
| Back to top |
|
 |
Frances Del Rio Guest
|
Posted: Tue Dec 28, 2004 6:24 pm Post subject: Re: pls explain these methods... |
|
|
Ryan Stewart wrote:
| Quote: | "Frances Del Rio" <fdr58 (AT) yahoo (DOT) com> wrote in message
news:33dilbF3vs0b6U1 (AT) individual (DOT) net...
Juha, thank you very much... connecting to a db is precisely what I'm
trying to do.. just starting out w/db (have mySql at my webhosting), am
very confused...
in example from my book it starts out w/this code:
String driver = getInitParameter("JdbcDriver");
String dbURL = getInitParameter(here put url given to me by webhosting..)
As Juha partly mentioned, a better place for DB parameters is the
ServletContext. Put this in web.xml:
context-param
param-name>JdbcDriver</param-name
param-value>my.driver.Class</param-value
/context-param
Then in your servlet:
String driver = this.getServletContext().getInitParameter("JdbcDriver");
"driver" now referes to the String "my.driver.Class"
no idea whether what I put in there for JdbcDriver is correct... (and not
sure how to find out..)
you say Parameters are set in web.xml file; but as I understood it
'parameters' is what users fill out in form... (getParameter("name") ...
("name") being name of form element in HTML...
"Parameter" is a word with many uses. This can be confusing. The parameters
in web.xml are context initialization parameters and servlet initialization
parameters (there are others, but leave it at that for now). The word
"parameter" also refers to request parameters. i.e.
request.getParameter("name"). What I demonstrated above was a context
initialization parameter. A servlet initialization parameter would look like
this (taken from the Servlet 2.3 spec):
servlet
servlet-name>catalog</servlet-name
servlet-class>com.mycorp.CatalogServlet</servlet-class
init-param
param-name>catalog</param-name
param-value>Spring
/init-param
/servlet
The reason that DB info is better suited to the context, aka ServletContext,
is that it's information that should be available to your entire
application, not just a single servlet.
Parameters are set in your application web.xml file; the servlet
specification has the details.
not sure what you mean by "servlet specification"...
Servlet 2.3 : http://www.jcp.org/aboutJava/communityprocess/final/jsr053/
Servlet 2.4 : http://www.jcp.org/aboutJava/communityprocess/final/jsr154/
|
ok, this is API, right? I just downloaded it, it's same servlet API
that comes w/tomcat... thank you very much for yr help... Frances
|
|
| Back to top |
|
 |
Ryan Stewart Guest
|
Posted: Tue Dec 28, 2004 6:24 pm Post subject: Re: pls explain these methods... |
|
|
"Frances Del Rio" <fdr58 (AT) yahoo (DOT) com> wrote
a different purpose.
|
|
| Back to top |
|
 |
Juha Laiho Guest
|
Posted: Wed Dec 29, 2004 12:52 pm Post subject: Re: pls explain these methods... |
|
|
Frances Del Rio <fdr58 (AT) yahoo (DOT) com> said:
| Quote: | Juha Laiho wrote:
Frances Del Rio <fdr58 (AT) yahoo (DOT) com> said:
getInitParameter(String) - Method in class javax.servlet.GenericServlet
Returns a String containing the value of the named initialization
parameter, or null if the parameter does not exist.
(what is "initialization parameter"?)
Initialization parameters are one way of passing external
(configuration) data to your web application. F.ex. you could put
something like address of a database server to an initialization
parameter instead of coding it into the servlet. (actually, database
configuration would be better done at the servlet engine level, but this
should give you an idea)
Juha, thank you very much... connecting to a db is precisely what I'm
trying to do.. just starting out w/db (have mySql at my webhosting), am
very confused...
in example from my book it starts out w/this code:
String driver = getInitParameter("JdbcDriver");
String dbURL = getInitParameter(here put url given to me by webhosting..)
no idea whether what I put in there for JdbcDriver is correct... (and
not sure how to find out..)
|
You already got some details from Ryan about where to put the configuration;
then to the issue of what to use as JdbcDriver; that should be a name
of a class that can be used as a JDB driver for your database. So, for
MySQL, I think it should be "com.mysql.jdbc.Driver" (depends on which
exact mysql JDBC implementation you use). Also, a library ( .jar file)
containing the class com.mysql.jdbc.Driver should be somewhere accessible
by the application (f.ex. in WEB-INF/lib).
| Quote: | you say Parameters are set in web.xml file; but as I understood it
'parameters' is what users fill out in form... (getParameter("name") ...
("name") being name of form element in HTML...
|
There are parameters and parameters - the word is generic enough to be
used in multiple contexts and with multiple meanings. Here above you
refer to parameters contained in the HTTP request your servlet is
processing.
| Quote: | Parameters are set in your application web.xml file; the servlet
specification has the details.
not sure what you mean by "servlet specification"...
|
Please do read the PDF document (so, not just the API docs, but the actual
specification document) that can be downloaded from the pages pointed to
by Ryan. That is the specification on how servlet containers (must) work.
Even though Ryan said it's not written for developers, I disagree; it
should be mandatory material for anyone writing servlets. It is not
a tutorial, but I think it gives a good overview of the servlet runtime
environment.
| Quote: | getAttribute(String) - Method in interface javax.servlet.ServletRequest
Returns the value of the named attribute as an Object, or null if
no attribute of the given name exists.
("named attribute" being what? attribute of WHAT?)
Attributes are pieces of data generated and processed during the
handling of a single request. Note that in servlet/JSP world, a single
request can cause invocation of multiple servlets: requests can be
forwarded from one servlet to another, or servlets can call other
servlets to provide part of the final contents (and as JSPs are basically
servlets, this applies to them just as well). So, one part in the
processing chain may produce some information that is needed by another
part, and pass the information as a request attribute. It's good to
note that the attributes are passed as objects, so any object type
can be passed. This is in contrast to the request parameters, which
are limited to be text-only.
ok, so when you 'set' attribute, you set attribute of what, precisely?
|
You set an attribute of a HTTP request - in a case where the current
servlet is not providing the final response to the client, but only
doing some preparatory (or partial) work related to the request. The
attribute may be any data that your application needs to pass on along
with the request to some other piece of the request processing chain.
| Quote: | sorry, this still confuses me... is this only used when you need to
comm. betw. a jsp and a servlet, for example?
|
It can be used at any time when passing the request to another piece
in the processing chain using the RequestDispatcher methods include
and forward.
So, for example, you have a servlet doing some request data validation,
creating some request-related object based on that, and want to use
a JSP page to generate the actual request:
public class MyServlet
extends HttpServlet {
// ...
protected void doGet(HttpServletRequest req, HttpServletResponse res)
throws java.io.IOException, ServletException {
MyData d;
RequestDispatcher rd;
// ... something to create the MyData object, most possibly
// dependent on the request parameters
req.setAttribute("attribName",d);
rd=this.getServletContext().getRequestDispatcher("/some.jsp");
rd.forward(req,res);
}
}
.... and then in the JSP page, you can then access the MyData object
from the request object in the JSP code. Sorry, I don't have
concrete examples of what the data to be passed would be - it is
completely dependent on your application needs.
--
Wolf a.k.a. Juha Laiho Espoo, Finland
(GC 3.0) GIT d- s+: a C++ ULSH++++$ P++@ L+++ E- W+$@ N++ !K w !O !M V
PS(+) PE Y+ PGP(+) t- 5 !X R !tv b+ !DI D G e+ h---- r+++ y++++
"...cancel my subscription to the resurrection!" (Jim Morrison)
|
|
| Back to top |
|
 |
Ryan Stewart Guest
|
Posted: Wed Dec 29, 2004 1:44 pm Post subject: Re: pls explain these methods... |
|
|
"Juha Laiho" <Juha.Laiho (AT) iki (DOT) fi> wrote
| Quote: | Please do read the PDF document (so, not just the API docs, but the actual
specification document) that can be downloaded from the pages pointed to
by Ryan. That is the specification on how servlet containers (must) work.
Even though Ryan said it's not written for developers, I disagree; it
should be mandatory material for anyone writing servlets. It is not
a tutorial, but I think it gives a good overview of the servlet runtime
environment.
I tend to agree with you that it is a great source of information. Whether |
it should be mandatory is another question. And straight from the spec
itself: "We emphasize that this specification is not a user's guide for
servlet developers and is not intended to be used as such." It should give a
good overview of the servlet engine since it's the specification of how it
is required to work.
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|