GeeCON Prague 2014*
The moose likes JSP and the fly likes Trouble setting an integer to a session ID Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » JSP
Bookmark "Trouble setting an integer to a session ID" Watch "Trouble setting an integer to a session ID" New topic
Author

Trouble setting an integer to a session ID

Rich Barry
Greenhorn

Joined: Jan 28, 2002
Posts: 19
Hi.. I am having trouble setting an interger to a session variaable.. I select a loginid out of the database and try to set it to a session,
but it gives this error....
"org.apache.jasper.JasperException: Unable to compile class for
JSPC:\jakarta-tomcat-3.2.1\work\localhost_8080\_0002fforum_0002fforum_0005flogincon_0002ejspforum_0005flogincon_jsp_1.java:139:
Incompatible type for method. Can't convert int to java.lang.Object.
session.setAttribute("user", result);"
Here is my code... I am not sure what to do here???
Thx
Rich

<%@ page language="java" contentType="text/html" %>
<%@ page import="java.sql.*" %>
<%@ page import="forum.DataAccess" %>
<%
// Request the information from the form
String login = request.getParameter("txtlogin");
String password = request.getParameter("txtpassword");
// This is initilzing the variables
int result = 0;
// Create a new instance of the DataAccess Class
DataAccess d = new DataAccess();
// This call the database connection method from the DataAccess Class
d.ConnectDatabase();
// This sets the sql variable to nothing
String sql = "";
// This sets the rs variable to null
ResultSet rs = null;
sql = "select * from forum_login where fl_username = '" + login + "' and fl_password = '" + password + "' ";
rs = d.SQLQuery(sql);
while (rs.next())
{
result = rs.getInt("fl_loginid");
}
%>
<html>
<head>
<title></title>
</head>
<body>
<%
if (result == 0){
%>
Invalid login
<%
}
else
{
session.setAttribute("user", login);
session.setMaxInactiveInterval(1000);
%>
Valid Login
<%
}
%>
</body>
</html>
Brian Glodde
Ranch Hand

Joined: Jun 27, 2001
Posts: 171
Incompatible type for method. Can't convert int to java.lang.Object.
The specfication states setAttribute( String, Object ), so my suggestion is to convert the int into a String object before inserting into the session. Upon retrieval, you can cast the session value to the expected datatype int.
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
Rich
I don't see the offending method call in the code you posted. The only call to setAttribute I see is:
session.setAttribute("user", login);
Which should work fine. The declaration of setAttribute is
public void setAttribute(java.lang.String name,
java.lang.Object value)
so you ahve to send it a string as the name of the attribute and an Object as the value. If you're trying to use an int as the value you'll get the Exception you describe. Try to convert the int to an Integer then store it:
session.setAttribute("user", new Integer(int));
That should do it for you.


Dave
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
Or an Integer. You can have:
// where result is some int
session.setAttribute("user", new Integer(result))
To retrieve it the cast is mandatory:
int n = ((Integer) session.getAttribute("user")).intValue();
-anthony
 
GeeCON Prague 2014
 
subject: Trouble setting an integer to a session ID