JSP and JavaBean
Although you can put a large piece of program code in a small instruction file, most Java program code is a reusable component called JavaBean. Like ActiveX controls, JavaBean provides known functions and is designed for reuse at any time.
The value of Javabean is that it can be used through a set of features that provide access to the JavaBean settings. For example, this person is a JavaBean, and his name, Social Welfare Security number, and address can be characteristic. For JSP websites, you basically dynamically connect 'java' to your website.
Suppose that Javabean is created before the website is created. The first thing you need to do is to tell the JSP page that it needs to Use Javabean. this can be done by using the <JSP: usebean> volume label: <JSP: usebean id = "localname" class = "com. jguru. person "Scope =" application "/>.
<JSP: usebean> you must use the ID attribute to identify beans. here, you provide a name for the JSP webpage to identify beans. In addition to the ID attribute, you must also tell the webpage where to find the beans, or its Java category name. The category attribute provides how to locate it in various methods. The last required component is the scope attribute. with the help of the range attribute, You Can Tell Beans to ask for a single webpage (preset) [scope = "page"]; for a requested webpage [scope = "request"]; for the call period [scope = "session"]; or [scope = "application"] for the entire application to maintain its own information. for the call period range, you can easily maintain items on the JSP page, such as the shopping cart.
Once you declare a JavaBean, you can access its features to define it. To obtain a specific value, use the <JSP: getproperty> volume tag. With the <JSP: getproperty> label, you can specify the name of the bean to be used (from the ID field of usebean) and the value acquisition feature. Then, the real value is placed in the output: <JSP: getproperty id = "localname" property = "name"/>.
To change the JavaBean feature, you need to use the <JSP: setproperty> volume label. you also need to identify beans and the features to be corrected for this scale. In addition, you need to provide new values. if the name is correct, these can be obtained directly in the specified format: <JSP: setproperty id = "localname" property = "*"/>;
To obtain a parameter, you must directly name this feature and the parameter: <JSP: setproperty id = "localname" property = "Address" Param = "parametername"/>;
Or directly set the parameter by name and value: <JSP: setproperty id = "localname" property = "serialnumber" value = "string"/> or <JSP: setproperty id = "localname" property = "serialnumber" value = <% = expression %>/>.
Last point about JavaBean: To enable the network server to find JavaBean, you need to put their category files in a special location. For jswdk, the simplest thing is the class directory in the installation directory, such as the jswdk-1.0.1classes.
Internal JSP objects
The last component related to JSP syntax is called an internal object. In a small JSP instruction file, you can access these internal objects to interact with the servlet environment that executes JSP pages. Access to many internal objects should be simplified. However, these are examples, and their access is acceptable. To fully utilize internal object settings, you need to understand the latest Java Servlet API.
The following table lists the internal objects you can use.
Internal Object Description
Request client request, which contains parameters from the get/POST request
Response returned from the response webpage to the client
The pagecontext page is managed here.
Session related to the request
Content being executed by application Servlet
Output stream used to send response
Config servlet architecture object
Page JSP page itself
Exception for the error webpage, exceptions not captured
So what are these, and how should you use them? Basically, in your small instruction file, you can use them to access the servlet that executes JSP code. To avoid talking about too many details about servlet APIs, let's examine what you can do with them:
Without expressions, you can directly access the internal out object to print something to response:
<% Out. println ("hello"); %>.
You do not need to directly transmit parameters to JavaBean. You can obtain the parameter values through the request object:
<% String name = request. getparameter ("name"); Out. println (name); %>.
After you write a lot of applications using JSP, If you create a JavaBeans or find that you put too many original Java codes into your JSP file, you need to create a supported Java category to encourage repeated use and reduce the time required for JSP page conversion. To create a Java class, you must:
Add the in directory of the jdswk installation directory to your path. Add C: 1.2.2in; at the end of the path line of your autoexec. BAT file ;.
Run the following command to copy the JAR file to the jrelibext directory:
Copy C: jswdk-1.0.1libservlet.jar C: jdk1.2.2jrelibext.