This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Servlets and the fly likes servlet init parameters Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "servlet init parameters" Watch "servlet init parameters" New topic
Author

servlet init parameters

robert dodson
Greenhorn

Joined: Mar 23, 2012
Posts: 8


"A servlet init parameter cannot have the same name with the servlet it refers to"

I think this is wrong but I dont know where to find out for sure and why its wrong. I think I read it somewhere but I can not find it can anyone explain this to a noob
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61103
    
  66

If it isn't in the Servlet Spec, it's not real.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
robert dodson
Greenhorn

Joined: Mar 23, 2012
Posts: 8
thanks for the fast response
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61103
    
  66

No problem. With all things servlets, the Spec has the final word.

You could also just try it and see what happens!
Swetha Bhagavathula
Ranch Hand

Joined: Jan 04, 2011
Posts: 112

robert dodson wrote:

"A servlet init parameter cannot have the same name with the servlet it refers to"

I think this is wrong but I dont know where to find out for sure and why its wrong. I think I read it somewhere but I can not find it can anyone explain this to a noob

Hai.. do you mean the life cycle method:public void init(ServletConfig servletconfig){//code}

if so then;please see this:

public void init(ServletConfig servletconfig){
this.config=config;
init();}


the above is the one of the life cycle methods of servlet..(the three life cycle methods of servlet:init(ServletConfig),service(ServletRequest,ServletResponse),destroy() )

a servlet interface contains this as a abstract method and GenericServlet(abstract class) implementing the " javax.servlet.Servlet(interface),javax.servlet.ServletConfig(interface), java.io.Serializable (marker interface)"contains this method.
init(-) method is the lifecycle method of servlet program which is executed by servlet container for instantiation event having ServletConfig object as argument value on our servlet class.since a good programmer will execute keeps his intialization logic of servlet program by overriding " init()" method and gives chance to inti(-) of GenericServlet class to execute.
the init(-) apart from performing initialization process ,invokes init() method at the end.
ServletCnfig object is the right hand(useful) object for our servlet program.It is one per servlet object.To pass information to servlet program ,we can use this ServletConfig object.Servlet container creates this
ServletConfig object when instantiation event is raised on our servlet class.
ServletConfig object means,it is the object of servlet container supplied java class that implements "javax.servlet.ServletConfig " interface.using this servletconfig bject when can get various details of our servlet program.our class(user defined class) extends HttpServlet class which is an abstract class that inturns extends GenericServlet class.
ex: public class servletdemo extends HttpServlet{//code}.As we cannot create object to HttpServlet class since abstract class we make our class extends it and use the required methods like doXxx(-,-) /service(-,-).
The beauty of HttpServlet class is, it an abstract class that contains no abstract methods(as any abstract class may or may not contain abstract method,means it can contain abstract methods also concrete methods)

for better understanding i suggest you to refer the source code of servlet-api. If you find the above explanation is not help full then please excuse me.. Thank you


SCJP5
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61103
    
  66

No, he was asking about servlet init parameters, not the init() life-cycle method.
Swetha Bhagavathula
Ranch Hand

Joined: Jan 04, 2011
Posts: 112

robert dodson wrote:

"A servlet init parameter cannot have the same name with the servlet it refers to"

I think this is wrong but I dont know where to find out for sure and why its wrong. I think I read it somewhere but I can not find it can anyone explain this to a noob

hi..
you can happily use same logical name for servlet as well as parameter name of your init-parameter.It is allowed because your<servlet-name> is the logical name used by the container during instatiation event.. object creation for you servlet class .. where as ini-parameter is used for a specific servlet to configure driver class names db url,dbpwd etc such parameter names.so these both are no where related .. note that init-parameter can have any value the paramter name of servlet program..same thing applies to context-parameter also.Hence,the statement is false.. A servlet iniit-parameter can have the same name with the servlet it refers to.. (this is true).

ex:
 
 
subject: servlet init parameters