Use of the actions of the JSP introductory beginner tutorial

Source: Internet
Author: User
Tags expression flush java web

JSP actions use you can dynamically insert a file, reuse the JavaBeans component, advance to another page, or generate an HTML for the Java plug-in. The action you can use is:

(1) Jsp:include--Included in a file when the page is requested.

(2) jsp:usebean--Find or instantiate a JavaBean.

(3) jsp:setproperty--sets a JavaBean property.

(4) jsp:getproperty--inserts the JavaBean attribute into the output.

(5) jsp:forward--allows the requester to move forward to a new page.

(6) jsp:plugin--uses the object or embed tag to generate a specific browser code for the Java plugins.

1. Jsp:include Action

This action allows you to include some files on the page you are about to generate:

<jsp:include page="relative URL" flush="true" />  

Unlike include directive, this action is to include a file when the page is requested, and include directive contains files when the JSP page is converted to a servlet. In order to improve efficiency, include action makes a small sacrifice, that is, it does not allow the included pages contain generic JSP code (for example, cannot set HTTP headers), but it has significant flexibility, such as the following JSP code, it implements four different fragments into the following page. Each time the title changes, you only need to modify the four files without changing the main JSP page.

whatsnew.jsp

<HTML>
<HEAD>
<TITLE> JSP教程</TITLE>
<BODY >
<CENTER>
<TABLE BORDER=5 BGCOLOR="#EF8429">
<TR><TH CLASS="TITLE"> What"s New at Chinese comic sites</TABLE>
</CENTER>
<P>
Here is a summary of our four most recent news stories:
<OL>
<LI><jsp:include page="news/Item1.html" flush="true"/>
<LI><jsp:include page="news/Item2.html" flush="true"/>
<LI><jsp:include page="news/Item3.html" flush="true"/>
<LI><jsp:include page="news/Item4.html" flush="true"/>
</OL>
</BODY>
</HTML>

Of course you can define yourself? ml file, but one thing to note:

You should put the file in the news directory under your JSP directory.

Use of Jsp:usebean Action

First, the grammar:

<jsp:useBean
id="beanInstanceName"
scope="page|request|session|application"
{ class="package.class" |
type="package.class" |
class="package.class" type="package.class" |
beanName="{package.class | <%= expression %>}" type="package.class"
}
{ /> |
> 其他元素
</jsp:useBean>
}

This action enables you to load a javabean into a JSP page. This is a very useful capability because it allows you to use reusable Java classes without sacrificing performance. The simplest syntax is to specify a bean:

<jsp:useBean id="name" class="package.class" />

This usually means "instantiating an object of a class by specifying a class and binding it to a variable with a name specified by ID." However, as we have seen, you can specify a scope property to make the bean not only associated with the current page. In this case, it is useful to get a reference to an existing bean, and to create a new one only if there are no identical IDs and scope beans. Now that you have a bean, you can modify it by Jsp:setproperty, or use the Scriptlet or explicit invocation method by using a name that was previously specified with an ID. When you say "This bean has a property of type X called foo", what you really mean is "this class has a method called Getfoo that returns a class value of type X, and another method called Setfoo, which takes X as an argument." "This jsp:setproperty action will be described in detail in the next unit, but now you can either give a definite value, give a property that the value is inherited from the request's arguments, or simply list the property to flag that the value should inherit from a parameter with the same name as the property name. You can get an existing JSP expression or Scriptlet property by calling the applicable GetXXX method, or more generally, using the Jsp:getproperty action.

Note that the class specified for the bean must be under the class path of the server's rule, not the path of the class that is being automatically loaded when the change occurs. For example, on a Java WEB server, it and the classes it uses must be in the class's directory or within a jar file in the Lib directory, not in the Servlets directory.

Let's look at a very simple example that loads a bean and sets/gets a simple string parameter.

beantest.jsp

<HEAD>
<TITLE>Reusing JavaBeans in JSP</TITLE>
</HEAD>
<BODY>
<CENTER>
<TABLE BORDER=5>
<TR><TH CLASS="TITLE">
Reusing JavaBeans in JSP</TABLE>
</CENTER>
<P>
<jsp:useBean id="test" class="hall.SimpleBean" />
<jsp:setProperty name="test"
property="message"
value="Hello WWW" />
<H1>Message: <I>
<jsp:getProperty name="test" property="message" />
</I></H1>
</BODY>

Simplebean.java

The following is the original code for the Bean:

package hall;
public class SimpleBean {
 private String message = "No message specified";
 public String getMessage() {
  return(message);
 }
 public void setMessage(String message) {
  this.message = message;
 }
}

The results of the operation are: page output: Reusing JavaBeans in JSP

Message:hello WWW

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.