jQuery in Action, 2nd edition*
The moose likes JSP and the fly likes UltraDev JSP Help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "UltraDev JSP Help" Watch "UltraDev JSP Help" New topic
Author

UltraDev JSP Help

Jenn Hickok
Greenhorn

Joined: Jul 09, 2002
Posts: 4
I have been working in UltraDev to create a registration and login page, but to no avail. I can get the registration to work, but I can't get a simple check to determine whether a userid has already been used. This is the code I'm using:
// *** Redirect if username exists
String MM_flag="MM_insert";
if (request.getParameter(MM_flag) != null) {
String MM_dupKeyRedirect="register.jsp";
String MM_rsKeyConnection=MM_LearningSite_STRING;
String MM_dupKeyUsernameValue = request.getParameter("userid");
String MM_dupKeySQL = "SELECT USERID FROM IMAATDBA.STUDENT_DATA WHERE USERID='" + MM_dupKeyUsernameValue + "'";
Driver MM_rsKeyDriver = (Driver)Class.forName(MM_LearningSite_DRIVER).newInstance();
Connection MM_rsKeyConn = DriverManager.getConnection(MM_rsKeyConnection,MM_LearningSite_USERNAME,MM_LearningSite_PASSWORD);
PreparedStatement MM_rsKeyStatement = MM_rsKeyConn.prepareStatement(MM_dupKeySQL);
ResultSet MM_rsKey = MM_rsKeyStatement.executeQuery();
boolean MM_rsKey_isEmpty = !MM_rsKey.next();
MM_rsKey.close(); // Close the recordset - we have all the info we need.
MM_rsKeyConn.close();
if (!MM_rsKey_isEmpty) {
// the username was found - can not add the requested username
String MM_qsChar = "?";
if (MM_dupKeyRedirect.indexOf("?") >= 0) MM_qsChar = "&";
MM_dupKeyRedirect = MM_dupKeyRedirect + MM_qsChar + "requsername=" + MM_dupKeyUsernameValue;
response.sendRedirect(response.encodeRedirectURL(MM_dupKeyRedirect));
I get an Illegal State Exception Error everytime I run it, any ideas?
Thanks,
Jenn
chanoch wiggers
Author
Ranch Hand

Joined: May 24, 2001
Posts: 245
probaby one of the close() calls. Not sure, try writing System.out.println("trying to execute quety")l
System.out.println("ran execute query");
and see how far you get when the illegal state exception has been thrown.


chanoch<p><a href="http://www.amazon.com/exec/obidos/ASIN/1861007736/" target="_blank" rel="nofollow">Author of Professional Apache Tomcat</a></p>
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
Jenn
Chanoch is probabaly right and it is one of the close() statements that is the cause of the exception. does it give you a line number or specify which method is causing the problem?
Both the ResultSet and the connectio object can be closed when they are garbage collected, although if this happening on a regular basis that is probably not the case. The ResultSet is also closed when the Statement that it is associated with is closed so you might want to try to close the MM_rsKeyStatement variable instead of the ResultSet. Also, the Connection object has an isClosed() method that tells you if it is closed or not so you might want to put the close in a finalize method and then test it and close it if neccesary.
hope that helps - if you have it, let us know exacly which method is the culprit.


Dave
chanoch wiggers
Author
Ranch Hand

Joined: May 24, 2001
Posts: 245
Hi Jenn, thanks for the personal message - I will try to answer it. You said that you are quite new to this (I guess that this code is all automatically created right?) so here is one way to solve it - I seem to remember that in Dreamweaver MX, there is a live access utility that allows you to see the actual results live through the server while you edit - if that is what yuo are using, you might not have a shell to see system out println statements, so here is an alternative that should tell you where the problem is. I am hoping that your bit of code is in a jsp, so that what I am doing is legal. When it works, you should remove all the extra lines I've put in, except for the check for isClosed() that dave suggested
try {
// *** Redirect if username exists
String MM_flag="MM_insert";
if (request.getParameter(MM_flag) != null) {
// if it exists, continue
// register is where the user is sent if they have chosen a duplicate username
String MM_dupKeyRedirect="register.jsp";
// rs key connection is the connection string to your database (whats the database btw?)
String MM_rsKeyConnection=MM_LearningSite_STRING;
// look for the userid (username) in the request
String MM_dupKeyUsernameValue = request.getParameter("userid");
// search for that userid in the database
String MM_dupKeySQL = "SELECT USERID FROM IMAATDBA.STUDENT_DATA WHERE USERID='" + MM_dupKeyUsernameValue + "'";
/*
nothing should go wrong up until this point
Its a bit wasteful not to check that the user id
retrieved from the request is not null, because it means connecting to the database for nothing. I would add a check for userid.equals("") and userid != null
from this point on, is where the problem might be
*/
%>About to create driver<%
// instantiate the db driver
Driver MM_rsKeyDriver = (Driver)Class.forName(MM_LearningSite_DRIVER).newInstance();
%>Created driver<%
// create a connection
Connection MM_rsKeyConn = DriverManager.getConnection(MM_rsKeyConnection,MM_LearningSite_USERNAME,MM_LearningSite_PASSWORD);
// is it worth checking the connection was correctly created?
%>Created a connection to the database.<%
// prepare and execute the query
PreparedStatement MM_rsKeyStatement = MM_rsKeyConn.prepareStatement(MM_dupKeySQL);
%>Created the prepared statement<%
ResultSet MM_rsKey = MM_rsKeyStatement.executeQuery();
%>Executed the query<%
// if there are an results in the set then its not empty and there is a user with that id
boolean MM_rsKey_isEmpty = !MM_rsKey.next();
%>Just called next() on the result set. About to close result set<%
MM_rsKey.close(); // Close the recordset - we have all the info we need.
// extra line of code...
// if the connection is not closed
if(!MM_rsKeyConn.isClosed()) {
// close it
%>About to close connection<%
MM_rsKeyConn.close();
}
%>Closed connection. Nothing should go wrong from here on<%
if (!MM_rsKey_isEmpty) {
// the username was found - can not add the requested username
String MM_qsChar = "?";
if (MM_dupKeyRedirect.indexOf("?") >= 0) MM_qsChar = "&";
MM_dupKeyRedirect = MM_dupKeyRedirect + MM_qsChar + "requsername=" + MM_dupKeyUsernameValue;
response.sendRedirect(response.encodeRedirectURL(MM_dupKeyRedirect));
} catch (Exception e) {
%>An exception occurred.<%=e.getMessage()%><%
}
%>
try that and tell us what you find
[ July 11, 2002: Message edited by: chanoch wiggers ]
Jenn Hickok
Greenhorn

Joined: Jul 09, 2002
Posts: 4
Chanoch,
Thank you!! I put that in and ran it on my server and got the following error:
org.apache.jasper.JasperException: Unable to compile class for JSP
An error occured between lines: 82 and 91 in the jsp file: /register.jsp
Generated servlet error:
D:\Tomcat 4.0\work\localhost\CourseBuilder\register$jsp.java:219: 'catch' without 'try'.
} catch (Exception e) {
^
D:\Tomcat 4.0\work\localhost\CourseBuilder\register$jsp.java:338: 'catch' without 'try'.
} catch (Throwable t) {
^
D:\Tomcat 4.0\work\localhost\CourseBuilder\register$jsp.java:346: 'try' without 'catch' or 'finally'.
}
^
D:\Tomcat 4.0\work\localhost\CourseBuilder\register$jsp.java:346: '}' expected.
}
^
4 errors
I really appreciate your helping out a clueless newbie. Thanks again!
Jenn
chanoch wiggers
Author
Ranch Hand

Joined: May 24, 2001
Posts: 245
sorry, my fault, the code should have had extra braces in which I have obviously not caught. Try this, to which 've added some lines you should take out, probably everything from the start to the try { line. The URL that is accessing this page should have a
?MM_insert=true
after the page name, is this the case?
<%@page import="java.sql.ResultSet, java.sql.PreparedStatement, java.sql.Driver, java.sql.Connection, java.sql.DriverManager"%>
<html><%
String MM_LearningSite_STRING = "jdbc dbc:mysite"; // testing
String MM_LearningSite_DRIVER = "com.sun.odbc.jdbc.OdbcJdbcDriver";
String MM_LearningSite_USERNAME = "";
String MM_LearningSite_PASSWORD = "";
%>About enter try block<%
try {
// *** Redirect if username exists
String MM_flag="MM_insert";
if (request.getParameter(MM_flag) != null) {
// if it exists, continue
// register is where the user is sent if they have chosen a duplicate username
String MM_dupKeyRedirect="register.jsp";
// rs key connection is the connection string to your database (whats the database btw?)
String MM_rsKeyConnection=MM_LearningSite_STRING;
// look for the userid (username) in the request
String MM_dupKeyUsernameValue = request.getParameter("userid");
%>About to construct sql<%
// search for that userid in the database
String MM_dupKeySQL = "SELECT USERID FROM IMAATDBA.STUDENT_DATA WHERE USERID='" + MM_dupKeyUsernameValue + "'";
/*
nothing should go wrong up until this point
Its a bit wasteful not to check that the user id
retrieved from the request is not null, because it means connecting to the database for nothing. I would add a check for userid.equals("") and userid != null

from this point on, is where the problem might be
*/
%>About to create driver<%
// instantiate the db driver
Driver MM_rsKeyDriver = (Driver)Class.forName(MM_LearningSite_DRIVER).newInstance();
%>Created driver<%
// create a connection
Connection MM_rsKeyConn = DriverManager.getConnection(MM_rsKeyConnection,MM_LearningSite_USERNAME,MM_LearningSite_PASSWORD);
// is it worth checking the connection was correctly created?
%>Created a connection to the database.<%
// prepare and execute the query
PreparedStatement MM_rsKeyStatement = MM_rsKeyConn.prepareStatement(MM_dupKeySQL);

%>Created the prepared statement<%
ResultSet MM_rsKey = MM_rsKeyStatement.executeQuery();
%>Executed the query<%
// if there are an results in the set then its not empty and there is a user with that id
boolean MM_rsKey_isEmpty = !MM_rsKey.next();
%>Just called next() on the result set. About to close result set<%
MM_rsKey.close(); // Close the recordset - we have all the info we need.
// extra line of code...
// if the connection is not closed
if(!MM_rsKeyConn.isClosed()) {
// close it
%>About to close connection<%
MM_rsKeyConn.close();
}
%>Closed connection. Nothing should go wrong from here on<%
if (!MM_rsKey_isEmpty) {
// the username was found - can not add the requested username
String MM_qsChar = "?";
if (MM_dupKeyRedirect.indexOf("?") >= 0) MM_qsChar = "&";
MM_dupKeyRedirect = MM_dupKeyRedirect + MM_qsChar + "requsername=" + MM_dupKeyUsernameValue;
response.sendRedirect(response.encodeRedirectURL(MM_dupKeyRedirect));
}
}
} catch (Exception e) {
%>An exception occurred.<%=e.getMessage()%><%
}%>
</html>
Jenn Hickok
Greenhorn

Joined: Jul 09, 2002
Posts: 4
The database is Oracle...if that's what you meant by that.
I'm still having issues, is there a way I could send this whole page to you so that you could look at it?
Thanks,
Jenn
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: UltraDev JSP Help
 
Similar Threads
jsp and jdbc
struts2 MVC pattern - business logic question
Connection reset by peer:JVM_recv in socket input stream read
For Jsp Development, UltraDev or JBuilder5?
jsp/Ultradev/Tomcat/Access/Sybase