Why is Java lying?

L

laredotornado

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
 
R

Robert Klemme

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.

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
 
T

Thomas Fritsch

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);
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
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.
Don't ask me why the compiler says "line: 2", although it actually seems
to be line 10.
 
D

dantarion

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);
 
K

Karl Uppiano

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

Lew

Thomas said:
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
 
J

jcsnippets.atspace.com

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
 
P

Patricia Shanahan

jcsnippets.atspace.com said:
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.

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,197
Latest member
Sean29G025

Latest Threads

Top