aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Accessing the Servlet Init parameters in jsp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Accessing the Servlet Init parameters in jsp" Watch "Accessing the Servlet Init parameters in jsp" New topic
Author

Accessing the Servlet Init parameters in jsp

Senthil Kumar
Ranch Hand

Joined: Mar 13, 2006
Posts: 264
Hai,
i'm trying to access the servlet init parameters inside a jsp but all i got was null.

methods i tried.
1.<%=config.getInitParameter("PrimeMinister") %>
2.<%!
String pm = null;
public void doInit()
{
pm = getServletConfig().getInitParameter("PrimeMinister");
}
%>

but both of the above returns null.Can any one point me where i'm getting itr wrong.


when you really want something, all the universe always conspires in your favour.<br /> <br />SCJP1.5-77%<br />SCWCD-89%
Purushoth Thambu
Ranch Hand

Joined: May 24, 2003
Posts: 425
I think it's not possible to read the init-param of a servlet in a jsp page. The config object refers to the ServletConfig of the Jsp not the servlet you are intended to read from. I did some search on the API to see if we have retrieve the ServletConfig of a particular servlet and came up with empty.

If you want to share the params between JSP and servlet you should consider using context-params.
Mohd Fuzail
Ranch Hand

Joined: Feb 20, 2002
Posts: 107
Please do the mapping in DD so that servlet init parameters are avaibale to the JSP


Being defeated is often a temporary condition. Giving up is what makes it permanent.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Please show us your web.xml. You should have a servlet configured as a jsp file, with init parameters in it.


[My Blog]
All roads lead to JavaRanch
Senthil Kumar
Ranch Hand

Joined: Mar 13, 2006
Posts: 264
hi,
Here is the web.xml part.

<servlet>
<servlet-name>TestJspServlet</servlet-name>
<servlet-class>TestJspServlet</servlet-class>
<jsp-file>/third.jsp</jsp-file>
<init-param>
<param-name>PrimeMinister</param-name>
<param-value>Manmohan</param-value>
</init-param>
</servlet>

though i've specified the jsp file name in servlet description i cudn't get the servlet init parameters.

This is my servlet snippet
System.out.println(" Servlet init paramater =>"+getServletConfig().getInitParameter("PrimeMinister"));
RequestDispatcher dispatch = req.getRequestDispatcher("third.jsp");
dispatch.forward(req,res);
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

And what is the mapping to your servlet ? You have to access the jsp via the servlet mapping, not directly.
Senthil Kumar
Ranch Hand

Joined: Mar 13, 2006
Posts: 264
This is the mapping.
<servlet-mapping>
<servlet-name>TestJspServlet</servlet-name>
<url-pattern>/jsptest/*</url-pattern>
</servlet-mapping>

And i'm accessing the jsp by
http://localhost:8080/myapp/jsptest
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

And [b]you cannot mix servlet-class and jsp-file[b]
The servlet TestJspServlet.class and the servlet third.jsp have to be different.

Basically, you need this :


And then forward to TestJspServlet.do.
[ September 25, 2006: Message edited by: Satou kurinosuke ]
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Maybe you don't want to access init params from the JSP, you seem to be confusing the concept.

You could remove the jsp-file line from your web.xml, and in TestJspServlet.java :



And get it back from JSP, from the request.
Senthil Kumar
Ranch Hand

Joined: Mar 13, 2006
Posts: 264
i am really confused now.what is the use of <jsp-file> tag in <servlet> tag.
If we can't access the servlet's init parameters inside a jsp,is that for accessing the jsp init parameters.
And i've read in HFSJ that inside the jspInit() we have access to ServletConfig() and ServletContext().If that is the case i should be able to access the init parameters within the jspInit(),right.

Can you please elobarate me on this.
amar kasar
Greenhorn

Joined: Jan 06, 2006
Posts: 17
why are you using "PrimeMinister" as parameter, prime minister doesn't accessible generally.
Swati Udas
Ranch Hand

Joined: Aug 18, 2004
Posts: 121
Well as I understand from the reply from Satou kurinosuke
U CAN access the init parameters from a JSP by using the code he has given, but the recommended way is to access the init parameters from a servlet and pass them to the JSP as request attributes !


SCJP 1.4 (90%)<br />SCWCD 1.4 (88%)
Senthil Kumar
Ranch Hand

Joined: Mar 13, 2006
Posts: 264
Well,
Earlier i've misunderstood servlet init parameters with jsp init parameters.Now tell me what would the config.getInitParameter() inside a jsp return.Ideally it has to be the init parameters which i've set in the DD

<servlet>
<servlet-name>JspServlet</servlet-name>
<jsp-file>/third.jsp</jsp-file>
<init-param>
<param-name>skipper</param-name>
<param-value>Ponting</param-value>
</init-param>
</servlet>
[For your information i'm directly accessing the jsp now]
Still i'm getting the null from config object.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

For your information i'm directly accessing the jsp now

This is the problem. The container does not map a jsp file to init parameters. It maps servlets to their init parameters. So you have to access your jsp file via a url, which is mapped to it.
Try to add:
<servlet-mapping>
<servlet-name>JspServlet</servlet-name>
<url-pattern>/third</url-pattern>
</servlet-mapping>

And access it via "/third".
Senthil Kumar
Ranch Hand

Joined: Mar 13, 2006
Posts: 264
thanks a ton i got it right this time..
Nestor Fabio Alduey Gonzalez
Greenhorn

Joined: Sep 26, 2006
Posts: 10
i saw that all the traditional servlet methods are the same but with the _jspNameOFMethod

in this case would be _jspInit...

Nestor Alduey
SCJP 1.4
SCWCD (preparing)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Accessing the Servlet Init parameters in jsp