Determine whether the current user has the permission to access the webpage

Source: Internet
Author: User
Tags tld

Function: checks whether the current user has the permission to access the webpage.
Idea: Read the permissions of the current user (set during login verification)
Compare with the value obtained by the tag on the JSP page. If the tag does not match, the page jumps to the specified page.
Summary: Tag classes are generally stored in Web-INF/lib.
XXXX. TLD configuration label class Parameter definition and specified class name
Web-inf.xml specifies the location and definition name of xxxx. TLD
<Mytag: checkpower power = "manager" gotopage = "login. jsp"/>
Error Summary: The get/set method is missing when writing the label class, and an error is returned.
When writing a JSP page, ctrl c ctrl v and the URI is not changed. The page is not recognized.
Purpose of the next step: Write the verification error information to the session or request when the label class is verified.
Then the error message can be read through the JSP page.
    
    
============================= Checkpower. class (Label class program) ============================================
Package com. mytld;
    
Import java. Io. ioexception;
    
Import javax. servlet. jsp. jspexception;
Import javax. servlet. jsp. tagext. tagsupport;
    
Public class checkpower extends tagsupport {
    
Private string power;
    
Private string gotopage;
    
Public int doendtag () throws jspexception {
String userpower = (string) pagecontext. getsession (). getattribute (
"Power ");
Try {
If (! Power. Equals (userpower )){
Pagecontext. Forward (gotopage );
Return super. eval_page;
}
} Catch (exception e ){
E. printstacktrace ();
}
    
Return super. eval_page;
// Return Super. skip_body;
// Return Super. skip_page;
}
    
Public String getgotopage (){
Return gotopage;
}
    
Public void setgotopage (string gotopage ){
This. gotopage = gotopage;
}
    
Public String getpower (){
Return power;
}
    
Public void setpower (string power ){
This. Power = power;
}
    
}
===================== Manager. JSP ========================================
<% @ Page pageencoding = "GBK" contenttype = "text/html; charset = GBK" %>
<% @ Taglib uri = "/tags/Struts-bean" prefix = "Bean" %>
<% @ Taglib uri = "/tags/Struts-logic" prefix = "logic" %>
<% @ Taglib uri = "mytag" prefix = "mytag" %>
<HTML>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = GBK"/>
<Title> Manager </title>
</Head>
<Body>
<Center> Manager </center>
<Mytag: checkpower power = "manager" gotopage = "login. jsp"/>
<Logic: present name = "loginbean" Scope = "request">
<Bean: write name = "loginbean" property = "password"/>
</Logic: Present>
</Body>
</Html>
    
==================== Mytag. TLD =======================================
<? XML version = "1.0" encoding = "shift_jis"?>
<Taglib>
<Tlibversion> 1.2 </tlibversion>
<Jspversion> 1.1 </jspversion>
<Shortname> mytag </shortname>
    
<Tag>
<Name> checkpower </Name>
<Tagclass> com. mytld. checkpower </tagclass>
<Attribute>
<Name> power </Name>
<Required> true </required>
<Rtexprvalue> true </rtexprvalue>
</Attribute>
    
<Attribute>
<Name> gotopage </Name>
<Required> true </required>
<Rtexprvalue> true </rtexprvalue>
</Attribute>
    
</Tag>
    
</Taglib>
    
    
========================== Web. XML (segment) ==============================
<Taglib>
<Taglib-Uri> mytag </taglib-Uri>
<Taglib-location>/WEB-INF/mytag. TLD </taglib-location>
</Taglib>

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.