• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

bean and jstl question

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This question has been edited for easier reading.
following code I have taken from an example


<jsp:useBean id="bean1" class="ourbeans.player" > <jsp:setProperty name="bean1" property="*" /> </jsp:useBean>

we are using bean instead of putting logic in the jsp page.

But, we should first create the 'player ' bean with all the attributes and getter & setter methods, as shown.



// player.java package ourbeans; public class player{ String name; String place; String game; public player(){ name=" "; place=" "; game=" "; } public void setName(String a){ name=a; } public void setPlace(String b){ place=b; } public void setGame(String c){ game=c; }

How is the values of parameters a, b and c is passed to the setName(String a), setPlace(String b) and setGame(String c) from the form without {param.text} ? If the values are passed from the form using {param.text} , then why not use that directly in the jsp page rathet than calling it through bean ? What is the use of bean in such a situation ?



public String getName(){ return name; } public String getPlace(){ return place; } public String getGame(){ return game; } }

Whats the need of getter methods, when all the variables are public and can be called directly ?

In this demo2.jsp, we collect the data and then display the data entered by the user. Question is, how is data sent to the bean from the form ?

Note that instead of {param.text1}, we are using {bean1.name}. We should carefully name the html form controls with the corresponding attribute names given in the bean. We cannot name the controls as 'text1' etc, now! Question is, we can use {bean1.name} in the jsp page and collect data from bean1 in the jsp page. But how does bean1 get parameters from the form ?



why use ${bean1.name} ? We can directly get parameters from form as ${param.text} ? Whats the point in using bean in this situation ?


thanks


 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

nirali shah wrote:
Whats the point in using bean ?


to Avoid the scriptlet in jsp.I guess nowadays useBean itself consider as bad practise

nirali shah wrote:
In the class player, all the variables are declared as public. In that case why do we need getter methods ?



I guess , useBean implementation is based on setter and getter of Bean
 
Sheriff
Posts: 67750
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
Please be sure to use code tags when posting code to the forums. Unformatted code is extremely hard to read and many people that might be able to help you will just move along to posts that are easier to read. Please read this for more information.

You can go back and change your post to add code tags by clicking the button on your post.

 
Bear Bibeault
Sheriff
Posts: 67750
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

seetharaman venkatasamy wrote:

nirali shah wrote:
Whats the point in using bean ?

I guess nowadays useBean itself consider as bad practise


Not correct in general. As useBean creates a scoped variable, it is still viable for use with the JSTL and EL.

Using it for this purpose, however -- gathering request params in a JSP rather than in a servlet controller -- is rather dated and not best practice.
 
What I don't understand is how they changed the earth's orbit to fit the metric calendar. Tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic