• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

usebean getProperty and setProperty in the same form field

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, my first post and please be gentle as I'm new to java.
I am building a JSP page which is displaying records from a database. I have saved the resultSet records to a javabean which then saved to a vector. I have a servlet which extracts each record/bean and forwards and displays the record details.

By pressing a next or previous button I can skip through the vector getting each record. While each record is displayed in the JSP I want to be able to update the record details and save to the vector.

I have been trying to do this by using usebean and the getProperty and setProperty in the same value field within the JSP page shown below.

<%@page import = "records.recordBean"%>

<html>
<head><title>Client Details</title></head>
<body>
<jsp:useBean id = "bean" class = "records.recordBean" scope="application" />
<FORM METHOD="POST" ACTION="/servlet/recordSkip">
<TABLE WIDTH=1000 BORDER="1">
<TR>
<TD WIDTH=250 ALIGN="RIGHT">Client ID</TD>
<TD WIDTH=250 ALIGN="LEFT">
<INPUT TYPE="TEXT" NAME="clientID" value = "<jsp:getProperty name="bean" property="clientID"/>" >
</TD>
<td WIDTH=250 ALIGN="RIGHT">Company:</td>
<TD WIDTH=250 ALIGN="LEFT">
<INPUT TYPE="TEXT" NAME="compName" value = "<jsp:getProperty name="bean" property="compName"/><jsp:setProperty name="bean" property="compName"/>" >
</TD>
</TR>
<TR>
</TABLE>
<P><INPUT TYPE="SUBMIT" VALUE="submit" NAME="submit"><INPUT TYPE="SUBMIT" VALUE="Previous" NAME="previous">
<INPUT TYPE="SUBMIT" VALUE="Next" NAME="next">
</FORM>
</table>

</body>
</html>


Is this a correct method to have a getProperty and setProperty in the same value field to get it to update the bean

value = "<jsp:getProperty name="bean" property="compName"/><jsp:setProperty name="bean" property="compName"/>"

Help and advise would be much appreciated
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch Kevin. A few things first: when posting code, please be sure to use UBB code tags. Unformatted code is extremely hard to read and many people that might be able to help you will just move along to other posts. Please read this for more information.

Secondly, forum instructions dictate that you should state what version of JSP and containers you are using. The answers for JSP 1.x can be very different than those for JSP 2.0.
[ June 26, 2008: Message edited by: Bear Bibeault ]
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For now, I'll assume JSP 2.0. And if so, then the getProperty and setProperty actions are rather outdated, replaced by the JSTL and EL.

Moreover, I do not believe that the setProperty action is doing what you intend. Are you trying to update the bean properties with data as entered by users at the browser?

If so, things don't work that way. The JSP executes on the server and sends its output HTML to the browser. So by the time that the user gets to enter data, the JSP execution is long over, and the bean is somewhere in the garbage collection process. Rather, to gather data from the user, the form must be submitted back to the server (preferably to a servlet controller. not another JSP) for processing.

You may want to take a look at this article to help understand the JSP lifecycle.

[ June 26, 2008: Message edited by: Bear Bibeault ]
[ June 26, 2008: Message edited by: Bear Bibeault ]
 
Kevin Chapple
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for you help Bear Bibeault. I'm using Netbeans 3.5 and so is JSP version 1.2 I believe.

Also in answer to your question yes I am trying to update data entered into the browser which is displayed in a form but I want to be able to change this data if required and saved back into the same bean. I'd hoped using the getProperty to display it and then the setProperty to then set it again so any changes would then be updated and saved in he bean

The course I am doing so far hasnt covered JSTL so I thought usnig the get and setproperty would work
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Kevin Chapple:
but I want to be able to change this data if required and saved back into the same bean.


Why the same bean? A rather unusual approach for a web app.

Is the ultimate purpose to update the database?
 
Kevin Chapple
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, I am building a web app which obtains records from a database. Each record needs to be displayed in a JSP which a user can view and change if required which will be updated back to a database.

