Java Servlet / JSP Request Scope Part 1

What is a scope?

The scope </ font> in Java Servlet / JSP is the area where the instance can be saved. You can keep an instance within the scope area and share or pass it between the Servlet class and JS.

Basically, let's use a "JavaBeans" instance!

JavaBeans </ font> is a design pattern that enhances class independence and makes it easier to reuse as a part.

JavaBeans rules

  1. It is serializable (implements java.io.Serializable).
  2. The class is public and belongs to the package.
  3. It is public and has a no-argument constructor.
  4. The field is encapsulated.
  5. Have a getter / setter that follows the naming convention.

JavaBeans sample program

Human.java


package model;
import java.io.Serializable;

public class Human implements Serializable {
  private String name;
  private int age;

  public Human() {}
  public Human(String name, int age) {
    this.name=name;
    this.age=age;
  }
  public String getName() { return name; }
  public void setName(String name) { this.name=name; }
  public int getAge() { return age; }
  public void setAge(int age) { this.age=age; }
}

Property

The instance attribute property </ font> is generated from the setter / getter method.


Next: Request Scope Part 2

Recommended Posts