wood burning stoves 2.0
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

Trouble setting an integer to a session ID

Rich Barry

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
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???

<%@ 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
// 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");
if (result == 0){
Invalid login
session.setAttribute("user", login);
Valid Login
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
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.

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();
I agree. Here's the link: http://aspose.com/file-tools
subject: Trouble setting an integer to a session ID
It's not a secret anymore!