KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > dlog4j > tags > SendPasswordTag


1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 2 of the License, or
5  * (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU Library General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15  */

16 package dlog4j.tags;
17
18 import java.io.BufferedReader JavaDoc;
19 import java.io.IOException JavaDoc;
20 import java.io.InputStream JavaDoc;
21 import java.io.InputStreamReader JavaDoc;
22 import java.text.MessageFormat JavaDoc;
23
24 import javax.servlet.ServletContext JavaDoc;
25 import javax.servlet.jsp.JspException JavaDoc;
26 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
27
28 import dlog4j.SiteManager;
29 import dlog4j.formbean.SiteForm;
30 import dlog4j.formbean.UserForm;
31 import dlog4j.util.mail.Mailer;
32
33 /**
34  * 用于发送密码到用户邮箱的标签
35  * @author liudong
36  */

37 public class SendPasswordTag extends TagSupport JavaDoc {
38     
39     String JavaDoc resultId = "result";
40     String JavaDoc user;
41     String JavaDoc title = "DLOG4j密码提示";
42
43     public int doStartTag() throws JspException JavaDoc {
44         UserForm userForm = (UserForm)pageContext.findAttribute(user);
45         if(userForm==null)
46             throw new JspException JavaDoc("cannot find user attribute.");
47         
48         try {
49             SiteForm site = SiteManager.getCurrentSite(pageContext.getRequest());
50             String JavaDoc[] args = new String JavaDoc[]{userForm.getLoginName(),
51                                          userForm.getPassword(),
52                                          site.getUrl(),
53                                          site.getDisplayName()};
54             String JavaDoc template = getContentTemplate();
55             String JavaDoc content = MessageFormat.format(template, args);
56             Mailer mailer = Mailer.getMailer();
57             mailer.send(site.getDisplayName(),
58                         new String JavaDoc[]{userForm.getEmail()},
59                         title,
60                         content);
61             pageContext.setAttribute(resultId,"SENT");
62         } catch (Exception JavaDoc e) {
63             pageContext.getServletContext().log("发送密码提示失败",e);
64             pageContext.setAttribute(resultId,e);
65         }
66         return SKIP_BODY;
67     }
68     
69     /**
70      * 获得忘记密码提示内容的模板
71      * @return
72      * @throws IOException
73      */

74     protected String JavaDoc getContentTemplate() throws IOException JavaDoc{
75         ServletContext JavaDoc sc = pageContext.getServletContext();
76         InputStream JavaDoc in = sc.getResourceAsStream("/WEB-INF/template/password_tip.html");
77         StringBuffer JavaDoc template = new StringBuffer JavaDoc(512);
78         BufferedReader JavaDoc reader = null;
79         try{
80             reader = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(in));
81             do{
82                 String JavaDoc 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 JavaDoc getUser() {
95         return user;
96     }
97     public void setUser(String JavaDoc user) {
98         this.user = user;
99     }
100     public String JavaDoc getTitle() {
101         return title;
102     }
103     public void setTitle(String JavaDoc title) {
104         this.title = title;
105     }
106     public String JavaDoc getResultId() {
107         return resultId;
108     }
109     public void setResultId(String JavaDoc resultId) {
110         this.resultId = resultId;
111     }
112 }
113
Popular Tags