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>