• Post Reply Bookmark Topic Watch Topic
  • New Topic

Bean problem - please help!  RSS feed

 
Lucy Smaile
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm experimenting with beans and I've put a method in my UserBean to retrieve all the user data from the database for the current user name. It works find when I test it with a main method, but when I try to access it from a jsp I just get compilation errors.
In the jsp I've got a UserBean called user, which has just been populated with a user name and password from a form. But if I then call
user.findUser();
in the jsp, it won't compile. I thought this was strange as findUser() works from the main method. Does anyone know what the problem might be?
The compilation error is:

Location: /proj/login.jsp
Internal Servlet Error:
javax.servlet.ServletException
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:508)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.tomcat.core.ServletWrapper.doService(ServletWrapper.java:405)
at org.apache.tomcat.core.Handler.service(Handler.java:287)
at org.apache.tomcat.core.ServletWrapper.service(ServletWrapper.java:372)
at org.apache.tomcat.core.ContextManager.internalService(ContextManager.java:812)
at org.apache.tomcat.core.ContextManager.service(ContextManager.java:758)
at org.apache.tomcat.service.http.HttpConnectionHandler.processConnection(HttpConnectionHandler.java:213)
at org.apache.tomcat.service.TcpWorkerThread.runIt(PoolTcpEndpoint.java:416)
at org.apache.tomcat.util.ThreadPool$ControlRunnable.run(ThreadPool.java:501)
at java.lang.Thread.run(Thread.java:484)
Root cause:
java.lang.NoSuchMethodError
at _0002flogin_0002ejsplogin_jsp_43._jspService(_0002flogin_0002ejsplogin_jsp_43.java:122)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:119)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.jasper.servlet.JspServlet$JspCountedServlet.service(JspServlet.java:130)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.jasper.servlet.JspServlet$JspServletWrapper.service(JspServlet.java:282)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:429)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:500)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.tomcat.core.ServletWrapper.doService(ServletWrapper.java:405)
at org.apache.tomcat.core.Handler.service(Handler.java:287)
at org.apache.tomcat.core.ServletWrapper.service(ServletWrapper.java:372)
at org.apache.tomcat.core.ContextManager.internalService(ContextManager.java:812)
at org.apache.tomcat.core.ContextManager.service(ContextManager.java:758)
at org.apache.tomcat.service.http.HttpConnectionHandler.processConnection(HttpConnectionHandler.java:213)
at org.apache.tomcat.service.TcpWorkerThread.runIt(PoolTcpEndpoint.java:416)
at org.apache.tomcat.util.ThreadPool$ControlRunnable.run(ThreadPool.java:501)
at java.lang.Thread.run(Thread.java:484)

Here is the offending method (the rest of UserBean is just get and set):
public void findUser()
{
Connection connection = null;
PreparedStatement userQuery = null;
try
{
// load the driver
Class.forName( "org.gjt.mm.mysql.Driver").newInstance();
// connect to the database
connection = DriverManager.getConnection(
"jdbc:mysql://localhost/quest1");
userQuery = connection.prepareStatement(
"SELECT * FROM user where user_name = ?"
);

userQuery.setString(1, userName);
ResultSet rs = userQuery.executeQuery();

// get row data
if ( rs.next() )
{
setUserName( rs.getString( 1 ) );
setPassword( rs.getString( 2 ) );
setCountry( rs.getString( 3 ) );
setStatus( rs.getString( 4 ) );
}
}
catch (SQLException sqle)
{
System.err.println("sql went wrong");
sqle.printStackTrace();
}
catch (Exception e)
{
System.err.println("some exception with sql");
e.printStackTrace();
}
finally
{
try
{
if (userQuery != null)
userQuery.close();
if (connection != null)
connection.close();
}
catch(Exception e)
{
System.err.println("problem closing");
e.printStackTrace();
}
}
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lucy
It looks like the complain is in the JSP itself not in the bean. Make sure you'e spelled the methods names correctly and that they are used in the right place in the JSP (ie not in a declaration, but in a scriptlet).
 
Lucy Smaile
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks alot for looking. Frustration!!
Well I came back to it after rebooting and it suddenly seemed to work - but strangely enough, not always!
The first time I ran it after rebooting, I got the compile error only if I pressed submit when the fields were blank. But then I gave them a value="" (and took it away again), and now I don't get any compile errors.
Usually it works now, but what really puzzles me is that it sometimes just doesn't do anything at all when I press submit. I can't see any pattern to it. Does this sometimes happen with jsps and if so, what's the likely cause?
Here's the whole of my jsp, in case you're interested:

<html>
<head>
<title>Login</title>
<link type = "text/css" rel="stylesheet" href="styles.css">

<jsp:useBean id = "user" scope = "session" class = "sonia.beans.UserBean" />

<jsp:setProperty name="user" property="*"/>

</head>
<body>
<%= user.getUserName() %>
<%= user.getPassword() %>
<%= user.getCountry() %>
<%= user.getStatus() %>
<%
if (request.getParameter("submitted") != "null")
{
user.findUser();
}
%>
<%= user.getUserName() %>
<%= user.getPassword() %>
<%= user.getCountry() %>
<%= user.getStatus() %>

<br/>
<br/>
<br/>
<div align="center">
<h1>Welcome</h1>
<form method="post" action = "login.jsp" name="login">
<input type="hidden" name="submitted" value="t">

<p>Please enter your user name and password:</p>

<table>
<tr>
<td>User Name</td>
<td>
<input type = "text" name = "userName" />
</td>
</tr>
<tr>
<td>Password</td>
<td>
<input type = "password" name = "password" />
</td>
</tr>

<tr>
<td colspan = "2" align="center">
<input type = "submit"
value = "Log-in">

</td>
</tr>
</table>


</form>
</div>
</body>
</html>
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!