• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why do I need two actions to set bean properties?  RSS feed

 
Sergey Lotvin
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a bean:


I use this object to create date for inserting it into database. So, I need to set some properties before insert. I use JSP for this:



I found out, that I need two actions to do to send data to Servlet that calls insert method later. I pass dbObjStd to Servlet. So, after I filled the fields with data I have to click <input type="submit" value="OK" /> at first and then the other button, that calls the Servlet. <input type="submit" value="Add" name="btnInsert" /> in my case. Only after these two acts I have the data. If I do it this way:


I will get NULL in dbObjStd.name etc.
Why? How can I avoid two clicks?

Servlet code (insert method is commented, I look at System.out to check if there any data passed):


System.out when two clicks done:
DBaddData HttpServlet C Vert 101 CVR101 CVR101 descrShort CVR101 descrLongdescrLongdescrLong 85830

System.out only if Add button was clicked:
DBaddData HttpServlet null null null null 45797
 
Stefan Evans
Bartender
Posts: 1831
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sergey.

The problem here is you have code in your JSP that doesn't belong in a JSP.

Why does it take two clicks?
Let's analyze what bits do what here:

The JSP is doing two jobs:
#1 - Displaying form to user for them to submit
#2 - Reading request parameters and setting values in the "dbObjStd" bean

The servlet:
#3 - Retrieves session attribute "dbObjStd"

Question: At what point do request parameters come into the JSP? Think about the JSP lifecycle and when this code is actually executed.

The problem:
The JSP should not be responsible for converting from request parameters to objects.
That responsibility should live with your servlet.

Right now you have
#1 JSP displays your input form
#2 JSP reads request parameters and converts into java object.
#3 Servlet reads java object.

What you want is:
#1 JSP displays your input form
#2 Servlet reads request parameters and converts into java object.

In a manual fashion, that would mean you would call request.getParameter("name") and then assign it to the property - essentially what the <c:set> commands in your JSP are doing
There are a number of frameworks around that abstract that away from you and automagically convert the request parameters into beans for your classes to use (Stripes, JSF...)

cheers,
Stefan
 
Sergey Lotvin
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK. Then, the better approach is:
This form calls DBaddData Servlet:


And DBaddData Servlet read parameters from the request scope, create an instanse and calls for insert method:


Am I right?

This was my first idea. I'm currently reading Servlets and JSP_2nd Edition_A BrainFriendly Guide and I'm on JSTL chapter. So I wanted to try it. But now I can't imagine the situation when I shall use JSTL. Maybe I shall wait until the situation will arise itself.

There are a number of frameworks...

I agree, but, as I said, at the moment I'm studying clear JSP/Servlet.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65757
130
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might find these articles helpful:
  • The Secret Life of JSPs
  • The Front Man

  •  
    Stefan Evans
    Bartender
    Posts: 1831
    10
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I don't use a whole lot of the JSTL tags any more. With EL in the container (for the last 10 years now) one of the main arguments for using them went out the window.

    The most common one I would use would be the <c:if> and <c:forEach> tags.
    I don't normally bother with <c:out> even though I probably should as it does HTML escape the values it is printing.
    The <fmt> library for formatting dates etc is also useful.

    In the MVC model, JSP pages are the view. So I only use the subset of JSP tags that help with displaying the JSP, and mostly ignore those that manipulate data, and do nasty things like database connections.






     
    Bear Bibeault
    Author and ninkuma
    Marshal
    Posts: 65757
    130
    IntelliJ IDE Java jQuery Mac Mac OS X
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Stefan Evans wrote:
    I don't normally bother with <c:out> even though I probably should as it does HTML escape the values it is printing.

    Yes, <c:out> should be used for any output with text that comes from an untrusted source, like the user. It avoids the perils of script injection.

     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!