Reputation: 153
I have a jsp file adminLogin.jsp where i check the session attributes passed on from index.jsp and from logout.jsp :
<%
String s=(String)session.getAttribute("wrongP");
if(s.equals('0'))
{out.println("Wrong Details or you logged out Please Login Again <br>");
session.invalidate();
}
%>
This is login
form
<br>Please login<br>
<form method="GET" action="login1">
<br>. Id:<input type="text" name="id"><br>Password:<input
type="password" name="pass"><br> <input type="submit"
value="Submit">
</form>
</div>
The error i am getting is:
HTTP Status 500 - An exception occurred processing JSP page /adminLogin.jsp at line 12
type Exception report
message An exception occurred processing JSP page /adminLogin.jsp at line 12 description The server encountered an internal error that prevented it from fulfilling this request. exception org.apache.jasper.JasperException: An exception occurred processing JSP page /adminLogin.jsp at line 12 9: <body> 10: <div align="center"> 11: <% 12: String s=(String)session.getAttribute("wrongP"); 13: if(s.equals('0')) 14: {out.println("Wrong Details or you logged out Please Login Again <br>"); 15: session.invalidate(); Stacktrace: org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:568) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:470) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334) javax.servlet.http.HttpServlet.service(HttpServlet.java:727) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) root cause java.lang.ClassCastException: java.lang.Character cannot be cast to java.lang.String org.apache.jsp.adminLogin_jsp._jspService(adminLogin_jsp.java:72) org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) javax.servlet.http.HttpServlet.service(HttpServlet.java:727) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334) javax.servlet.http.HttpServlet.service(HttpServlet.java:727) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) note The full stack trace of the root cause is available in the Apache Tomcat/7.0.53 logs.
I type casted the attribute but i am still getting this error.
Upvotes: 0
Views: 1158
Reputation: 4525
Your method session.getAttribute("wrongP")
returning a char
and you tries to cast it to String
, that's why you are getting ClassCastException
.
You have to use String.valueOf(char)
to cast your char
value into String
:
String str = String.valueOf(session.getAttribute("wrongP"));
One more thing :
after getting String
, in your if
condition, you are comparing the String
object with char
value if(s.equals('0'))
.
You have to do this like :
if(s.equals("0"))
Note: Try to do minimum java code in your jsp page.
Upvotes: 1
Reputation: 88717
Your message tells you what the problem is: java.lang.Character cannot be cast to java.lang.String
So the value of that attribute is of type Character
and thus casting that to String
of course will fail.
String.valueOf(...)
would help, but keep in mind that the result depends on the actual type, e.g. if the attribute was a string array, it would not work.
Addtionally, if(s.equals('0'))
would not work, since a string and character (which you get using the literal notation '0'
) are not equal.
In your case use one of the following:
Id' prefer this because there's no cast:
String s=String.valueOf( session.getAttribute("wrongP") );
if(s.equals("0")) { //use a String here
...
}
This should work as well, but would have the risk of another ClassCastException
it the type of the attribute changes:
Character c=(Character)session.getAttribute("wrongP");
if(s.equals('0')) {
...
}
Upvotes: 1
Reputation: 2081
what you have stored in session.setAttribute("attributeName",attributeValue)
is not able to convert in String
.
Upvotes: 0
Reputation: 5440
Type casting using
(String) Object
can only type cast an object of typeString
.
If its not a String object then it will generate an error.. Better Method is to use
String.valueOf(object);
Here in your case it will surely generate an error if the session.getAttribute("wrongP")
returns an instance of object other than String.
Upvotes: 1