Link to home
Start Free TrialLog in
Avatar of dkim18
dkim18

asked on

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,
Avatar of koppcha
koppcha
Flag of United States of America image

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>
Avatar of dkim18
dkim18

ASKER

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>  





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 %>
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 %>'
<%
}
%>
>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>


Avatar of dkim18

ASKER

>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.
Avatar of dkim18

ASKER

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");
      }
%>
++++++++++++++
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)

Avatar of dkim18

ASKER

>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
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>


Avatar of dkim18

ASKER

It didn't work. It just displayed directory. Furthermore, http://localhost:8080/~dkim18/hw8/InitPage.jsp displayed company name as "null."
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>

Avatar of dkim18

ASKER

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.
there shouldn't  be '~' what ever you are getting in your URL in that place keep the exact one
post your JSP and whole web.xml if you can after doing the after changes
Avatar of dkim18

ASKER

<?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");
      }
%>

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>

Avatar of dkim18

ASKER

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>
do not use declaration  <%! like this use scriptlets <% .I corrected this error in my previous post
Avatar of dkim18

ASKER

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;

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>
Avatar of dkim18

ASKER

I didn't get error, but the company name was null again.
ok you got to restart the server or redploy the application and let me know
I just tried for you and it worked fine
Avatar of dkim18

ASKER

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>
Just noticed in my earlier post there is typo did you check that ?
><%
      String comapnyName = getServletConfig().getInitParameter("companyName");
%>

check the String.. it is companyName
Avatar of dkim18

ASKER

yes, i corrected it.
ASKER CERTIFIED SOLUTION
Avatar of koppcha
koppcha
Flag of United States of America 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
My appserver is weblogic so the port is different .it is 7001 for weblogic and 8080 for tomcat
Is that working ?
Avatar of dkim18

ASKER

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>
Great ..Keep up the good work.
Thanks For the grade 'A'