Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

reading parameter out of the servlet context

Posted on 2005-04-03
6
Medium Priority
?
333 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
  • 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

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!

Question has a verified solution.

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

The article explains the process to deploy a Self-Service password reset portal I developed a few years ago. Hopefully, it will prove useful to someone.  Any comments, bug reports etc. are welcome...
"Day by day nothing changes but when u look back, everything is different". That quote precisely describes today’s digital era. For example, you may not have noticed the change, but Voice Search is now all around us.
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
How can you see what you are working on when you want to see it while you to save a copy? Add a "Save As" icon to the Quick Access Toolbar, or QAT. That way, when you save a copy of a query, form, report, or other object you are modifying, you…
Suggested Courses

572 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