• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Retriving Init Param of a JSP

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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>


 
if you think brussel sprouts are yummy, you should try any other food. And this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic