Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2006

Tip: Looking for answers? Try searching our database.

Why is Java lying?

Thread view: 
laredotornado@zipmail.com - 28 Nov 2006 16:50 GMT
Hi,

I'm trying to compile a very simple JSP page on Tomcat 5.5, JDK 1.5

<%@ page import="java.util.*" %>
<%
       Object v = new String("b");
       session.setAttribute("a", v);

       Enumeration e = session.getAttributeNames();
       while (e.hasMoreElements()) {
               String attr = (String) e.nextElement();
               String v1 = session.getAttribute(attr);
               String v2 = session.getValue(attr);
               out.println("attr: " + attr + " v1:" + v1 + " v2:" +
v2);
       }   // while
%>

but I'm getting this compile error:

An error occurred at line: 2 in the jsp file: /session_vars.jsp
Generated servlet error:
Type mismatch: cannot convert from Object to String

An error occurred at line: 2 in the jsp file: /session_vars.jsp
Generated servlet error:
Type mismatch: cannot convert from Object to String

    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:512)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

First off, the line number is incorrect (line 2 is "<%") but more
importantly, I thought String extended Object.

Any thoughts? - Dave
Robert Klemme - 28 Nov 2006 16:59 GMT
> Hi,
>
[quoted text clipped - 33 lines]
> First off, the line number is incorrect (line 2 is "<%") but more
> importantly, I thought String extended Object.

Yes, but you are casting the other way - and in some places you do not
even have the cast (String v2 = session.getValue(attr);).

    robert
Thomas Fritsch - 28 Nov 2006 17:12 GMT
> I'm trying to compile a very simple JSP page on Tomcat 5.5, JDK 1.5
>
[quoted text clipped - 8 lines]
>        String v1 = session.getAttribute(attr);
>        String v2 = session.getValue(attr);
I suspect the JSP compiler complains about the line above, because
HttpSession.getValue is declared as
    public Object getValue(String name)
but not as
    public String getValue(String name)
See
<http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/http/HttpSession.html>.

>        out.println("attr: " + attr + " v1:" + v1 + " v2:" + v2);
>    }   // while
[quoted text clipped - 18 lines]
> First off, the line number is incorrect (line 2 is "<%") but more
> importantly, I thought String extended Object.
Don't ask me why the compiler says "line: 2", although it actually seems
to be line 10.

> Any thoughts? - Dave

Signature

Thomas

dantarion@gmail.com - 28 Nov 2006 20:20 GMT
You have to cast from Object to String.

java does not know that that "Object" is a "String".
it does know that "String"s are "Object"s.

You have to cast whenever you go from general->specific

String v2 = (String) session.getValue(attr);

On Nov 28, 12:12 pm, Thomas Fritsch <i.dont.like.s...@invalid.com>
wrote:
> laredotorn...@zipmail.com wrote:
> > I'm trying to compile a very simple JSP page on Tomcat 5.5, JDK 1.5
[quoted text clipped - 42 lines]
> > Any thoughts? - Dave--
> Thomas
Karl Uppiano - 28 Nov 2006 21:32 GMT
> java does not know that that "Object" is a "String".
> it does know that "String"s are "Object"s.

I know this doesn't answer the basic question, but if it is just the string
you want,

   String attr = e.nextElement().toString();

should work without casting.
Lew - 02 Dec 2006 07:23 GMT
laredotornado@zipmail.com wrote:
>> I'm trying to compile a very simple JSP page on Tomcat 5.5, JDK 1.5
>>
[quoted text clipped - 17 lines]
>> Generated servlet error:
>> Type mismatch: cannot convert from Object to String

>> First off, the line number is incorrect (line 2 is "<%")

> Don't ask me why the compiler says "line: 2", although it actually seems
> to be line 10.

Perhaps in a JSP the entire content from <% to %> is considered one line?

NttpSession.getValue() is deprecated.

- Lew
jcsnippets.atspace.com - 05 Dec 2006 13:41 GMT
<snip>
>> Don't ask me why the compiler says "line: 2", although it actually
>> seems to be line 10.
>
> Perhaps in a JSP the entire content from <% to %> is considered one
> line?

A JSP cannot be compiled, since it is not Java code. Your web/app server
will create a .java file from the JSP file, which in turn will be compiled
into a servlet.

Look up the .java file for your JSP file, and you will see that those line
numbers actually are correct - they don't refer to the JSP, but to the
accompanying .java file.

Best regards,

JayCee
Signature

http://jcsnippets.atspace.com/
a collection of source code, tips and tricks

Patricia Shanahan - 05 Dec 2006 14:58 GMT
> <snip>
>>> Don't ask me why the compiler says "line: 2", although it actually
[quoted text clipped - 9 lines]
> numbers actually are correct - they don't refer to the JSP, but to the
> accompanying .java file.

C has a system for specifying user-meaningful file names and line
numbers in automatically generated code. Maybe Java should have an
annotation to do the same job.

Patricia


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.