?
Solved

reading parameter out of the servlet context

Posted on 2005-04-03
6
Medium Priority
?
332 Views
Last Modified: 2010-04-01
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,

0
Comment
Question by:dkim18
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 92

Accepted Solution

by:
objects earned 600 total points
ID: 13693855
easier to just use:

 <LI> Company Name: <%= application.getAttribute("ABC") %>

or if you want a companyName variable use:

<%
String companyName = application.getAttribute("ABC");
%>
0
 
LVL 92

Expert Comment

by:objects
ID: 13694024
I generally use a servlet to copy the init parameter into the application context (on startup).
0
 

Author Comment

by:dkim18
ID: 13694697
I just got null for company name.

<BODY>
   <H2>Init Parameter</H2>
   <LI> Company Name: <%= application.getAttribute("ABC") %>
</BODY>
</HTML>
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 92

Expert Comment

by:objects
ID: 13694716
you still need to copy the init param into the servlet context at startup.

application.setAttribute(config.getInitParameter("companyName"));
0
 

Author Comment

by:dkim18
ID: 13694852
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...
0
 
LVL 92

Expert Comment

by:objects
ID: 13694878
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.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Learn how to use the free Acronis True Image app to easily transfer data between iPhones and Android phones.
The top devops trends for 2017 are focused on improved deployment frequency, decreased lead time for change and decreased MTTR.
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question