?? checklogontag.java
字號:
package emptyprj;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpSession;
import org.apache.struts.util.RequestUtils;
import org.apache.struts.util.ResponseUtils;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
import org.apache.struts.config.ModuleConfig;
import org.apache.struts.Globals;
import org.apache.struts.util.MessageResources;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
public final class CheckLogonTag extends TagSupport {
private String role="";
private String name = Constants.USER_KEY;
private String page = "/logon.jsp";
public String getName() {
return (this.name);
}
public void setName(String name) {
this.name = name;
}
public String getPage() {
return (this.page);
}
public void setPage(String page) {
this.page = page;
}
public void setRole(String newrole)
{
role=newrole;
}
public String getRole()
{
return role;
}
public int doStartTag() throws JspException {
return (SKIP_BODY);
}
public int doEndTag() throws JspException {
// Is there a valid user logged on?
boolean valid = false;
HttpSession session = pageContext.getSession();
if ((session != null) && (session.getAttribute(name) != null)) {
String str="null";
String getusername="null";
String getrole="null";
try
{
str=(String)session.getAttribute(name);
getusername=str.substring(0, str.indexOf(";"));
getrole=str.substring(str.indexOf(";")+1,str.length()).trim();
if(CheckRole(role,getrole)) //role是標簽指定的角色,getrole是從數據庫讀取出來的用戶角色。
{
valid = true;
}
}
catch (Exception e) {
valid = false;
}
/*
ResponseUtils.write(pageContext, str);
ResponseUtils.write(pageContext, "<p></p>");
ResponseUtils.write(pageContext, getusername);
ResponseUtils.write(pageContext, "<p></p>");
ResponseUtils.write(pageContext, getrole);
ResponseUtils.write(pageContext, "<p></p>");
ResponseUtils.write(pageContext, role);
*/
}
// return SKIP_BODY;
// Forward control based on the results
if (valid) {
return (EVAL_PAGE);
} else {
ActionErrors errors = new ActionErrors();
errors.add(ActionErrors.GLOBAL_ERROR,
new ActionError("error.password.mismatch"));
pageContext.getRequest().setAttribute(Globals.ERROR_KEY, errors); //保存出錯信息
ModuleConfig config =
(ModuleConfig) pageContext.getServletContext().getAttribute(
org.apache.struts.Globals.MODULE_KEY);
try {
pageContext.forward(config.getPrefix() + page);
} catch (ServletException e) {
throw new JspException(e.toString());
} catch (IOException e) {
throw new JspException(e.toString());
}
return (SKIP_PAGE);
}
}
public void release() {
super.release();
this.name = Constants.USER_KEY;
this.page = "/logon.jsp";
this.role="";
}
public boolean CheckRole(String role,String getrole) //role是標簽指定的角色,getrole是從數據庫讀取出來的用戶角色。
{
getrole=getrole.trim();;
role=role.trim();
//if("".equals(getrole))return false;
String thisrole;
role=";"+role+";";
while(getrole.indexOf(";")>1)
{
thisrole=getrole.substring(0,getrole.indexOf(";")).trim();
getrole=getrole.substring(getrole.indexOf(";")+1,getrole.length()).trim();
thisrole=";"+thisrole+";";
if(role.indexOf(thisrole,0)>-1)
{
return true;
}
}
thisrole=getrole;
thisrole=";"+thisrole+";";
if(role.indexOf(thisrole,0)>-1)
{
return true;
}
return false;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -