Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Accessing the Servlet Init parameters in jsp

 
Senthil Kumar
Ranch Hand
Posts: 264
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Purushoth Thambu
Ranch Hand
Posts: 425
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please do the mapping in DD so that servlet init parameters are avaibale to the JSP
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please show us your web.xml. You should have a servlet configured as a jsp file, with init parameters in it.
 
Senthil Kumar
Ranch Hand
Posts: 264
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And what is the mapping to your servlet ? You have to access the jsp via the servlet mapping, not directly.
 
Senthil Kumar
Ranch Hand
Posts: 264
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 264
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why are you using "PrimeMinister" as parameter, prime minister doesn't accessible generally.
 
Swati Udas
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 !
 
Senthil Kumar
Ranch Hand
Posts: 264
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 264
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks a ton i got it right this time..
 
Nestor Fabio Alduey Gonzalez
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic