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,
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,
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-patter n>
</servlet-mapping>
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
<jsp-file>/Naming.jsp</jsp
</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
<url-pattern>/Naming.jsp</
</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>readInitPara mJSP</serv let-name>
<jsp-file>/web/readInitPar amJSP.jsp< /jsp-file>
<init-param>
<param-name>name</param-na me>
<param-value>EE</param-val ue>
</init-param>
<init-param>
<param-name>Number</param- name>
<param-value>1234</param-v alue>
</init-param>
</servlet>
In JSP you can access it by
<%
String name = getServletConfig().getInit Parameter( "name");
String Number = getServletConfig().getInit Parameter( "Number");
%>
Name: <%= name %>
Number: <%= Number %>
like this
in web.xml
<servlet>
<servlet-name>readInitPara
<jsp-file>/web/readInitPar
<init-param>
<param-name>name</param-na
<param-value>EE</param-val
</init-param>
<init-param>
<param-name>Number</param-
<param-value>1234</param-v
</init-param>
</servlet>
In JSP you can access it by
<%
String name = getServletConfig().getInit
String Number = getServletConfig().getInit
%>
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().getInit ParameterN ames();
// Print the init parameter names and values to the console
while (params.hasMoreElements()) {
String name = (String) params.nextElement();
String value=(String)getServletCo nfig(getIn itParamete r(name));
%>
Parameter Pair: '<%= name %>' = '<%= value %>'
<%
}
%>
see this sample code
<%
Enumeration params = getServletConfig().getInit
// Print the init parameter names and values to the console
while (params.hasMoreElements())
String name = (String) params.nextElement();
String value=(String)getServletCo
%>
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>somefileServ let</servl et-name>
<jsp-file>/someDirectory/s ome-file.j sp</jsp-fi le>
</servlet>
<servlet-mapping>
<servlet-name>somefileServ let</servl et-name>
<url-pattern>/someDirector y</url-pat tern>
</servlet-mapping>
you have to do this in your web.xml
<servlet>
<servlet-name>somefileServ
<jsp-file>/someDirectory/s
</servlet>
<servlet-mapping>
<servlet-name>somefileServ
<url-pattern>/someDirector
</servlet-mapping>
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.
when I requested http://host/~yourID/someDir/, just Directory Listing For someDir showed up, not the contents of JSP page.
ASKER
This is what I did.
<servlet>
<servlet-name>InitPage</se rvlet-name >
<jsp-file>/hw8/InitPage.js p</jsp-fil e>
<init-param>
<param-name>companyName</p aram-name>
<param-value>ABC</param-va lue>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>InitPage</se rvlet-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("c ompanyName ");
}
%>
++++++++++++++
<servlet>
<servlet-name>InitPage</se
<jsp-file>/hw8/InitPage.js
<init-param>
<param-name>companyName</p
<param-value>ABC</param-va
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>InitPage</se
<url-pattern>/hw8/InitPage
</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("c
}
%>
++++++++++++++
This is what you said you have right
<servlet>
<servlet-name>InitPage</se rvlet-name >
<jsp-file>/hw8/InitPage.js p</jsp-fil e>
<init-param>
<param-name>companyName</p aram-name>
<param-value>ABC</param-va lue>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>InitPage</se rvlet-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)
<servlet>
<servlet-name>InitPage</se
<jsp-file>/hw8/InitPage.js
<init-param>
<param-name>companyName</p
<param-value>ABC</param-va
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>InitPage</se
<url-pattern>/hw8/InitPage
</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)
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\we bapps\~dki m18\hw8
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\we
OK then in your web.xml
Change from
><servlet-mapping>
<servlet-name>InitPage</se rvlet-name >
<url-pattern>/hw8/InitPage .jsp</url- pattern>
</servlet-mapping>
To
<servlet-mapping>
<servlet-name>InitPage</se rvlet-name >
<url-pattern>/hw8</url-pat tern>
</servlet-mapping>
Change from
><servlet-mapping>
<servlet-name>InitPage</se
<url-pattern>/hw8/InitPage
</servlet-mapping>
To
<servlet-mapping>
<servlet-name>InitPage</se
<url-pattern>/hw8</url-pat
</servlet-mapping>
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</se rvlet-name >
<url-pattern>/~dkim18/hw8< /url-patte rn>
</servlet-mapping>
<servlet>
<servlet-name>InitPage</se rvlet-name >
<jsp-file>/~dkim18/hw8/Ini tPage.jsp< /jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>InitPage</se
<url-pattern>/~dkim18/hw8<
</servlet-mapping>
<servlet>
<servlet-name>InitPage</se
<jsp-file>/~dkim18/hw8/Ini
</servlet>
ASKER
you meant this?
<servlet>
<servlet-name>InitPage</se rvlet-name >
<jsp-file>/~dkim18/hw8/Ini tPage.jsp< /jsp-file>
<init-param>
<param-name>companyName</p aram-name>
<param-value>ABC</param-va lue>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>InitPage</se rvlet-name >
<url-pattern>/~dkim18/hw8< /url-patte rn>
</servlet-mapping>
didn't work either.
<servlet>
<servlet-name>InitPage</se
<jsp-file>/~dkim18/hw8/Ini
<init-param>
<param-name>companyName</p
<param-value>ABC</param-va
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>InitPage</se
<url-pattern>/~dkim18/hw8<
</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
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</se rvlet-name >
<jsp-file>/~dkim18/hw8/Ini tPage.jsp< /jsp-file>
<init-param>
<param-name>companyName</p aram-name>
<param-value>ABC</param-va lue>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>InitPage</se rvlet-name >
<url-pattern>/~dkim18/hw8< /url-patte rn>
</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("c ompanyName ");
}
%>
<!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</se
<jsp-file>/~dkim18/hw8/Ini
<init-param>
<param-name>companyName</p
<param-value>ABC</param-va
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>InitPage</se
<url-pattern>/~dkim18/hw8<
</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("c
}
%>
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("c ompanyName ");
%>
<LI> Company Name: <%= companyName %>
</BODY>
</HTML>
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("c
%>
<LI> Company Name: <%= companyName %>
</BODY>
</HTML>
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/wor k/Catalina /localhost /~dkim18/o rg/apache/ jsp/hw8/In itPage_jsp .java:13: expected
companyName = config.getInitParameter("c ompanyName ");
^
1 error
org.apache.jasper.compiler .DefaultEr rorHandler .javacErro r(DefaultE rrorHandle r.java:127 )
org.apache.jasper.compiler .ErrorDisp atcher.jav acError(Er rorDispatc her.java:3 51)
org.apache.jasper.compiler .Compiler. generateCl ass(Compil er.java:41 5)
org.apache.jasper.compiler .Compiler. compile(Co mpiler.jav a:458)
org.apache.jasper.compiler .Compiler. compile(Co mpiler.jav a:439)
org.apache.jasper.JspCompi lationCont ext.compil e(JspCompi lationCont ext.java:5 52)
org.apache.jasper.servlet. JspServlet Wrapper.se rvice(JspS ervletWrap per.java:2 91)
org.apache.jasper.servlet. JspServlet .serviceJs pFile(JspS ervlet.jav a:301)
org.apache.jasper.servlet. JspServlet .service(J spServlet. java:248)
javax.servlet.http.HttpSer vlet.servi ce(HttpSer vlet.java: 856)
sun.reflect.GeneratedMetho dAccessor8 6.invoke(U nknown Source)
sun.reflect.DelegatingMeth odAccessor Impl.invok e(Delegati ngMethodAc cessorImpl .java:25)
java.lang.reflect.Method.i nvoke(Meth od.java:58 5)
org.apache.catalina.securi ty.Securit yUtil$1.ru n(Security Util.java: 284)
java.security.AccessContro ller.doPri vileged(Na tive Method)
javax.security.auth.Subjec t.doAsPriv ileged(Sub ject.java: 517)
org.apache.catalina.securi ty.Securit yUtil.exec ute(Securi tyUtil.jav a:306)
org.apache.catalina.securi ty.Securit yUtil.doAs Privilege( SecurityUt il.java:20 0)
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("c ompanyName ");
%>
<LI> Company Name: <%= companyName %>
</BODY>
</HTML>
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-
companyName = config.getInitParameter("c
^
1 error
org.apache.jasper.compiler
org.apache.jasper.compiler
org.apache.jasper.compiler
org.apache.jasper.compiler
org.apache.jasper.compiler
org.apache.jasper.JspCompi
org.apache.jasper.servlet.
org.apache.jasper.servlet.
org.apache.jasper.servlet.
javax.servlet.http.HttpSer
sun.reflect.GeneratedMetho
sun.reflect.DelegatingMeth
java.lang.reflect.Method.i
org.apache.catalina.securi
java.security.AccessContro
javax.security.auth.Subjec
org.apache.catalina.securi
org.apache.catalina.securi
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("c
%>
<LI> Company Name: <%= companyName %>
</BODY>
</HTML>
do not use declaration <%! like this use scriptlets <% .I corrected this error in my previous post
ASKER
I got this error with scriptleets and that was why I changed.
org.apache.jasper.JasperEx ception: 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/wor k/Catalina /localhost /~dkim18/o rg/apache/ jsp/hw8/In itPage_jsp .java:42: illegal start of expression
private String companyName;
org.apache.jasper.JasperEx
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-
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().getInit Parameter( "companyNa me");
%>
<LI> Company Name: <%= companyName %>
</BODY>
</HTML>
<!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().getInit
%>
<LI> Company Name: <%= companyName %>
</BODY>
</HTML>
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
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</se rvlet-name >
<jsp-file>/hw8/InitPage.js p</jsp-fil e>
<init-param>
<param-name>companyName</p aram-name>
<param-value>ABC</param-va lue>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>InitPage</se rvlet-name >
<url-pattern>/hw8/</url-pa ttern>
</servlet-mapping>
</web-app>
<?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</se
<jsp-file>/hw8/InitPage.js
<init-param>
<param-name>companyName</p
<param-value>ABC</param-va
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>InitPage</se
<url-pattern>/hw8/</url-pa
</servlet-mapping>
</web-app>
Just noticed in my earlier post there is typo did you check that ?
><%
String comapnyName = getServletConfig().getInit Parameter( "companyNa me");
%>
check the String.. it is companyName
><%
String comapnyName = getServletConfig().getInit
%>
check the String.. it is companyName
ASKER
yes, i corrected it.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
My appserver is weblogic so the port is different .it is 7001 for weblogic and 8080 for tomcat
Is that working ?
ASKER
This is what I did and working.
<servlet>
<servlet-name>InitPage</se rvlet-name >
<jsp-file>/hw8/InitPage.js p</jsp-fil e>
<init-param>
<param-name>companyName</p aram-name>
<param-value>ABC</param-va lue>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>InitPage</se rvlet-name >
<url-pattern>/hw8/*</url-p attern>
</servlet-mapping>
<servlet>
<servlet-name>InitPage</se
<jsp-file>/hw8/InitPage.js
<init-param>
<param-name>companyName</p
<param-value>ABC</param-va
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>InitPage</se
<url-pattern>/hw8/*</url-p
</servlet-mapping>
Great ..Keep up the good work.
Thanks For the grade 'A'
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</u
</servlet-mapping>