羅比特

          學習筆記

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            2 隨筆 :: 3 文章 :: 0 評論 :: 0 Trackbacks

          2008年7月20日 #

          這里先介紹用Java Mail 類實現發送郵件,然后再介紹Commons Email組件實現發送簡單郵件和帶附件的郵件

          首先到官網上下載 Java Mail 1.4.1
          http://java.sun.com/products/javamail/index.html
          添加mail.jar到用戶庫中, 然后構建郵件并發送
          impoet javax.mail.*;
          Properties props
          =new Properties();
          Session session
          =Session.getInstance(props,null);
          props.put(
          "mail.host","127.0.0.1");
          props.put(
          "mail.transport.protocol","smtp");
          Message message
          =new MimeMessage(session);
          //message構建郵件內容,因為Message是抽象類,所以這里用它的子類MimeMessage
          message.setForm(new InternetAddress("abc@163.com"));
          message.setRecipient(Message.RecipientType.To,
          new InternetAddress("123@163.com"));
          message.setSubject(
          "Hello");
          message.setText(
          "I love java");
          Transport.send(message);

          Commons Email組件很好的封裝了Java Mail 類,用起來更加方便,功能更強大
          下載該組件,把commons-email-1.1.jar 加載到該應用中
          用Servlet處理發郵件,在doPost()方法中添加如下代碼:
          request.setCharacterEnconding("UTF-8");
          SimpleEmail email
          =new SimpleEmail();
          email.setHostName(
          "smtp.sina.com");
          email.setAuthentication(
          "username","password");
          //這里設定一下郵件內容編碼格式
          email.setCharset("UTF-8");
          //request.getParameter()從提交的表單中獲取信息
          email.setFrom(request.getParameter("from"));
          email.addTo(request.getParameter(
          "to"));
          email.setSubjet(request.getPatameter(
          "subject"));
          email.setMsg(request.getParameter(
          "content"));
          email.send();

          下面介紹帶附件的郵件如何編寫,實際上就是把上述兩部分結合。
          首先,表單里要有"file"域,在表單標簽里添加 <Form enctype="multipart/form-data">
          寫一個方法getFile(),返回file類型,具體的內容請參加File Upload 那篇文章,先把文件存在web server的一個目錄上,然后再發送
          所不同的是,這次,構建一個MultiPartEmail email=new MultiPartEmail();
          然后按上述代碼添加好主題,內容等,需要注意的是,在getFile()中獲取文件的時候,已經獲取了表單域的信息,把這些信息存在一個HashMap中,然后通過email對象的方法添加這些信息時,直接從HashMap中獲得即可
          通過getFile()獲得文件,然后
          if(file!=null)
          {
            EmailAttachment att
          =new EmailAttachment();
            att.setPath(file.getPath());
            att.setDisposition(EmailAttachment.ATTACHMENT);
          //這里是附件的類型
           att.setName(file.getName());
            email.addattach(att);
          }

          posted @ 2008-07-20 05:06 羅比特 閱讀(173) | 評論 (0)編輯 收藏

            在Web應用中,實現文件上傳,可以用這個組件:Commons FileUpload
            先到Apache官網下載這個組件,并將Commons-Fileupload.jar 和 Commons-io.jar加到用戶庫中,并添加到本應用中。
           
            在Servlet 添加一個Init()方法,用來接收一些初始參數,代碼如下:
           
          public ServletContext sc;
          public String savePath;
          public void init(ServletConfig config)
          {
            config.getInitParameter(
          "savePath");
            sc
          =config.getServletContext();
          }
             在doPost()方法中,添加代碼:
          DiskFileItemFactory factoty=new DiskFileItemFactory();
          ServletFileUpload upload
          =new ServletFileUpload(factory);
          以下需要try
          -catch一下
          List Items
          =upload.parseRequest(request);
          Iterator it
          =items.iterator();
          while(it.hasNext())
          {
            FileItem item
          =(FileItem)it.next();
            
          if(item.isFormField())
              {  
                item.getFiledName();
                item.getString(
          "UTF-8")
              }
            
          else
              {
                 
          if(item.getName()!=null&&item.getName().equals(""))
                   {
                     File temp
          =new File(item.getName());
                     File file
          =new File(sc.getRealPath("/")+savePath,temp.getName());
                     item.write(file);
                   }
                  
              }
          }
          posted @ 2008-07-20 04:30 羅比特 閱讀(116) | 評論 (0)編輯 收藏

          2008年7月15日 #

           FCKEditor之一款類似插件的東西,一般Web開發中,需要留言的地方,經常會使用該插件。功能很強大
           首先要下載FCKEditor, http://www.fckeditor.net/ ,并拷到當前自己的Web 應用當中
            
          <script type="text/javascript" src="fckeditor/fckeditor.js"></script>

          <script type="text/javascript">
              var oFCKeditor 
          = new FCKeditor('FCKeditor1');
              oFCKeditor.BasePath    
          = '/webproject12/fckeditor/';
              var sSkin;
              sSkin 
          = "office2003";
              var sSkinPath 
          = oFCKeditor.BasePath + 'editor/skins/' + sSkin + '/';
                      
              oFCKeditor.Config[
          'SkinPath'= sSkinPath;

              oFCKeditor.Config[
          'PreloadImages'=
                          
                  sSkinPath 
          + 'images/toolbar.start.gif' + ';' +
                                  sSkinPath 
          + 'images/toolbar.end.gif' + ';' +
                                  sSkinPath 
          + 'images/toolbar.bg.gif' + ';' +
                                  sSkinPath 
          + 'images/toolbar.buttonarrow.gif' ;
                      
              oFCKeditor.Create();
          </script>

           上述代碼引自他人博客,羅比特實在是懶得打了,又發現寫的如此工整的代碼,嘿嘿,特此說明一下。
          posted @ 2008-07-15 20:01 羅比特 閱讀(125) | 評論 (0)編輯 收藏

            Servlet過濾器可以過濾瀏覽器和Servlet之間的內容。 主要用途有:
           1.用戶認證和授權管理
           2.統計Web的訪問量
           3.實現Web應用的日志的功能
           4.數據壓縮和加密
           5.Xml文件轉換到XSLT文件

          實現過濾器的方法就是實現一個Java類,這個Java類要是實現javax.servlet.Filter接口,并配置web.xml文件
          在Java類中要實現doFilter() 和 init() 方法。

          在web.xml中添加以下配置:
          <filter>
            
          <filter-name>EncodingFilter</filter-name>
            
          <filter-class>com.tutu.EncodingFilter<filter-class>
            
          /*這里可以添加初始參數,在Init()方法里通過config對象得到,例如:程序應轉向的頁面*/
            
          <init-parma>
              
          <parma-name>url<parma-name>
              
          <parma-value>login.jsp<parma-value>
            
          </init-parma>
          </filter>
          <filter-mapping>
            
          <filter-name>EncodingFilter</filter-name>
            
          //這里指需要被過濾得頁面
            <url-pattern>/admin/secure</url-pattern>
          </filter-mapping>
          posted @ 2008-07-15 19:55 羅比特 閱讀(110) | 評論 (0)編輯 收藏

          2008年7月14日 #

           這是Apache公司出的一個工具集,用于方便Web開發中關于數據庫的操作。最大的好處就是,在SQL查詢的時候,可以返回一個MapList 或者BeanList對象。
           首先到官網下載相關包 http://commons.apache.org/downloads/download_dbutils.cgi  下載后,引用commons-dbutils.jar 包到項目中

          String url="jdbc:oracle:thin:@192.168.1.101:1521:ora9";
          String sql
          ="select id,name,phone,email from guestbook order by id desc";
          DbUtils.loadDriver(
          "oracle.jdbc.driver.OracleDriver");
          try
           
          {
                       Connection conn
          =DriverManager.getConnection(url,"scott","tiger");
                       QueryRunner qr 
          = new QueryRunner();
                       List result
          =(List)qr.query(conn,sql,new MapListHandler());
                       DbUtils.close(conn);

           }
             
               在qr.query()這個函數中,還可以選擇一個BeanList 對象 
           
          List results=(List)qr.query(conn,sql,new BeanListHandler(Guestbook.class));

              這樣的好處是可以直接得到javaBean 的對象,把它直接保存在request 對象中,方便顯示層的jsp 頁面調用
          request.setAttribute("BeanName",BeanList)

              同時,這里也可以用數據連結池實現數據庫的連接
           
          String sql="insert into guestbook(id,name,email) values (gb_seq.nextval,?,?)";
          String[] param 
          = {"abc","abc@163.com"};

          Context context 
          = new InitialContext();
          DataSource ds 
          = context.lookup("java:/comp/env/jdbc/oracleds"); 
          QueryRunner qr
          =new QueryRunner(ds);
          qr.update(sql,param);
            
          posted @ 2008-07-14 21:39 羅比特 閱讀(742) | 評論 (0)編輯 收藏

          僅列出標題  
          主站蜘蛛池模板: 德化县| 九龙坡区| 清丰县| 图们市| 象州县| 浮梁县| 邯郸市| 海林市| 蕉岭县| 民丰县| 敖汉旗| 镇江市| 洞头县| 海林市| 双城市| 乌拉特后旗| 建平县| 浮山县| 金坛市| 淮北市| 孟连| 礼泉县| 陆河县| 遂昌县| 邯郸市| 桂东县| 巴林右旗| 桃园县| 西昌市| 德化县| 油尖旺区| 新兴县| 新宾| 开原市| 双流县| 河西区| 阿尔山市| 绵阳市| 昭苏县| 崇礼县| 射阳县|