JSP Tutorial uses JavaBean to complete the business logic method _jsp programming

Source: Internet
Author: User

This article is an example of how the JSP tutorial uses JavaBean to complete the business logic. Share to everyone for your reference. Specifically as follows:

First, the goal:

① master what is JavaBean;
② Master how to write JavaBean;
③ Master How to access JavaBean in JSP;
④ understand the 4 scopes of objects in a JSP.

Second, the main content:

By encapsulating the verification process in the previous lecture login_process.jsp to form JavaBean, and then invoking the JavaBean in the JSP page, the author introduces JavaBean's writing and access.
The 4 scopes of JavaBean objects are described by a simple example.

1, what is JavaBean

JavaBean are components written in the Java language. A component is a part of a large system that normally operates independently of the other. Components can perform specific functions, and these functions are primarily shared.
JavaBean is a special Java class. Special in: Usually provide no parameter construction method, the outside world does not need to know its concrete implementation, usually the property is private, need to provide public to operate on the property method; There should be some business methods.

2. Example: Verify the user information

Package JavaBean;
public class User
{
  private String username;
  Private String Userpass;
  Public String GetUserName ()
  {return
   username;
  }
  public void Setusername (String username)
  {
   this.username = username;
  }
  Public String Getuserpass ()
  {return
   userpass;
  }
  public void Setuserpass (String userpass)
  {
   this.userpass = userpass;
  }
  public boolean check ()
  {
   if (Username==null | | userpass==null) return
     false;
   if (Username.equals ("Zhangsan") && userpass.equals ("Lisi"))
   {return
     true;
   } else{return
     false;}}

3. How to call JavaBean in JSP

First, the invocation in Java:

Import JavaBean;
  ... User user = new user (); Defines the object and instantiates
  user.setusername ("zaaaa");//initializes the member variable
  user.setuserpass ("sssss");  Initialize the user's password
  boolean b = User.check ();  Calling a business method for validation

1) Creating objects

Copy Code code as follows:
<jsp:usebean id= "class=" "scope=" "/>

Role: Instantiate the object of the JavaBean, or find the JavaBean object.

The id attribute represents the name of the created object, class is the full class name of the JavaBean, and the Scope property indicates the scope of the variable being created.
Scope has 4 Scopes: page Request session Application
Page represents the current page, which is actually the object of the Java class corresponding to the current JSP file;
The request represents a request, from the time we send out the request to the system to respond to us. If one request involves only one JSP page, the effect of page and request is the same.
For example: The login function, when the first page is displayed, the page and request scope are the same. After the commit, if the login succeeds, it involves two files login_process.jsp and success.jsp, each file corresponds to a page, but two files belong to a request.
Session, which represents the conversation. For each client of a Web application, an object is a session, and if the scope of the JavaBean object is set to session, it is equivalent to saving the JavaBean object in the Session object. This object can be accessed throughout the client's access process.
Application, representing applications. If the scope of the JavaBean object is set to application, all users of the application can access the object in all interfaces.

Example: <jsp:usebean id= "user" class= "JavaBean". User "scope=" Request "/>
Test (about 4 scope of action):

The page.jsp document is prepared as follows:

<%@ page contenttype= "text/html;charset=gb2312"%>
<%@ taglib prefix= "C" uri= "http://java.sun.com/jsp/" Jstl/core "%> <c:set var= pageattribute" value= "PPPP" scope= "
page"/> <c:set "var=
" Requestattribute "value=" rrrrr "scope=" request "/> <c:set var=" Sessionattribute "value=" SSSs "scope="
Session "/>
<c:set var=" Applicationattribute "value=" AAAA "scope=" Application "/>
<jsp:include Page= "success.jsp"/>

Output information in page.jsp:

${pagescope.pageattribute}
${requestscope.requestattribute}
${sessionscope.sessionattribute}
${ Applicationscope.applicationattribute}

Where the:<c:set> tag is used to store information, VAR indicates the variable's name, value indicates the value of the variable (you can use an expression), and scope indicates the storage scope of the variable.

${pagescope.pageattribute} is used to access the Pageattribute property in the page range, ${requestscope.requestattribute} Use to access the Requestscope properties within the request scope, and so on.

The request.jsp document is prepared as follows:

<%@ page contenttype= "text/html;charset=gb2312"%>
<%@ taglib prefix= "C" uri= "http://java.sun.com/jsp/" Jstl/core "%>

Output information in request.jsp:

${pagescope.pageattribute}
${requestscope.requestattribute}
${sessionscope.sessionattribute}
${ Applicationscope.applicationattribute}
<br>
<a href= "session.jsp" > Access session</a>

