JSP page that reads the company name from a page-specific initialization parameter

hi!

I am trying to make a JSP page that reads the company name from a page-specific initialization parameter and then uses it in the page. Since I am doing this in school server, I already have a web application (URL prefix:~myID; top-level directory: your public_html dir). So, I can’t make any other web application and it has to be part of my existing web application. Is that means that my web.xml has to include whatever it already had plus the parts for this assignment? How do I start this?

Thanks,
dkim18Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
koppchaConnect With a Mentor Commented:
OK...I do not see any problem but i will paste what i have .It worked for me .

JSP
<%@ page language="java" import="java.util.*" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  </head>
 
  <body>
    <%
      String company = getServletConfig().getInitParameter("companyName");
      %>
      COMPANY: <%= company %>
  </body>
</html>

This is my web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<servlet>
       <servlet-name> jspServlet</servlet-name>
       <jsp-file>/HelloWorld.jsp</jsp-file>
         <init-param>
           <param-name>companyName</param-name>
           <param-value>BankOfAmerica</param-value>
         </init-param>
  </servlet>
        <servlet-mapping>
               <servlet-name>jspServlet</servlet-name>
               <url-pattern>/</url-pattern>
        </servlet-mapping>
</web-app>

My JSP's regular URL is
http://localhost:7001/HelloWorld/HelloWorld.jsp
but i am able to use
http://localhost:7001/HelloWorld
to access the same JSP
because of my changes in web.xml

0
 
koppchaCommented:
If you want to use some other application...You can keep the JSP page along with other JSPs in the application.If you are not using servlets or any other externel classes you do not have to worry about anything else.Just keep the JSP withother JSPs in the existing application.
        In case you have a servlet then you have to change the existing applications web.xml to enter your servlets specific tags like <servlet>
             <servlet-name>Servlet Name</servlet-name>
             <servlet-class>Fuly qualifies servlet class</servlet-class>
             </servlet>
             <servlet-mapping>
             <servlet-name>Servlet Name</servlet-name>
                       <url-pattern>/MyServlet</url-pattern>
      </servlet-mapping>
0
 
dkim18Author Commented:
thanks for your comment.

Since the questin says "make a JSP page that reads the company name from a page-specific initialization parameter and then uses it in the page", don't I need to use JSP?

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> HW#8 - #1</TITLE>

</HEAD>
<BODY>

<H1> URI: <%= request.getRequestURI() %> </H1>
</BODY>
</HTML>
++++++++
<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE web-app PUBLIC
   "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
   "http://java.sun.com/dtd/web-app_2-3.dtd">

<web-app>

  <servlet>
       <servlet-name>ABC</servlet-name>
     <jsp-file>/Naming.jsp</jsp-file>
  </servlet>
...
...
++++++++++++
Now, after keep reading question, I am a bit confused by "reads the company name from a page-specific initialization parameter.." what does exactly this mean?

Lets assume that the URL of above is http://host/~yourID/someDirectory/some-file.jsp. how do I make sure that the JSP page of #1 is displayed if the user requests http://host/~yourID/someDir/, with no filename specified. I used the following, but didn't work.(since this is additional question, I increased the point.)

   <servlet-mapping>
     <servlet-name>ABC</servlet-name>
     <url-pattern>/Naming.jsp</url-pattern>
  </servlet-mapping>  





0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
koppchaCommented:
you have to specify the page specific parameters in web.xml and you can access them in the JSP
like this
in web.xml
<servlet>
    <servlet-name>readInitParamJSP</servlet-name>
    <jsp-file>/web/readInitParamJSP.jsp</jsp-file>
    <init-param>
      <param-name>name</param-name>
      <param-value>EE</param-value>
    </init-param>
    <init-param>
      <param-name>Number</param-name>
      <param-value>1234</param-value>
    </init-param>
  </servlet>

In JSP you can access it by
<%
String name   = getServletConfig().getInitParameter("name");
String Number   = getServletConfig().getInitParameter("Number");
%>
Name: <%= name %>
Number: <%= Number %>
0
 
koppchaCommented:
you can also get all the init parameters and use Enumeration to pass through
see this sample code

<%
   Enumeration params = getServletConfig().getInitParameterNames();

   // Print the init parameter names and values to the console
   while (params.hasMoreElements()) {
       String name = (String) params.nextElement();
       String value=(String)getServletConfig(getInitParameter(name));
       
   %>
    Parameter Pair: '<%= name %>' = '<%= value %>'
<%
}
%>
0
 
koppchaCommented:
>Lets assume that the URL of above is http://host/~yourID/someDirectory/some-file.jsp. how do I make sure that the >JSP page of #1 is displayed if the user requests http://host/~yourID/someDir/, with no filename specified


you have to do this in your web.xml

<servlet>
  <servlet-name>somefileServlet</servlet-name>
  <jsp-file>/someDirectory/some-file.jsp</jsp-file>
</servlet>
<servlet-mapping>
  <servlet-name>somefileServlet</servlet-name>
  <url-pattern>/someDirectory</url-pattern>
