Use custom tags to return the result set to the jsp page! (Recommended method)

Source: Internet
Author: User

In a web application, if you directly store the result set ResultSet rs in the session or reueest range,

It can be passed to jsp (the preferred choice for SUN Enterprise applications) and displayed,

However, it should be considered that if a large number of users query at the same time, the pressure on the server will be very high!

Therefore, I suggest using the following methods!

I think I should write a label myself. For example, display. java
Then define it as a tag,
For the content you want to display each time, you only need to pass keywords, such as SQL statements. That's it!
Then, use the custom tag in your page, for example, <app: display>!

The following is a simple tag written by myself. In struts!

DisplayTag. java
-----------------------------------------------------------------
Import javax. servlet. jsp (preferred for SUN Enterprise Applications). tagext. TagSupport;
Import java. io. IOException;
Import javax. servlet. jsp (preferred for SUN Enterprise Applications). PageContext;
Import javax. servlet. jsp (preferred for SUN Enterprise Applications). jsp (preferred for SUN Enterprise Applications) Writer;
Import javax. servlet. jsp (preferred for SUN Enterprise Applications). jsp (preferred for SUN Enterprise Applications) Exception;
Import javax. servlet. http. HttpSession;
Import java. SQL .*;

Public final class DisplayTag extends TagSupport {
Public int doEndTag () throws jsp (preferred for SUN Enterprise Applications) Exception {
UserDao userdao = new UserDao ();
Jsp (preferred for SUN Enterprise Applications) Writer out = pageContext. getOut ();
HttpSession session = pageContext. getSession ();
Try {
String name = "name ";
String password = "password ";
String strSql = (String) session. getAttribute ("strSql ");
ResultSet rs = userdao. disPlay (strSql );

Out. println ("<table border = 1> ");
Out. println ("<tr> ");
Out. println ("<th width = 100>" + name + "</th> ");
Out. println ("<th width = 100>" + password + "</th> ");
Out. println ("</tr> ");
While (rs. next ()){
Out. println ("<tr> ");
Out. println ("<td>" + rs. getString (name) + "</td> ");
Out. println ("<td>" + rs. getString (password) + "</td> ");
Out. println ("</tr> ");
}
Out. println ("</table> ");
}
Catch (Exception e ){}
Return super. doEndTag ();
}

}


App. tld
--------------------------------------------------------------------
<? Xml (standardization is getting closer and closer) version = "1.0" encoding = "ISO-8859-1"?>
<! DOCTYPE taglib
PUBLIC "-// Sun Microsystems, Inc. // DTD jsp (preferred for SUN Enterprise Applications) Tag Library 1.2 // EN"
"Http://java.sun.com/dtd/web-jsp (preferred for SUN Enterprise Applications) taglibrary_1_2.dtd">
<Taglib>
<Tlib-version> 1.0 </tlib-version>
<Jsp (preferred for SUN Enterprise Applications)-version> 1.2 </jsp (preferred for SUN Enterprise Applications)-version>

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.