I have a jsp page on which I have a scriplet element at the top of jsp page. Down in the body after f:view and h:form tags i have a4j:commandButton in which i provide action. Now when i press the a4j:commandButton each time scriplet element get executed while it is my ajax call to server. is it a normal behavior or i m doing some mistake.
1 Answers
JSP scriptlets are due to the nature of the legacy JSP view technology executed during JSF view build time (thus, also during restore view phase of a postback). So yes, it's definitely expected behaviour.
You sound like as if you would like to execute some Java code on initial GET request only and not on subsequent postbacks. In that case, check in the scriptlet if ResponseStateManager#isPostback()
returns false
.
FacesContext context = FacesContext.getCurrentInstance();
if (!context.getRenderKit().getResponseStateManager().isPostback(context)) {
// Not a postback. Do your job here.
}
Note that there's a shorter way by FacesContext#isPostback()
in JSF 2.x, but since you're using legacy JSP, I assume that you're also still using legacy JSF 1.x.
See also:
Unrelated to the concrete problem, using scriptlets is discouraged since JSP 2.0 a decade ago. It's recommended to perform the job in a normal Java class. E.g. in the (post)constructor of a backing bean. You can use <a4j:keepAlive>
to simulate the new JSF 2.0 view scope on a JSF 1.x request scoped bean.