Summary of various common operation methods of JSP to JavaBean

  • 2021-10-11 19:18:28
  • OfStack

Define an JavaBean (JavaBean is actually a simple java class)


javac -d ./ UserBean.java

Generate package file


package com.javaweb.ch07;
//1 A simple one JavaBean Example 
public class UserBean{
  // User name attribute 
  private String username;
  // User password attributes 
  private String password;
  // Get User Name 
  public String getUsername(){
    return username;
  }
  // Set User Name 
  public void setUsername(String username){
    this.username = username;
  }
  // Get user password 
  public String getPassword(){
    return password;
  }
  // Set the user's password 
  public void setPassword(String password){
    this.password = password;
  }
}


<%@page language="java" contentType="text/html;charset=gb2312"%>
<!DOCTYPE html>
<html>
  <head>
    <title> Call JavaBean</title>
  </head>
  <body>
    <%-- Pass JavaBean Action instruction call JavaBean--%>
    <jsp:useBean id="user" scope="page" class="com.javaweb.ch07.UserBean"></jsp:useBean>
    <%
      // Settings user Adj. username Attribute 
      user.setUsername("zhangdapeng");
      // Settings user Adj. password Attribute 
      user.setPassword("zhangda890126;;");
      // Printout user Adj. username
      out.println(" The user name is: "+user.getUsername()+"<br />");
      // Printout user Adj. password
      out.println(" The user's password is: "+user.getPassword()+"<br />");
    %>
    <%
      try{
        Class.forName("com.mysql.jdbc.Driver");// Loading JDBC Driver 
      }catch(ClassNotFoundException e){
        out.println(" Driver class not found ");// Prompt information when an exception is thrown 
      }
    %>
  </body>
</html>

The generated Class 1 must be placed under the classes folder in WEB-INF in the directory of your project, if not, create it yourself.

Set the properties of javaBean:
Used in jsp < jsp:setProperty > Action command to set JavaBean attribute, there are 4 formats


<jsp:setProperty name=" Instantiation object name " property="*" />

The "*" in this mode means that the JavaBean attribute is set according to all the parameters passed by the form, and the passed parameter value must keep 1 with the attribute name in JavaBean


<%@page language="java" contentType="text/html;charset=gb2312"%>
<%@page import="java.sql.*" %>
<!DOCTYPE html>
<html>
  <head>
    <title> User form </title>
  </head>
  <body>
    <form action="SetPropertyDemo.jsp" method="post">
      <table>
        <tr><td colspan="2"> User form </td></tr>
        <tr><td> User name: </td><td><input type="text" name="username" /></td></tr>
        <tr><td> User password: </td><td><input type="password" name="password" /></td></tr>
        <tr><td colspan="2"><input type="submit"><input type="reset" /></td></tr>
        <tr></tr>
      </table>
    </form>
  </body>
</html>

<%@page language="java" contentType="text/html;charset=gb2312"%>
<!DOCTYPE html>
<html>
  <head>
    <title> Settings JavaBean Attribute </title>
  </head>
  <body>
    <%-- Pass useBean Action instruction call JavaBean--%>
    <jsp:useBean id="user" scope="page" class="com.javaweb.ch07.UserBean"></jsp:useBean>
    <%-- Set according to all parameters JavaBean Attribute in --%>
    <jsp:setProperty name="user" property="*" />
    <%
      // Printout user Adj. username
      out.println(" The user name is: "+user.getUsername()+"<br />");
      // Printout user Adj. password
      out.println(" The user's password is: "+user.getPassword()+"<br />");
    %>
  </body>
</html>
<jsp:setProperty name=" Instantiation object name " property=" Attribute name " />

The page for submitting the form is the same as above


