aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes how to get init-param in jsp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "how to get init-param in jsp" Watch "how to get init-param in jsp" New topic
Author

how to get init-param in jsp

Bobby Sharma
Ranch Hand

Joined: Mar 18, 2008
Posts: 550
    
    1

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


Back to Java , again.
nav katoch
Ranch Hand

Joined: May 02, 2008
Posts: 246

Try with application.getInitParameter(" ")

Thanks, Naveen.
nav katoch
Ranch Hand

Joined: May 02, 2008
Posts: 246

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

Joined: May 04, 2007
Posts: 332
try to remove this line from DD:
<servlet-class>TestServlet</servlet-class>
Bobby Sharma
Ranch Hand

Joined: Mar 18, 2008
Posts: 550
    
    1

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

Joined: Mar 18, 2008
Posts: 550
    
    1

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

Joined: Mar 18, 2008
Posts: 550
    
    1

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

Joined: May 04, 2007
Posts: 332

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

Joined: Mar 18, 2008
Posts: 550
    
    1

So any idea? why I am getting nulls?

best regards,
omi
John Stone
Ranch Hand

Joined: May 04, 2007
Posts: 332
What URL are you typing in address bar?
/Test.jsp
or
/Hobby.do?
John Stone
Ranch Hand

Joined: May 04, 2007
Posts: 332
*.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

Joined: Mar 18, 2008
Posts: 550
    
    1

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

Joined: May 02, 2008
Posts: 246

config
Bobby Sharma
Ranch Hand

Joined: Mar 18, 2008
Posts: 550
    
    1

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

Joined: May 04, 2007
Posts: 332
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

Joined: Mar 18, 2008
Posts: 550
    
    1

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

Joined: Aug 31, 2005
Posts: 29
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

Joined: Mar 18, 2008
Posts: 550
    
    1

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

Joined: Feb 10, 2011
Posts: 52

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.


SCJP 6 (93%), SQL Expert 11g (95%), SCWCD 6 (84%), OCE-EJBD 6 (98%), OCE-JPAD 6 (93%)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to get init-param in jsp
 
Similar Threads
i get ' null ' for jsp init params
Cannot retrieve init parameter through JSP file
question--Initializing JSP
Getting Junk Values While overridding jspInit()
Init parameter null