Initialize the class using the constructor in <jsp: useBean>
I am trying to initialize a class by passing a parameter to a constructor. I need the area to be a "page". I know I have one argument in my constructor, but how do I have one that takes a parameter using <jsp:useBean>
and can be called from the JSP page?
public class A extends B {
A(ServletRequest req) {
super(req);
}
If there is no no-arg constructor, we can use the tag < jsp:useBean id="someId" class="mypackage.A" scope="page" />
. But in the useBean JSP tag, you cannot call any constructor.
Is there a way to initialize a class with a constructor?
source to share
Not.
Use <jsp:setProperty>
,
<jsp:useBean id="someId" class="mypackage.A" scope="page">
<jsp:setProperty name="someId" property="request" value="${pageContext.request}" />
</jsp:useBean>
or use a regular servlet:
request.setAttribute("someId", new A(request));
This is surprising by the way that you noted [servlets]
in the question, while usually it shouldn't be used in conjunction with <jsp:useBean>
, as these two approaches to managing beans are conflicting (one is MVC level 1 and the other is MVC level 2) ... See also our servlet wiki page for more information .
However, the presence of a bean type property HttpServletRequest
is suspect. There are undoubtedly better ways to achieve a specific functional requirement for which you incorrectly thought it would all be the right solution .
source to share