How to use bean in JSP with only <jsp:useBean>
, not MVC?
Assume you have a grade.txt
file which contains following data:
Tom 90 Jerry 70 Katy 80 John 60
It asks you to create a bean named grade.java
, a JSP page named graderesult.jsp
, and a html page named gradecheck.html
.
gradecheck.html
provides a input textbox and a button submit, once you submit the name of the student, the graderesult.jsp
will communicates with bean to show the name and the score corresponding to the person.
You can make use of <jsp:setProperty name="beanname" property="*" />
to "automatically" set all request parameters as bean properties matching the property name. As this is a typical homework question, I won't give complete code examples, but only hints:
Grade
with a property name
. Map<String, Integer>
property representing name-score pairs. Learn more about Java IO here and about Java Maps here . getScore()
which returns the score from the Map
using the name
as key. <input type="text" name="name">
in the gradecheck.html
. Let the form submit to graderesult.jsp
. The request method doesn't matter, I would prefer POST
though. graderesult.jsp
use <jsp:useBean>
to declare and instantiate the bean in request
scope and use <jsp:setProperty>
to "automatically" set all input values in the bean. ${grade.name}
and the associated score by ${grade.score}
. Good luck.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.