</servlet-mapping>


0
 
dkim18Author Commented:
>Lets assume that the URL of above is http://host/~yourID/someDirectory/some-file.jsp. how do I make sure that the >JSP page of #1 is displayed if the user requests http://host/~yourID/someDir/, with no filename specified

when I requested http://host/~yourID/someDir/, just Directory Listing For someDir showed up, not the contents of JSP page.
0
 
dkim18Author Commented:
This is what I did.

<servlet>
       <servlet-name>InitPage</servlet-name>
     <jsp-file>/hw8/InitPage.jsp</jsp-file>

       <init-param>
         <param-name>companyName</param-name>
         <param-value>ABC</param-value>
       </init-param>
  </servlet>

  <servlet-mapping>
    <servlet-name>InitPage</servlet-name>
    <url-pattern>/hw8/InitPage.jsp</url-pattern>
  </servlet-mapping>
++++++++++++++
<BODY>
   <H2>Init Parameter</H2>
   <LI> Company Name: <%= companyName %>
</BODY>
</HTML>
<%!
      private String companyName;
      public void jspInit() {
            ServletConfig config = getServletConfig();
            companyName = config.getInitParameter("companyName");
      }
%>
++++++++++++++
0
 
koppchaCommented:
This is what you said you have right
<servlet>
      <servlet-name>InitPage</servlet-name>
     <jsp-file>/hw8/InitPage.jsp</jsp-file>

      <init-param>
        <param-name>companyName</param-name>
        <param-value>ABC</param-value>
      </init-param>
  </servlet>

  <servlet-mapping>
    <servlet-name>InitPage</servlet-name>
    <url-pattern>/hw8/InitPage.jsp</url-pattern>
  </servlet-mapping>

Now tell me this
1>Which url you want to call to the JSP page(not the refular URL for the JSP but the short form that you are looking for)
2>Where is the JSP that you want to call(give me the full path)

0
 
dkim18Author Commented:
>Which url you want to call to the JSP page(not the refular URL for the JSP but the short form that you are looking for)
http://localhost:8080/~dkim18/hw8/
( I want JSP page is displayed if a user requests http://host/~yourID/someDir/, with no filename specified.)

>Where is the JSP that you want to call(give me the full path)

C:\jakarta-tomcat-5.5.4\webapps\~dkim18\hw8
0
 
koppchaCommented:
OK then in your web.xml

Change from
><servlet-mapping>
    <servlet-name>InitPage</servlet-name>
    <url-pattern>/hw8/InitPage.jsp</url-pattern>
  </servlet-mapping>

To
<servlet-mapping>
    <servlet-name>InitPage</servlet-name>
    <url-pattern>/hw8</url-pattern>
  </servlet-mapping>


0
 
dkim18Author Commented:
It didn't work. It just displayed directory. Furthermore, http://localhost:8080/~dkim18/hw8/InitPage.jsp displayed company name as "null."
0
 
koppchaCommented:
ok try this
<servlet-mapping>
    <servlet-name>InitPage</servlet-name>
    <url-pattern>/~dkim18/hw8</url-pattern>
  </servlet-mapping>
<servlet>
      <servlet-name>InitPage</servlet-name>
     <jsp-file>/~dkim18/hw8/InitPage.jsp</jsp-file>
</servlet>

0
 
dkim18Author Commented:
you meant this?

  <servlet>
     <servlet-name>InitPage</servlet-name>
     <jsp-file>/~dkim18/hw8/InitPage.jsp</jsp-file>

       <init-param>
         <param-name>companyName</param-name>
         <param-value>ABC</param-value>
       </init-param>
  </servlet>

  <servlet-mapping>
    <servlet-name>InitPage</servlet-name>
    <url-pattern>/~dkim18/hw8</url-pattern>
  </servlet-mapping>

didn't work either.
0
 
koppchaCommented:
there shouldn't  be '~' what ever you are getting in your URL in that place keep the exact one
0
 
koppchaCommented:
post your JSP and whole web.xml if you can after doing the after changes
0
 
dkim18Author Commented:
<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE web-app PUBLIC
   "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
   "http://java.sun.com/dtd/web-app_2-3.dtd">
   
<web-app>

<!--hw8 #1 -->

  <servlet>
     <servlet-name>InitPage</servlet-name>
     <jsp-file>/~dkim18/hw8/InitPage.jsp</jsp-file>

       <init-param>
         <param-name>companyName</param-name>
         <param-value>ABC</param-value>
       </init-param>
  </servlet>

  <servlet-mapping>
    <servlet-name>InitPage</servlet-name>
    <url-pattern>/~dkim18/hw8</url-pattern>
  </servlet-mapping>

</web-app>
++++++++++++
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> HW#8 - #1</TITLE>

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

0
 
koppchaCommented:
1> You are trying to print company name before even you get it so it is null.Just try the below JSP
2>Do not keep '~' in the web.xml for <url-pattern> and <jsp-file> keep what ever is the exact path.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> HW#8 - #1</TITLE>

</HEAD>
<BODY>
   <H2>Init Parameter</H2>
<%
     private String companyName;
      ServletConfig config = getServletConfig();
      companyName = config.getInitParameter("companyName");
     
%>
   <LI> Company Name: <%= companyName %>
</BODY>
</HTML>

0
 
dkim18Author Commented:
I got  this error.

An error occurred at line: 9 in the jsp file: /hw8/InitPage.jsp

Generated servlet error:
    [javac] Compiling 1 source file

/usr/local/jakarta-tomcat-5.0.16/work/Catalina/localhost/~dkim18/org/apache/jsp/hw8/InitPage_jsp.java:13:  expected
     companyName = config.getInitParameter("companyName");
                 ^
1 error


      org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:127)
      org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:351)
      org.apache.jasper.compiler.Compiler.generateClass(Compiler.java:415)
      org.apache.jasper.compiler.Compiler.compile(Compiler.java:458)
      org.apache.jasper.compiler.Compiler.compile(Compiler.java:439)
      org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:552)
      org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:291)
      org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:301)
      org.apache.jasper.servlet.JspServlet.service(JspServlet.java:248)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
      sun.reflect.GeneratedMethodAccessor86.invoke(Unknown Source)
      sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      java.lang.reflect.Method.invoke(Method.java:585)
      org.apache.catalina.security.SecurityUtil$1.run(SecurityUtil.java:284)
      java.security.AccessController.doPrivileged(Native Method)
      javax.security.auth.Subject.doAsPrivileged(Subject.java:517)
      org.apache.catalina.security.SecurityUtil.execute(SecurityUtil.java:306)
      org.apache.catalina.security.SecurityUtil.doAsPrivilege(SecurityUtil.java:200)