<%@page language="java" contentType="text/html;charset=gb2312"%>
<!DOCTYPE html>
<html>
  <head>
    <title> Settings JavaBean Attribute </title>
  </head>
  <body>
    <%-- Pass useBean Action instruction call JavaBean--%>
    <jsp:useBean id="user" scope="page" class="com.javaweb.ch07.UserBean"></jsp:useBean>
    <%-- Set according to all parameters JavaBean Attribute in --%>
    <jsp:setProperty name="user" property="username" />
    <%
      // Printout user Adj. username
      out.println(" The user name is: "+user.getUsername()+"<br />");
      // Printout user Adj. password
      out.println(" The user's password is: "+user.getPassword()+"<br />");
    %>
  </body>
</html>
<jsp:setProperty name=" Instantiation object name " property=" Attribute name " param=" Parameter name " />

<%@page language="java" contentType="text/html;charset=gb2312"%>
<%@page import="java.sql.*" %>
<!DOCTYPE html>
<html>
  <head>
    <title> User form </title>
  </head>
  <body>
    <form action="SetPropertyDemo.jsp" method="post">
      <table>
        <tr><td colspan="2"> User form </td></tr>
        <tr><td> User name: </td><td><input type="text" name="username" /></td></tr>
        <tr><td> User password: </td><td><input type="password" name="userpassword" /></td></tr>
        <tr><td colspan="2"><input type="submit"><input type="reset" /></td></tr>
        <tr></tr>
      </table>
    </form>
  </body>
</html>

Note here the name property of the password form


<%@page language="java" contentType="text/html;charset=gb2312"%>
<!DOCTYPE html>
<html>
  <head>
    <title> Settings JavaBean Attribute </title>
  </head>
  <body>
    <%-- Pass useBean Action instruction call JavaBean--%>
    <jsp:useBean id="user" scope="page" class="com.javaweb.ch07.UserBean"></jsp:useBean>
    <%-- Set according to all parameters JavaBean Attribute in --%>
    <jsp:setProperty name="user" property="username" param="username"/>
    <span style="color:#e53333;"><jsp:setProperty name="user" property="<span style="color:#003399;">password</span>" param="<span style="color:#003399;">userpassword</span>"/></span>    <%
      // Printout user Adj. username
      out.println(" The user name is: "+user.getUsername()+"<br />");
      // Printout user Adj. password
      out.println(" The user's password is: "+user.getPassword()+"<br />");
    %>
  </body>
</html>

The above code pays attention to the blue part in red, which is more flexible


<jsp:setProperty name=" Instantiation object name " property=" Attribute name " value=" Attribute value " />

<%@page language="java" contentType="text/html;charset=gb2312"%>
<!DOCTYPE html>
<html>
  <head>
    <title> Settings JavaBean Attribute </title>
  </head>
  <body>
    <%-- Pass useBean Action instruction call JavaBean--%>
    <jsp:useBean id="user" scope="page" class="com.javaweb.ch07.UserBean"></jsp:useBean>
    <%-- Set according to all parameters JavaBean Attribute in --%>
    <jsp:setProperty name="user" property="username" value="Devidpeng"/>
    <jsp:setProperty name="user" property="password" value="Devidpeng"/>
    <%
      // Printout user Adj. username
      out.println(" The user name is: "+user.getUsername()+"<br />");
      // Printout user Adj. password
      out.println(" The user's password is: "+user.getPassword()+"<br />");
    %>
  </body>
</html>

Get the properties of JavaBean:


<jsp:getProperty name="" property=""/>

<%@page language="java" contentType="text/html;charset=gb2312"%>
<!DOCTYPE html>
<html>
  <head>
    <title> Get JavaBean Attribute </title>
  </head>
  <body>
    <%-- Pass useBean Action instruction call JavaBean--%>
    <jsp:useBean id="user" scope="page" class="com.javaweb.ch07.UserBean"></jsp:useBean>
    <%-- Set according to all parameters JavaBean Attribute in --%>
    <jsp:setProperty name="user" property="username" value="Devidpeng"/>
    <jsp:setProperty name="user" property="password" value="zhangdapeng"/>
    <jsp:getProperty name="user" property="username" />
    <jsp:getProperty name="user" property="password" />
  </body>
