1 16 package dlog4j.tags; 17 18 import java.io.BufferedReader ; 19 import java.io.IOException ; 20 import java.io.InputStream ; 21 import java.io.InputStreamReader ; 22 import java.text.MessageFormat ; 23 24 import javax.servlet.ServletContext ; 25 import javax.servlet.jsp.JspException ; 26 import javax.servlet.jsp.tagext.TagSupport ; 27 28 import dlog4j.SiteManager; 29 import dlog4j.formbean.SiteForm; 30 import dlog4j.formbean.UserForm; 31 import dlog4j.util.mail.Mailer; 32 33 37 public class SendPasswordTag extends TagSupport { 38 39 String resultId = "result"; 40 String user; 41 String title = "DLOG4j密码提示"; 42 43 public int doStartTag() throws JspException { 44 UserForm userForm = (UserForm)pageContext.findAttribute(user); 45 if(userForm==null) 46 throw new JspException ("cannot find user attribute."); 47 48 try { 49 SiteForm site = SiteManager.getCurrentSite(pageContext.getRequest()); 50 String [] args = new String []{userForm.getLoginName(), 51 userForm.getPassword(), 52 site.getUrl(), 53 site.getDisplayName()}; 54 String template = getContentTemplate(); 55 String content = MessageFormat.format(template, args); 56 Mailer mailer = Mailer.getMailer(); 57 mailer.send(site.getDisplayName(), 58 new String []{userForm.getEmail()}, 59 title, 60 content); 61 pageContext.setAttribute(resultId,"SENT"); 62 } catch (Exception e) { 63 pageContext.getServletContext().log("发送密码提示失败",e); 64 pageContext.setAttribute(resultId,e); 65 } 66 return SKIP_BODY; 67 } 68 69 74 protected String getContentTemplate() throws IOException { 75 ServletContext sc = pageContext.getServletContext(); 76 InputStream in = sc.getResourceAsStream("/WEB-INF/template/password_tip.html"); 77 StringBuffer template = new StringBuffer (512); 78 BufferedReader reader = null; 79 try{ 80 reader = new BufferedReader (new InputStreamReader (in)); 81 do{ 82 String line = reader.readLine(); 83 if(line==null) 84 break; 85 template.append(line); 86 template.append("\r\n"); 87 }while(true); 88 }finally{ 89 in.close(); 90 } 91 return template.toString(); 92 } 93 94 public String getUser() { 95 return user; 96 } 97 public void setUser(String user) { 98 this.user = user; 99 } 100 public String getTitle() { 101 return title; 102 } 103 public void setTitle(String title) { 104 this.title = title; 105 } 106 public String getResultId() { 107 return resultId; 108 } 109 public void setResultId(String resultId) { 110 this.resultId = resultId; 111 } 112 } 113 | Popular Tags |