Use a custom label to return the result set to the JSP page! (Recommended method)

Source: Internet
Author: User
Tags query tld
js| page
In Web applications, if the result set is ResultSet RS directly, stored in the session, or Reueest range,

You can pass it to the JSP and show that the

But to take into account, if the query at the same time a lot of users, the server pressure is very large!

So I suggest using the following methods!

I think I should write a label myself. For example, Display.java will then be defined as the label, for each time you want to display the content, in fact, you just pass the keyword, such as: SQL statements. You can do it! Then use the custom tags in your page: For example: <app:display> can be achieved!

Here is a very simple label I wrote, in struts!

Displaytag.java-----------------------------------------------------------------Import Javax.servlet.jsp.tagext.tagsupport;import Java.io.ioexception;import Javax.servlet.jsp.pagecontext;import Javax.servlet.jsp.jspwriter;import Javax.servlet.jsp.jspexception;import Javax.servlet.http.httpsession;import java.sql.*;

Public final class Displaytag extends TagSupport {public int doendtag () throws jspexception{Userdao userdao=new User    Dao ();    JspWriter 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 version= "1.0" encoding= " Iso-8859-1 "? ><! DOCTYPE taglib Public "-//sun Microsystems, Inc.//dtd JSP Tag Library 1.2//en" "Http://java.sun.com/dtd/web-jsptaglibrar Y_1_2.dtd "><taglib> <tlib-version>1.0</tlib-version> <jsp-version>1.2</jsp-version > <short-name>application Tag library</short-name> <tag> <name>display</name> ; Tag-class>netregister. Displaytag</tag-class> <body-content>empty</body-content> </tag></taglib>

Web.xml---------------------------------------------------------------Add the following: <taglib> <taglib-uri>/ Web-inf/app.tld</taglib-uri> <taglib-location>/WEB-INF/app.tld</taglib-location> </taglib >

Displayaction.do------------------------------------------------------------------string Strsql=new string ("    SELECT * from Yonghu ");    HttpSession session=httpservletrequest.getsession ();    Session.setattribute ("strSQL", strSQL); Return Actionmapping.findforward ("Success");

If not with struts, you can achieve this step by other means!

display.jsp-----------------------------------------------------------------<%@ page contenttype= "text/html; CHARSET=GBK "%><%@ taglib uri=/web-inf/app.tld" prefix= "app"%>


In this way, in each of your JSP, as long as you want to output query results, only need to call <app:display/> on it! The above code may not be exactly right, but that's the idea! And in the big project, very respected! It embodies the idea of Java code reuse, but also make the result set of related operations not directly exposed to users, but also achieve security! For reference!


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.