JSP (11) Tag Library

Source: Internet
Author: User
Tags exception handling tostring tld stringbuffer
JS JSP 1.1 presents a method of extending the JSP flag, which is named "Logo Library". These libraries allow other flags similar to those of Jsp:include or jsp:forward, but they are not JSP: prefix and also have some attributes attached to the attachment.

To introduce the logo library, we use the Blazix tag library as an example in this tutorial. This logo library can only be used in Blazix servers, which you can download for free.

Each logo library has its own logo library special document. In order to use the logo library, you can use the "taglib" directive to indicate where the logo library "description" is located. For the Blazix logo library, the directive is as follows:

<%@ taglib prefix= "BLX" uri= "/blx.tld"%>

The "uri" in the above sentence indicates where the logo library description is located. The prefix for the tag library is different. This instruction means that we can use the BLX: to use the flags in the library. The Blazix logo Library provides a blx:getproperty flag. This flag allows the user to edit the form data. Now, in the getname.jsp file, we add a jsp:usebean and place the form in Blx:getproperty:

<%@ taglib prefix= "BLX" uri= "/blx.tld"%>


<jsp:usebean id= "user" class= "UserData" scope= "Session"/>



<HTML>



<BODY>



<blx:getproperty name= "User" property= "*" >



<form method=post action= "savename.jsp" >



What ' s your name? <input Type=text Name=username size=20><br>



What ' s your e-mail address? <input Type=text Name=email size=20><br>



What ' s your age? <input Type=text name=age size=4>



<p><input type=submit>



</FORM>



</blx:getProperty>



</BODY>



</HTML>

From the above code we can find that blx:getproperty not end with/>, but to end it. This statement enters all form input areas into the blx:getproperty so that they can be modified correctly by the tag library.

Then create a connection from getname.jsp to nextpage.jsp, and you'll find the bean's data automatically displayed in the input area. So now the user can edit the data.

At this point, we still have some questions that have not been clarified. First, the user cannot clear the name area, and if the user types an incorrect content in the age area (not an integer value for an effect), the Java exception error occurs.
Now we're going to use the other flags in the Blazix tag library to solve this problem. Blazie provides a blx:setproperty flag to solve this problem. Blx:setproperty allows us to define an exception handling method. If an unexpected error occurs, we can collect an error message for the user and continue processing.
Here is our modified savename.jsp, which can handle any errors. Not only can it display getname.jsp and can type the correct data on it, but it is automatically connected to nextpage.jsp:

<%@ taglib prefix= "BLX" uri= "/blx.tld"%>


<%!



Boolean haveerror;



StringBuffer errors;





public void ErrorHandler (String field,



String value,



Exception ex)



{



Haveerror = true;



if (errors = null)



Errors = new StringBuffer ();



Else



Errors.append ("<P>");



Errors.append ("<p>value for field \" +



Field + "\" is invalid. ");



if (ex instanceof Java.lang.NumberFormatException)



Errors.append ("The value must be a number.");



}



%>



<%



Variables must be initialized outside declaration!



Haveerror = false;



Errors = null;



%>



---------------



<BODY>



<jsp:usebean id= "user" class= "UserData" scope= "Session"/>



<blx:setproperty name= "User"



property= "*"



Onerror= "ErrorHandler"/>



<%



if (haveerror) {



Out.println (Errors.tostring ());



Pagecontext.include ("getname.jsp");



} else



Pagecontext.forward ("nextpage.jsp");



%>



</BODY>



</HTML>



Note here that Haveerror and errors must be reinitialized each time because they are initialized outside of the declaration.

Now, if an error occurs in the process of blx:setproperty processing, we will display this error and return to getname.jsp again so that the user can correct the error. If no errors occur, we automatically let the user connect to nextpage.jsp.
If you're more careful, you can also see that the form still has a problem where "age" starts showing empty instead of 0. This problem can be achieved by adding "emptyint=0" to the Blx:getproperty and Blx:setproperty flags so that the bean area is initialized to 0. If the age you type is not a valid number, then we use 0来 to flag the null character.

Another small problem is that if there are errors and our old books contain "getname.jsp", we get Two "<HTML>" signs. A perfect solution to this problem is to remove the OUT.PRINTLN and pass the error as follows:

<%

if (haveerror) {

Request.setattribute ("Errors",

Errors.tostring ());

Pagecontext.forward ("getname.jsp");

} else

Pagecontext.forward ("nextpage.jsp");

%>

Then we can execute "request.getattribute" in getname.jsp, and if the return value is Non-null, an error is displayed. Let's keep this as an exercise in this section of the tutorial.

Finally, let's talk about the practice of this section, first of all, what you just said. Also read the SQL flags in the logo library document, and then create a database table, then write a JSP file with a form and a selection box on the form, and then use the SQL flag to make a selection box from the database table

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.