?
Solved

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

Posted on 2005-04-02
33
Medium Priority
?
190 Views
Last Modified: 2010-04-01
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,
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
  • 20
  • 13
33 Comments
 
LVL 8

Expert Comment

by:koppcha
ID: 13690016
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
 

Author Comment

by:dkim18
ID: 13693388
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
 
LVL 8

Expert Comment

by:koppcha
ID: 13694073
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
Technology Partners: 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 8

Expert Comment

by:koppcha
ID: 13694111
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
 
LVL 8

Expert Comment

by:koppcha
ID: 13694137
>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
 

Author Comment

by:dkim18
ID: 13694573
>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
 

Author Comment

by:dkim18
ID: 13694582
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
 
LVL 8

Expert Comment

by:koppcha
ID: 13694627
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
 

Author Comment

by:dkim18
ID: 13694645
>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
 
LVL 8

Expert Comment

by:koppcha
ID: 13694665
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
 

Author Comment

by:dkim18
ID: 13694687
It didn't work. It just displayed directory. Furthermore, http://localhost:8080/~dkim18/hw8/InitPage.jsp displayed company name as "null."
0
 
LVL 8

Expert Comment

by:koppcha
ID: 13694806
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
 

Author Comment

by:dkim18
ID: 13694828
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
 
LVL 8

Expert Comment

by:koppcha
ID: 13696881
there shouldn't  be '~' what ever you are getting in your URL in that place keep the exact one
0
 
LVL 8

Expert Comment

by:koppcha
ID: 13697111
post your JSP and whole web.xml if you can after doing the after changes
0
 

Author Comment

by:dkim18
ID: 13697479
<?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
 
LVL 8

Expert Comment

by:koppcha
ID: 13697558
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
 

Author Comment

by:dkim18
ID: 13697873
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
 
LVL 8

Expert Comment

by:koppcha
ID: 13697937
do not use declaration  <%! like this use scriptlets <% .I corrected this error in my previous post
0
 

Author Comment

by:dkim18
ID: 13697977
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
 
LVL 8

Expert Comment

by:koppcha
ID: 13698046
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
 

Author Comment

by:dkim18
ID: 13698102
I didn't get error, but the company name was null again.
0
 
LVL 8

Expert Comment

by:koppcha
ID: 13698182
ok you got to restart the server or redploy the application and let me know
0
 
LVL 8

Expert Comment

by:koppcha
ID: 13698186
I just tried for you and it worked fine
0
 

Author Comment

by:dkim18
ID: 13698341
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
 
LVL 8

Expert Comment

by:koppcha
ID: 13698436
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
 

Author Comment

by:dkim18
ID: 13698499
yes, i corrected it.
0
 
LVL 8

Accepted Solution

by:
koppcha earned 600 total points
ID: 13698608
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
 
LVL 8

Expert Comment

by:koppcha
ID: 13698614
My appserver is weblogic so the port is different .it is 7001 for weblogic and 8080 for tomcat
0
 
LVL 8

Expert Comment

by:koppcha
ID: 13700840
Is that working ?
0
 

Author Comment

by:dkim18
ID: 13711890
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
 
LVL 8

Expert Comment

by:koppcha
ID: 13712162
Great ..Keep up the good work.
0
 
LVL 8

Expert Comment

by:koppcha
ID: 13712167
Thanks For the grade 'A'
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

The well known Cerber ransomware continues to spread this summer through spear phishing email campaigns targeting enterprises. Learn how it easily bypasses traditional defenses - and what you can do to protect your data.
Hey fellow admins! This time, I have a little fairy tale for you. As many tales do, it starts boring and then gets pretty gory. I hope you like it. TL;DR: It is about an important security matter, you should read it if you run or administer Windows …
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses
Course of the Month13 days, 10 hours left to enroll

800 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