The session.jsp document is prepared as follows:

<%@ page contenttype= "text/html;charset=gb2312"%>
<%@ taglib prefix= "C" uri= "http://java.sun.com/jsp/" Jstl/core "%>

Output information in session.jsp:

${pagescope.pageattribute}
${requestscope.requestattribute}
${sessionscope.sessionattribute}
${ Applicationscope.applicationattribute}

The application.jsp document is prepared as follows:

<%@ page contenttype= "text/html;charset=gb2312"%>
<%@ taglib prefix= "C" uri= "http://java.sun.com/jsp/" Jstl/core "%>

Output information in application.jsp:

${pagescope.pageattribute}
${requestscope.requestattribute}
${sessionscope.sessionattribute}
${ Applicationscope.applicationattribute}

Run the process:

Run page.jsp and get the following results:

request.jsp Output information: rrrr ssss AAAA
page.jsp Output information: pppp rrrr ssss AAAA
Analysis: Because page.jsp and request.jsp belong to the same request, the request object, Session object, and Application object are shared, but the information stored in the page cannot be displayed.

Click "Access session" to session.jsp

session.jsp Output information: SSSs AAAA

Analysis: The request and page information cannot be accessed without the same requests as before, and only session and application can be accessed.

Reopen a browser to access application.jsp
Output information in application.jsp: AAAA
Analysis: belong to different clients, so can only share application objects, only see the information in application.

2 Assigning values to properties

<jsp:setproperty name= "property=" "value=" "/>
Name indicates the names of the JavaBean objects, which indicate the name of the property and the value to assign.

Example: <jsp:setproperty name= "user" property= "username" value= "Zhangsan"/>

If the Value property is not specified, a FORM element with the name username is looked up from the request object if there is a saved assignment. Equivalent to the following code:

String username = request.getparameter ("username");
User.setusername (username);

If the Value property is omitted and the property attribute is set to "*", the equivalent:

String username = request.getparameter ("username");
User.setusername (username);
String Userpass = Request.getparameter ("Usernpass");
User.setusernpass (Userpass);

If the name of the form element does not match the name of the JavaBean property, you can specify which form element to use through the Param property.

3) Embed Java code call method

<%
  Boolean b = User.check ();
%>

4) Get JavaBean property

Copy Code code as follows:
<jsp:getproperty name= "" property= ""/>

4. Example: Modify login_process.jsp

<%@ page contenttype= "text/html;charset=gb2312"%>
<%@ taglib prefix= "C" uri= "http://java.sun.com/jsp/" Jstl/core "%>
<!--Create object-->
<jsp:usebean id=" user "class=" JavaBean. User "scope="/>
<!--Initializes an object-->
<jsp:setproperty name= "user" property= "*"
/> <!--method of calling objects-->
<%
  Boolean b = User.check ();
  Request.setattribute ("B", new Boolean (b));
  Save the information in request, the first parameter is the name, the second parameter object itself
%>
<!--based on the results of the judgment-->
<c:if test= "${b}" >
  <jsp:include page= "success.jsp"/>
</c:if>
<c:if test= "${!b}" >
  <jsp:include Page= "failure.jsp"/>
</c:if>
<br> user information:
<jsp:getproperty name= "user" property= " Username "/>
<jsp:getproperty name=" user "property=" Userpass "/>

Run: The Java file User.java written before compiling. The compiled files are then placed in the classes subdirectory of the project's Web-inf directory.

The effect is the same after the run.

But analyzing the code above, we found that the Java code was embedded inside, because the JSP did not provide a good tag to call the JavaBean access. If you want to use tags on business method calls, you need to write custom tags, more cumbersome. Here's a compromise approach.

5. Example: Reconstructing login_process.jsp

Add the following methods to the User.java:

public boolean GetResult ()
{return
   check ();
}

Although the JSP does not provide a label to access the business method, but provides a way to access the property, the method indirectly invokes the check business method by providing the result property.

Modify the login_process.jsp file as follows:

<%@ page contenttype= "text/html;charset=gb2312"%>
<%@ taglib prefix= "C" uri= "http://java.sun.com/jsp/" Jstl/core "%>
<jsp:usebean id=" user "class=" JavaBean. User "scope=" request/>
<jsp:setproperty name= "user" property= "*"/> <c:if
"test= ${user.result}" ">
  <jsp:include page=" success.jsp "/>
</c:if>
<c:if test=" ${!user.result} ">
  <jsp:include page= "failure.jsp"/>
</c:if>

I hope this article will help you with the JSP program design.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.