Jsp custom tag-3

Source: Internet
Author: User


1. Tag case-developing anti-leech labels

Leeching refers to the content that service providers do not provide services themselves. They use technical means to bypass the end user interfaces (such as advertisements) of other interests ), provide the service content of other service providers directly to end users on their own websites to defraud end users of browsing and clicking rates. Beneficiaries do not provide resources or provide few resources, but real service providers do not receive any benefits.

One solution -- restrict reference pages

The anti-leech principle is that the server obtains the website address of the user's submitted information, and then compares it with the address of the real server. If it is consistent, it indicates that the website is submitted or submitted for the site that you trust, otherwise, it is considered as leeching.

Objective: To develop tags

<Class3g: referer site = "http: // drinkeye: 8080" page = "/index. jsp"/>

Site: trusted site, only site requests allowed

Page: The correct link page, which is automatically transferred to this page after leeching is found


1) Tag processing class


<Span style = "font-size: 16px;"> public void doTag () throws JspException, IOException {


PageContext pageContext = (PageContext) this. getJspContext ();


HttpServletRequest request =


(HttpServletRequest) pageContext. getRequest ();


HttpServletResponse response =


(HttpServletResponse) pageContext. getResponse ();



String referer = request. getHeader ("referer ");


System. out. println (request. getContextPath ());



If (referer = null |! Referer. startsWith (site )){


// Determine whether leeching is successful





<Span style = "font-size: 16px;"> // Based on the page property value, link redirection refers to the correct page for accessing the leeching content


String contextPath = request. getContextPath ();


If (page. startsWith (contextPath )){


Response. sendRedirect (page );


} Else if (page. startsWith ("/")){


Response. sendRedirect (contextPath + page );


} Else {


Response. sendRedirect (contextPath + "/" + page );




Throw new SkipPageException ();









2) description file

3) use tags on the Content Page

2. Tag case-<c: if> tag

Tag function:


<Span style = "font-size: 16px;"> <class3g: if exp = "$ {psw = null}">


User = null <br>


</Class3g: if>





Session. setAttribute ("user", "Tom ");




<Class3g: if exp = "$ {user! = Null} ">


User! = Null <br>


</Class3g: if>


Processing class


<Span style = "font-size: 16px;"> public class MyIfTag extends SimpleTagSupport {



Private boolean exp;



Public void setExp (boolean exp ){


This. exp = exp;





Public void doTag () throws JspException, IOException {


If (exp ){


JspFragment jf = this. getJspBody ();


Jf. invoke (null );









3. Tag case if else tag


<Span style = "font-size: 16px;"> <class3g: choose>


<Class3g: when exp = "$ {user! = Null} ">




</Class3g: when>


<Class3g: otherwise>




</Class3g: otherwise>


</Class3g: choose>






L write the choose tag without attributes, but there is a member invoked. You need to write the setter and getter methods for it. Note that the TAG body needs to be output in the doTag.

L write the child tag when tag with the boolean attribute exp,

L write the sub-tag otherwise tag, which has no attributes and no member variables.


From the path of the mouse programmer

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.