• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to initialize jsp init parameters  RSS feed

 
Pradeep Kumar
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I want to send some init paramets to my InitJsp file. I am not able to do it in the Jsp as mentioned in HeadFirst servlets and Jsp book. I will just write the code for more clarificatio.

In the web.xml

<servlet>
<servlet-name>MyJsp</servlet-name>
<jsp-file>/InitJsp.jsp</jsp-file>

<init-param>
<param-name>email</param-name>
<param-value>abc@acs.co.in</param-value>
</init-param>
</servlet>

And in the Jsp file

<%! public void jspInit(){
ServletConfig sConfig = getServletConfig();
String mailId = sConfig.getInitParameter("email");
System.out.println("The mail id is "+mailId);
%>

Kindly let me know how to make it work. Also what is the servlet name i should give in <servlet-name> tag in the DD.

Thanks and Regards,
Pradeep
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your best bet would be to move all that logic into a Servlet and leave Java code out of the JSPs.
 
Bennet Xavier
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Pradeep,

i think your <url-pattern> element in <servlet-mapping> element is mapping some other url. if you are getting null for that.

let me know what output you are getting in the console.

do as per what Bear Bibeault said "avoid using scriptlet in jsp".

do reply.........
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The container automatically creates a url pattern that matches the name of the JSP file for you. It may be that the url-pattern generated by the container is overriding yours.

Try a different url pattern; one that doesn't match the name of the file.
For example: "InitJsp" and see if that works.


+1 for Bear's advice to move this logic out of JSPs.
But out of curiosity, I'd try to see if I could get this to work.
 
Pradeep Kumar
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi ranchers,
The code works fine. The mistake i did was i did not put the <servlet-mapping> tag and sub tags. It works fine. Thanks ranchers. :-)
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!