Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to get init-param in jsp

 
Bobby Sharma
Ranch Hand
Posts: 598
3
Google App Engine jQuery Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not able to get init-param in jsp. please tell me how to get it?



From above code you can see that I tried two ways to get init-param without
success ,I only get nulls .



best regards,
omi
 
nav katoch
Ranch Hand
Posts: 246
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try with application.getInitParameter(" ")

Thanks, Naveen.
 
nav katoch
Ranch Hand
Posts: 246
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Disregard the contents of the previous post. Try this way:-

<%! String emailHost = null;
String webMaster = null;
public void jspInit() {
ServletConfig config = getServletConfig();
emailHost = config.getInitParameter("emailHost");
webMaster = config.getInitParameter("webMaster");
}
%>
 
John Stone
Ranch Hand
Posts: 332
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try to remove this line from DD:
<servlet-class>TestServlet</servlet-class>
 
Bobby Sharma
Ranch Hand
Posts: 598
3
Google App Engine jQuery Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by n katoch:
Disregard the contents of the previous post. Try this way:-

<%! String emailHost = null;
String webMaster = null;
public void jspInit() {
ServletConfig config = getServletConfig();
emailHost = config.getInitParameter("emailHost");
webMaster = config.getInitParameter("webMaster");
}
%>


You did the same thing I did ,Please give a second thought of my post.
Declaring variable outside the jspInit method doesn't change the output because we have to set attribute inside the method.
It's not working.

best regards,
omi
 
Bobby Sharma
Ranch Hand
Posts: 598
3
Google App Engine jQuery Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by John Stone:
try to remove this line from DD:
<servlet-class>TestServlet</servlet-class>


LOL,what made you think if I remove the line ,it will be fine.

My friend,It will get bigger problem as big fat null pointer exception.

best regards,
omi
 
Bobby Sharma
Ranch Hand
Posts: 598
3
Google App Engine jQuery Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The main problem is getInitParameter() does not get parameter for me,
neither as stand-alone nor inside the method.

best regards,
omi
 
John Stone
Ranch Hand
Posts: 332
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

LOL,what made you think if I remove the line ,it will be fine.

The fact, that you have both <jsp-file> and <servlet-class> used in one <servlet> element.
 
Bobby Sharma
Ranch Hand
Posts: 598
3
Google App Engine jQuery Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So any idea? why I am getting nulls?

best regards,
omi
 
John Stone
Ranch Hand
Posts: 332
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What URL are you typing in address bar?
/Test.jsp
or
/Hobby.do?
 
John Stone
Ranch Hand
Posts: 332
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
*.jsp is implicitly mapped to org.apache.jasper.servlet.JspServlet, so if you are writing .jsp in address bar, that could be the reason.

from http://faq.javaranch.com/view?InvokerServlet
 
Bobby Sharma
Ranch Hand
Posts: 598
3
Google App Engine jQuery Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by John Stone:
What URL are you typing in address bar?
/Test.jsp
or
/Hobby.do?


I am trying to access /Hobby.do via form.

forget my problem, the real question is how to access init-param in jsp?
(not context-param)

applicaion.getInItParameter() is for context-param so what for init-param?

best regards,
omi
[ May 23, 2008: Message edited by: omi sharma ]
 
nav katoch
Ranch Hand
Posts: 246
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
config
 
Bobby Sharma
Ranch Hand
Posts: 598
3
Google App Engine jQuery Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by n katoch:
config


Yea that's for init-param but the funny thing is it is also getting null
like others. LOL

But after setting deployment descriptor according to this code , it's working fine.



Actually init-param is not for jsp it's for servlet for jsp we use
context-param like a charm.

John isn't that the same you were trying tell me?

thanks guys for replies

best regards,
omi
[ May 23, 2008: Message edited by: omi sharma ]
 
John Stone
Ranch Hand
Posts: 332
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I took your code, removed <servlet-class> element, accessed the page via /Hobby.do and I can see init params.





output:
 
Bobby Sharma
Ranch Hand
Posts: 598
3
Google App Engine jQuery Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
LOL,I think we both were posting at the same time

thanks John

best regards,
omi
[ May 23, 2008: Message edited by: omi sharma ]
 
Vijay Bharghav bheemineni
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Omi,

I feel that your statement "Actually init-param is not for jsp it's for servlet for jsp we use context-param like a charm" is "wrong".

Jsp is nothing but finally Servlet. We can also set "init-param" to JSP. Your solution of "<context-param>" is just alternate solution but you didn't fix the root cause.

John was right, actual problem is we can't have both "<jsp-file>" and "<servlet-class>" tag, at any time we can have only "<jsp-file>" or <servlet-class>" tag.

I would request you to check the first version of the code by removing the "<servlet-class>" tag.

Thanks.

Vijay Bheemineni.
 
Bobby Sharma
Ranch Hand
Posts: 598
3
Google App Engine jQuery Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Vijay Bharghav bheemineni:
Hi Omi,

I feel that your statement "Actually init-param is not for jsp it's for servlet for jsp we use context-param like a charm" is "wrong".


John was right, actual problem is we can't have both "<jsp-file>" and "<servlet-class>" tag, at any time we can have only "<jsp-file>" or <servlet-class>" tag.


I would request you to check the first version of the code by removing the "<servlet-class>" tag.



Well my dear ,jsp is servlet for container not for client.
If jsp is really servlet for client ,then please tell me why do we need
to use <jsp-file> element in DD?


Yes , I know John was right.I did not know the whole story because I had
form-parameters to access via servlet and then process it in jsp and a model java class.When I tried to remove <servlet-class> I got null-pointer exception so that time I thought what was he talking about.Later I knew what did he mean.

I already solved my problem. Thank you for reply.

best regards,
omi
[ May 23, 2008: Message edited by: omi sharma ]
 
Cosmin Nicolae Vacaroiu
Ranch Hand
Posts: 55
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, servlet-class must be removed. You either have the servlet from servlet-class, or the servlet generated by the jsp-file.

It shoud work:
<% out.print(pageContext.getServletConfig().getInitParameter("name")); %>

ps: in EL you can't get them.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic