This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Retriving Init Param of a JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Retriving Init Param of a JSP " Watch "Retriving Init Param of a JSP " New topic
Author

Retriving Init Param of a JSP

Ashok Markkanden
Greenhorn

Joined: Oct 14, 2005
Posts: 3
I need a clarification in jsp .

I have set initparam to a jsp file . once a request is made from the html the request is sent to a Servlet from where i call the JSP using REQUESTDISPATCHER.
When i do so the page is dynamically loaded on the client side but i am unable to retrieve the parameter whihc was set in the web.xml for jsp.



the result page has the URL ::

http://localhost:9090/scwcd/SampleJspView/SampleJspView?

o/p :: is

Counter::

null


But When i access the URL

http://localhost:9090/scwcd/SampleJsp -------------> this is the URL pattern that i gave for JSP in servlet mapping

i can able to get the correct output

O/P::
counter::

ashok.markkandan@abcd.com




html::
--------
SampleJspView .html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
</HEAD>

<BODY>
<FORM ACTION = "SampleJspView/SampleJspView" METHOD = "GET">

<INPUT TYPE = "SUBMIT"/>
</FORM>
</BODY>
</HTML>


Servlet::
-------------

SampleJspView .java


package SampleJspView;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class SampleJspView extends HttpServlet
{

public void doGet(HttpServletRequest request, HttpServletResponse response)throws IOException,ServletException{

response.setContentType("text/html");
PrintWriter out = response.getWriter();

HttpSession session = request.getSession();
RequestDispatcher rd = request.getRequestDispatcher("/JspCounter.jsp");
rd.forward(request,response);


}
}


jsp::
------
JspCounter.jsp

<html>
<body>
<H1>Counter::<H1>
<%! String email;%>
<%! public void jspInit(){
ServletConfig sc = getServletConfig();
email = sc.getInitParameter("email");

ServletContext ctx = getServletContext();
ctx.setAttribute("mail",email);
}
%>
<%=email%>

</body>
</html>


web.xml
-------------

<servlet>
<servlet-name>SampleJspView</servlet-name>
<servlet-class>SampleJspView.SampleJspView</servlet-class>
</servlet>


<servlet>
<servlet-name>SampleJsp</servlet-name>
<jsp-file>
/JspCounter.jsp
</jsp-file>
<init-param>
<param-name>
email
</param-name>
<param-value>
ashok.markkandan@abcd.com
</param-value>
</init-param>

</servlet>


<servlet-mapping>
<servlet-name>SampleJspView</servlet-name>
<url-pattern>/SampleJspView/SampleJspView</url-pattern>
</servlet-mapping>


<servlet-mapping>
<servlet-name>SampleJsp</servlet-name>
<url-pattern>/SampleJsp</url-pattern>
</servlet-mapping>


Please advise me on this
Gaurav Gambhir
Ranch Hand

Joined: Mar 04, 2006
Posts: 254
In the first case when you are getting Counter::null by accessing
http://localhost:9090/scwcd/SampleJspView/SampleJspView?
container is looking for init-paramater for SampleJspView that are not
defined as a result you got null

But when you are accessing http://localhost:9090/scwcd/SampleJsp
whose corresponding JSP file is JspCounter.jsp
having init-parameter email, it gives the value for that

If you need result for the first case define parameters for that
Ashok Markkanden
Greenhorn

Joined: Oct 14, 2005
Posts: 3
hey Gaurav,
Thanks for your reply.The solution that you gave was for setting init-param to a servlet and retrieve it through config object.
But the thing is i am going to add init-param only for a JSP, which should be retrieved when the Request is forwarded to a JSP.



I got the solution for it ..the jsp file name has to be given in the URL-PATTERN element in web.xml, so that the init param value set for a jsp can be retrieved in the same URL that servlet was called

<servlet-mapping>
<servlet-name>SampleJsp</servlet-name>
<url-pattern>/JspCounter.jsp</url-pattern>
</servlet-mapping>


 
Don't get me started about those stupid light bulbs.
 
subject: Retriving Init Param of a JSP
 
Similar Threads
Using getInitParameter( ) method
Doubt about how configuring init parameters in JSP
got stuck in running my first struts example. help
showing one jsp instead of other
servlets