Link to home
Start Free TrialLog in
Avatar of dkim18
dkim18

asked on

reading parameter out of the servlet context

Hello,

I am doing this assignment.

make a servlet of JSP page that reads the company name from init parameters and stores it in the servlet context. Have another page use the same company name by reading it out of the servlet context(not from init parameters). Be sure nothing bad happens if someone tries to access the second page before the first.

Here is what I did so far.
I added the following in web.xml.

...
      <servlet-name>InitPage</servlet-name>
      <jsp-file>/InitPage.jsp</jsp-file>
       <init-param>
         <param-name>companyName</param_name>
         <param-value>ABC</param-value>
       </init-param>
      <servlet-mapping>
        <servlet-name>InitPage</servlet-name>
        <url-pattern>/InitPage.jsp</url-pattern>
      </servlet-mapping>  

this is InitPage.jsp:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> HW#8 - #3</TITLE>

</HEAD>
<BODY>
   <LI> Company Name: <%= companyName %>
<%!
      private String companyName;
      public void jspInit() {
            ServletConfig config = getServletConfig();
            companyName = config.getInitParameter("companyName"); }
%>
</BODY>
</HTML>

I guess this will  read the company name from init parameters and stores it in the servlet context. Now, how do I make another page uses the same company name by reading it out of the servlet context?

thanks,

ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I generally use a servlet to copy the init parameter into the application context (on startup).
Avatar of dkim18
dkim18

ASKER

I just got null for company name.

<BODY>
   <H2>Init Parameter</H2>
   <LI> Company Name: <%= application.getAttribute("ABC") %>
</BODY>
</HTML>
you still need to copy the init param into the servlet context at startup.

application.setAttribute(config.getInitParameter("companyName"));
Avatar of dkim18

ASKER

Did you mean like this in InitPage.jsp?

<%!
      private String companyName;
      public void jspInit() {
            ServletConfig config = getServletConfig();
            companyName = config.getInitParameter("companyName");
            application.setAttribute(config.getInitParameter("companyName"));
      }
%>

It didn't work by the way...
I always use a servlet to do this sort of thing.

If you are accessing the company name from anoither jsp page, then you'll do to have whatever does the copy loaded at startup.