I also need to be able to move to 'next'and 'previous'. I've saved each record from the resultSet into a vector and then pass the vector to a servlet which passes each javabean(record) to the JSP page by using a 'counter' variable.

As you can see I have been trying to use getProperty to display the details of the javabean and then if its changed use setProperty to alter the details in the same bean.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So how are you retaining the vector of records across each view? Or are you simply recreating the whole array every time previous or next is clicked?
[ June 28, 2008: Message edited by: Bear Bibeault ]
 
Kevin Chapple
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for you help with this Bear I really appreciate it.

I am holding the vector in a servlet which I am using called recordSkip. When 'next' or 'skip' is clicked what I am hoping happens is that the setProperty updates the record if there are any changes.

When I click next or skip this value is sent to the servlet and adds or subtracts 1 to or from the 'counter' variable I use within the servlet skipRecord. This then extracts the next or previous Javabean from the vector which is sent to the JSP which then displays the values in the JSP.

Do you have any advice on the best approach to extract a javabean from a collection. Pass this to a JSP and then change the javabean info if this is required?
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You never really answered my question, though.

When the servlet stops executing, what happens to your vector? Are you doing something to persist it across requests? Or are you just recreating it from the database every time?

It sounds as if you don't really understand the request/response nature of HTTP. If you are not doing anything to persist the vector, then grabbing all the records every time a button is clicked just to show a single record is vastly overkill.

Backing up, what is your primary intent? My guess:

1) Allow users to iterate through a series of records
2) Make optional changes as necessary as they progress through the records, and record any changes in the database

Correct so far?
[ June 29, 2008: Message edited by: Bear Bibeault ]
 
Kevin Chapple
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes you are correct.

So far my web app is using 2 servlets. One which checks a users password and username and if authorised then accesses the database and retrieves the database records and saves each record into a bean and saves the bean into a vector and then closes the database connection.

This is then passed to the 2nd servlet 'recordSkip' using getServletContext.setAttribute() and then requestDispatcher.forward().

I then access the vector in the 'recordSkip' servlet which obtains each javabean/record individually and passes to JSP.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But why create an array of all the records if you are only going to show just one? Why does your servlet not just fetch the current record for display?

The other part of the equation is the servlet that the JSP will submit any changes to. This will submit whatever form contains the new values for you to gather and make the appropriate database changes. Remember, at this point, the original vector , most of which was needless is long gone.

Ideally, your flow for a single record cycle would be as follows (ignores the authentication step which is moot):

1) A Fetch Servlet fetches the single record to be displayed given some info that uniquely identifies that record. This could be its primary key or its position within the total results.

2 ) The information for that record is stored in request scope and the JSP is forwarded to. This info should never be a resultset construct.

3) The JSP displays the UI to the user along with controls to: see next record, see previous record, change current record. (And f course, the fields to change the records data)

4) On click of next or previous, the Fetch Servlet is again invoked with info identifying the appropriate target record.

5) On click of change, the Change Servlet is invoked with the record's new data. The data is updated in the DB (preferably delegated to a model layer) and the Fetch servlet is invoked to redisplay the current record.

Does all that make sense?

It's not the only way to set this up, but it is the simplest which is probably what you should be aiming for at this point.
 
Kevin Chapple
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My thoughts were to extract all the records of of the database using a resultSet and working with the records in an array before saving the entire array of records back to the database.

In the approach you describe should I access the database everytime I want a particular record?

Thanks for the help. I'll change my design using your advice. Thanks Bear and I'll let you know how I go!
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Kevin Chapple:
My thoughts were to extract all the records of of the database using a resultSet and working with the records in an array before saving the entire array of records back to the database.

This is also a viable approach (but not without its own set of issues), but you need a way to maintain the list across requests and not just recreate it each time a button is clicked,

In the approach you describe should I access the database everytime I want a particular record?

Yes. In my approach, only a single record at a time is read from the DB. This is a much simpler approach that I would advise unless you have good reasons otherwise,
 
I am Arthur, King of the Britons. And this is a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic