In the previous article, we saw a first example of using JSP, where Java was embedded into the HTML to perform the dynamic part.
In this article, we are going to enhance the structure of the previous example, by making use of a Java Bean.
This structure enhances readability, and separates the code of the user interface from the code to perform computations.
In the case of more complex applications, the structure of the first example would lead to a solution difficult to understand and maintain. The structure of the solution presented in this article is more flexible, readable and so more easy to maintain and evolve.
So, we shall create a Net Beans project, just like in the first example, and make the same modifications to index.jsp. Then, we create the new JSP file (newjsp.jsp):
Contrary to what we saw in the First JSP Example, this file has not embedded the code to obtain the parameters or the calculation of the operation result. It has, instead, a JavaBean component that is no more than a class who are passed the parameters and performs the required calculations.
Tag <jsp:useBean> is an action element used to instanciate a JavaBean component. In this case, the component to instanciate is class Calculator that is localized in the package Beans.
The scope of the useBean is defined as page, meaning that the instanced bean may be used anywhere in the current page.
Other possible scopes are:
- request: the bean may be used from any JSP that processes the same request.
- session: the bean may be used from any JSP in the same session of the JSP that created the bean.
- application: the bean may be used from any page in the same application of the JSP that created the bean.
Tag <jsp:setProperty> is used to set the bean's parameter values. It is equivalent to invoking the setter of the identified class' property, and assigning it the value of the identified parameter (obtained in the request via URL).
Now, we only have to create our JavaBean:
- Create a package Beans:
- Then create a new class (Calculator) in that package:
The class' code is:
package Beans;
public class Calculator {
private int lhs = 0;
private int rhs = 0;
private int result= 0;
private String oper = "";
public int getLhs() {
return lhs;
}
public void setLhs(int lhs) {
this.lhs = lhs;
}
public int getRhs() {
return rhs;
}
public void setRhs(int rhs) {
this.rhs = rhs;
}
public String getOper() {
return oper;
}
public void setOper(String oper) {
this.oper = oper;
}
public int getResult() {
if(oper.equals("add")){
result = lhs + rhs;
}
else if(oper.equals("sub")){
result = lhs - rhs;
}
else if(oper.equals("mul")){
result = lhs * rhs;
}
else if(oper.equals("div")){
result = lhs / rhs;
}
return result;
}
}
The result is exactly the same as the one obtained in the First JSP Example.
See you next time!!!
great
ReplyDeleteiOS app development training emphasizes hands-on application building. It covers design, coding, and deployment. This ios app development training strengthens practical skills. It is dependable.
ReplyDelete“Helpful post! Our UI/UX program teaches practical design skills for user-friendly digital products.” ui ux training
ReplyDelete“Nice share! Our Java course focuses on real-world coding skills with project-based learning.” java training online
ReplyDeleteiOS app development course teaches how to build powerful applications for Apple devices. It explains Swift programming concepts clearly. This ios app development course improves mobile development skills. Learners create real-time applications. Interface design techniques are included. API integration is covered. Practical projects are provided. It prepares professional iOS developers.
ReplyDeleteA gen ai course introduces learners to the fundamentals of generative artificial intelligence and its real-world applications. It explains prompt engineering, AI models, and content generation techniques clearly. This gen ai course helps students gain practical knowledge through exercises and projects. Learners explore how AI systems generate content. The course prepares learners for careers in AI development and automation.
ReplyDeleteServiceNow training offers a comprehensive curriculum covering everything from incident management to workflow automation. Gain the skills needed to manage and optimize the ServiceNow platform effectively.servicenow online training
ReplyDeleteExcellent insights! MuleSoft certification validates your expertise and improves career prospects.Excellent insights! MuleSoft certification validates your expertise and improves career prospects.
ReplyDeleteWell explained! devops full course
ReplyDeleteis perfect for improving collaboration between development and operations teams.
Great blog! A servicenow course from OnlineITGuru helps learners understand workflows, automation, and enterprise service management.servicenow course
ReplyDeleteGreat article! OnlineITGuru offers excellent ServiceNow admin training with practical exposure and real-time scenarios.
ReplyDeleteThis post explains MuleSoft certification training very well. The focus on real-time projects is especially important for gaining practical experience.mulesoft certification training
ReplyDeleteMulesoft training courses provide detailed knowledge about APIs, connectors, and data transformation.mulesoft training courses
ReplyDeleteExcellent post! Our devops with aws course
ReplyDeleteprovides real-world training in automation, cloud deployment, and continuous monitoring for IT professionals.