This is what I did.
---------------------------------

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> HW#8 - #1</TITLE>

</HEAD>
<BODY>
   <H2>Init Parameter</H2>
<%!
     private String companyName;
     ServletConfig config = getServletConfig();
     companyName = config.getInitParameter("companyName");
     
%>
   <LI> Company Name: <%= companyName %>
</BODY>
</HTML>
0
 
koppchaCommented:
do not use declaration  <%! like this use scriptlets <% .I corrected this error in my previous post
0
 
dkim18Author Commented:
I got this error with scriptleets and that was why I changed.

org.apache.jasper.JasperException: Unable to compile class for JSP

An error occurred at line: 9 in the jsp file: /hw8/InitPage.jsp

Generated servlet error:
    [javac] Compiling 1 source file

/usr/local/jakarta-tomcat-5.0.16/work/Catalina/localhost/~dkim18/org/apache/jsp/hw8/InitPage_jsp.java:42: illegal start of expression
     private String companyName;

0
 
koppchaCommented:
ok just use this JSP as it is and let meknow what is the error

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> HW#8 - #1</TITLE>

</HEAD>
<BODY>
   <H2>Init Parameter</H2>
<%
      String comapnyName = getServletConfig().getInitParameter("companyName");
%>
   <LI> Company Name: <%= companyName %>
</BODY>
</HTML>
0
 
dkim18Author Commented:
I didn't get error, but the company name was null again.
0
 
koppchaCommented:
ok you got to restart the server or redploy the application and let me know
0
 
koppchaCommented:
I just tried for you and it worked fine
0
 
dkim18Author Commented:
This is web.xml and I am still having null and I tried both.

<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE web-app PUBLIC
   "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
   "http://java.sun.com/dtd/web-app_2-3.dtd">
   
<web-app>

<!--hw8 #1 -->

  <servlet>
     <servlet-name>InitPage</servlet-name>
     <jsp-file>/hw8/InitPage.jsp</jsp-file>

       <init-param>
         <param-name>companyName</param-name>
         <param-value>ABC</param-value>
       </init-param>
  </servlet>

  <servlet-mapping>
    <servlet-name>InitPage</servlet-name>
    <url-pattern>/hw8/</url-pattern>
  </servlet-mapping>

</web-app>
0
 
koppchaCommented:
Just noticed in my earlier post there is typo did you check that ?
><%
      String comapnyName = getServletConfig().getInitParameter("companyName");
%>

check the String.. it is companyName
0
 
dkim18Author Commented:
yes, i corrected it.
0
 
koppchaCommented:
My appserver is weblogic so the port is different .it is 7001 for weblogic and 8080 for tomcat
0
 
koppchaCommented:
Is that working ?
0
 
dkim18Author Commented:
This is what I did and working.
 
<servlet>
     <servlet-name>InitPage</servlet-name>
     <jsp-file>/hw8/InitPage.jsp</jsp-file>
      

       <init-param>
         <param-name>companyName</param-name>
         <param-value>ABC</param-value>
       </init-param>
  </servlet>

  <servlet-mapping>
    <servlet-name>InitPage</servlet-name>
    <url-pattern>/hw8/*</url-pattern>
  </servlet-mapping>
0
 
koppchaCommented:
Great ..Keep up the good work.
0
 
koppchaCommented:
Thanks For the grade 'A'
0
All Courses

From novice to tech pro — start learning today.