</html>

Set the range of JavaBean:
Set the JavaBean of the page range


<%@page language="java" contentType="text/html;charset=gb2312"%>
<!DOCTYPE html>
<html>
  <head>
    <title> Settings page The scope of </title>
  </head>
  <body>
    <%-- Pass useBean Action instruction call JavaBean--%>
    <jsp:useBean id="user" scope="<span style="color:#e53333;">page</span>" class="com.javaweb.ch07.UserBean"></jsp:useBean>
    <%-- Set according to all parameters JavaBean Attribute in --%>
    <jsp:setProperty name="user" property="username" value="page_username"/>
    <jsp:setProperty name="user" property="password" value="page_password"/>
    <jsp:forward page="PageJavaBeanDemo2.jsp"/>
  </body>
</html>

<%@page language="java" contentType="text/html;charset=gb2312"%>
<!DOCTYPE html>
<html>
  <head>
    <title> Settings page The scope of </title>
  </head>
  <body>
    <%-- Pass useBean Action instruction call JavaBean--%>
    <jsp:useBean id="user" scope="<span style="color:#e53333;">page</span>" class="com.javaweb.ch07.UserBean"></jsp:useBean>
    <%-- Set according to all parameters JavaBean Attribute in --%>
    <jsp:getProperty name="user" property="username" />
    <jsp:getProperty name="user" property="password" />
  </body>
</html>

Set JavaBean of request range


package com.javaweb.ch07;
//1 A simple one JavaBean Example 
public class UserBean{
  // User name attribute 
  private String username;
  // User password attributes 
  private String password;
  // Get User Name 
  public String getUsername(){
    return username;
  }
  // Set User Name 
  public void setUsername(String username){
    this.username = username;
  }
  // Get user password 
  public String getPassword(){
    return password;
  }
  // Set the user's password 
  public void setPassword(String password){
    this.password = password;
  }
}

0

Set the JavaBean of the session range


package com.javaweb.ch07;
//1 A simple one JavaBean Example 
public class UserBean{
  // User name attribute 
  private String username;
  // User password attributes 
  private String password;
  // Get User Name 
  public String getUsername(){
    return username;
  }
  // Set User Name 
  public void setUsername(String username){
    this.username = username;
  }
  // Get user password 
  public String getPassword(){
    return password;
  }
  // Set the user's password 
  public void setPassword(String password){
    this.password = password;
  }
}

1

Set JavaBean of application range


package com.javaweb.ch07;
//1 A simple one JavaBean Example 
public class UserBean{
  // User name attribute 
  private String username;
  // User password attributes 
  private String password;
  // Get User Name 
  public String getUsername(){
    return username;
  }
  // Set User Name 
  public void setUsername(String username){
    this.username = username;
  }
  // Get user password 
  public String getPassword(){
    return password;
  }
  // Set the user's password 
  public void setPassword(String password){
    this.password = password;
  }
}

2

Remove the properties of JavaBean:
The removal of JavaBean varies according to different ranges

pageContext, request, session and application are used corresponding to page range, request range, session range and application range

Methods all use the removeAttribute () method

Example


<%@page language="java" contentType="text/html;charset=gb2312"%>
<!DOCTYPE html>
<html>
  <head>
    <title> Settings page The scope of </title>
  </head>
  <body>
    <%-- Pass useBean Action instruction call JavaBean--%>
    <jsp:useBean id="user" scope="application" class="com.javaweb.ch07.UserBean"></jsp:useBean>
    <%
      // Remove application Properties of the scope 
      application.removeAttribute("user"); 
    %>
    <%-- Set according to all parameters JavaBean Attribute in --%>
    <jsp:getProperty name="user" property="username" />
    <jsp:getProperty name="user" property="password" />
  </body>
</html>


Related articles: