Lucy Smaile

Ranch Hand
+ Follow
since Apr 17, 2002
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Lucy Smaile

How good a solution would it be just to mark all the necessary methods of DataBean as synchronized?
I read in Bruce Eckel Ch14 that:
"When you call any synchronized method, that object is locked and no other synchronized method of that object can be called until the first one finishes and releases the lock"
...so that would mean that no two synchronized methods of DataBean could run concurrently at all. I don't need that much security. If I just put the actual database access bits inside the methods in a block like
synchronized(this) {
// do actual database update
}
would that solve the problem better? I only need to stop versions of each individual bit of database code running concurrently with each other.
Am I missing anything important?
Thanks
21 years ago
JSP
I've got quite far with a project (student), and now I'm thinking I should synchronize some of my database access methods.
I've got one class called DataBean which is an application scope bean, and which contains all of the methods that access the database. What I'm trying to figure out is how I should organise synchronizing some of its methods. I can't seem to relate this situation to the chapter I read about threads, which specifically creates and destroys threads. With JSP, I'd have thought it's different sessions whose access I want to synchronize?
Please can anyone throw any light on the question?
Thanks
21 years ago
JSP
If people get impatient and click the submit button more than once for my surveys, the data gets sent more than once to the database - resulting in some questions writing multiple duplicate answers and some throwing exceptions...
Is there any way to stop this happening? I have sometimes seen websites which explictly say "Submit. Please click only once" but I don't really want to write that. (Maybe the answer's just to have faster access...)
Thanks for any thoughts.
21 years ago
JSP
Just to give you an update, someone else suggested building dynamic html tables and just altering the width attribute dynamically - it's blissfully simple and works fine. Maybe that's similar to Shawn's technique? I only had some very simple charts to produce. Thanks for all the suggestions anyway!
Lucy
[ August 15, 2002: Message edited by: Lucy Smaile ]
21 years ago
JSP
How do you convert a java.util.Date to java.sql.Date? (I presume this is what you have to do to store it with preparedStatement.setDate?)
Thanks
I need to dynamically generate some bar-charts in a JSP page. I was wondering if anyone knows of any good ways of doing it?
I thought of making an applet, but I may have to display up to 20 of them at a time, with different parameters, so I'm worried applets would just take for ever to load...
I wonder if there are any other (free) solutions floating around?
Any suggestions gratefully recieved!!
21 years ago
JSP
Thanks for looking. Actually it worked when I booted up again this morning - the second time a problem solved itself after giving the computer a rest. Sorry for the unnecessary post. I'm wondering if my computer's a bit dodgy.
(There's no debugging in there because I tested a modified version with a main method and dummy valueBound/Unbound methods and found that it worked outside of JSP - probably a silly way of doing it but I'm learning..)
21 years ago
JSP
Please could anyone tell what's wrong with this? I get null pointer exceptions in findUser() at these 2 lines:
userQuery = connection.prepareStatement(
"SELECT * FROM user where user_name = ?" );

userQuery.setString(1, userName);

I can't tell why. This is the code:

// JavaBean to store data for a user
package sonia.beans;
import java.sql.*;
import javax.servlet.http.*;
public class UserBean implements HttpSessionBindingListener
{
private String userName;
private String password;
private String country;
private String status;
private String bound;
private Connection connection = null;
private PreparedStatement userQuery = null;

public void findUser()
{
try
{
userQuery = connection.prepareStatement(
"SELECT * FROM user where user_name = ?"
);
}
catch (Exception e)
{
System.err.println("exception getting connection");
e.printStackTrace();
}
try
{
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("exception getting row data");
sqle.printStackTrace();
}
catch (Exception e)
{
System.err.println("some exception with sql");
e.printStackTrace();
}
}

public void valueBound(HttpSessionBindingEvent be)
{
try
{
// load the driver
Class.forName( "org.gjt.mm.mysql.Driver").newInstance();
// connect to the database
connection = DriverManager.getConnection(
"jdbc:mysql://localhost/quest1");
bound = "bound";
}
catch(Exception e)
{
System.err.println("Error connecting to database");
}
}

public void valueUnbound(HttpSessionBindingEvent be)
{
try
{
if (userQuery != null)
userQuery.close();
if (connection != null)
connection.close();
}
catch(Exception e)
{
System.err.println("problem closing");
e.printStackTrace();
}
}
21 years ago
JSP
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>
21 years ago
JSP
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();
}
}
21 years ago
JSP
I think I mainly want to model sequence and class diagrams (for me), in order to get good idea of what I'm trying to program before starting. I'm a student and it's the first time I've tried to program a largish project. I would feel very nervous about jumping into coding without having a formal kind of plan worked out first. Maybe I've just been too well brainwashed...??
I'm now planning to use separate classes on my models for my jsps as client pages and server pages (as suggested by JC - see link above) and for all the forms and beans. I think I should then be able to use these as a basis for coding.
But as I said, I'm new to all this and I was just wondering what other people do to plan web apps.
[ July 16, 2002: Message edited by: Lucy Smaile ]
21 years ago
JSP
There must be someone here who models JSP somehow??
I found some stuff by Jim Conallen who has developed an extension to UML for web applications.
http://www.conallen.com/whitepapers/webapps/ModelingWebApplications.htm
I'll probably try using that - do you think that's overkill? What other ways tend to be used?
[ July 16, 2002: Message edited by: Lucy Smaile ]
21 years ago
JSP
I also found this post from Jim Conallen that suppliments his article and I think I see now how to represent Javabeans and forms.
http://www.jguru.com/faq/view.jsp?EID=431982
So that's probably what I'll try and go ahead and do.
Here's the official UML extension
http://www.conallen.com/technologyCorner/webextension/WebExtension091.htm
I just think it's strange that there is so little discussion of this - Jim C's links seem to be the only thing that searches throw up...
I'm left wondering what everyone else does about modelling JSPs. I've posted that question in several JSP forums and had no response - is the general thing just to use class diagrams for maybe a database, then just jump in and code??
[ July 15, 2002: Message edited by: Lucy Smaile ]
Thanks for the link. But that page doesn't say anything specifically about using JSP and Javabeans. I'm stuck on how to show the interaction between JSPs and Javabeans.
I haven't been able to find much info about this. How do people normally model JSP? Is there a better way than UML?
Thanks