• Post Reply Bookmark Topic Watch Topic
  • New Topic

Servlet Object in JSP  RSS feed

 
somasundar venkatesh
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
Can we access or get Servlet object in JSP page. If so, then what type and how we can perform operations on that object.

Thanks,
Somu
 
John Eipe
Ranch Hand
Posts: 215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

You mean ServletConfig? I guess you can use
config Implicit Object inside scriptlets

and in EL as follows


You can use ServletConfig object to retrieve servlet specific values but mostly you will you it to retrieve init parameters.

Regards,
John

EDIT: The EL code didn't work when I tried. Something is wrong.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66154
146
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For what purpose? You shouldn't even have any Java code in your JSPs. That is a practice that has been discredited for almost 10 years now. You should using only JSTL and EL in JSP pages.
 
John Eipe
Ranch Hand
Posts: 215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. But

threw exception
javax.el.MethodNotFoundException: Unable to find method [initParameter] with [1] parameters
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66154
146
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What version of JSP are you using? Unless it is the latest in a container like Tomcat 7, you can't call methods in the EL. And even if so, there is no initParam() method on ServletConfig.

But what it all comes down to is why aren't you just using the ${initParam} implicit EL variable to access the context parameters?
 
John Eipe
Ranch Hand
Posts: 215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As a matter of fact, I saw this link:
http://www.coderanch.com/how-to/java/InitParamsThruExpressionLanguage
there it concludes as follows:
If you want to access Servlet init parameters using EL, then it is better to create a Map of the init parameters for the servlet and place it in the request as a scoped variable.


It would be great if someone could show an example code as I'm new to JSP .
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66154
146
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sometimes I wonder why I bother replying when you aren't even going to read what I write.

I repeat:

Bear Bibeault wrote:But what it all comes down to is why aren't you just using the ${initParam} implicit EL variable to access the context parameters?


The map is already there! Why would create another one? Just make your parameters context parameters and you are home free!
 
somasundar venkatesh
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi John,

I don't mean ServletConfig Object. I mean Servlet Object itself. Can you please clarify on this.

Thanks,
Somu







John Eipe wrote:Hi

You mean ServletConfig? I guess you can use
config Implicit Object inside scriptlets

and in EL as follows


You can use ServletConfig object to retrieve servlet specific values but mostly you will you it to retrieve init parameters.

Regards,
John

EDIT: The EL code didn't work when I tried. Something is wrong.


 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66154
146
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I repeat. Why do you need this?
 
somasundar venkatesh
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've faced this question in one of my interview and still I didn't get the answer.

Bear Bibeault wrote:I repeat. Why do you need this?

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66154
146
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's a stupid question. The fact that JSP is layered on servlets is moot and should be ignored. But the answer that they were likely looking for is the page implicit variable.

It's only available in scriptlets, which should no longer be used, so it is useless information.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!