Thứ Tư, 5 tháng 6, 2013

Login đơn giản với struts




Thư mục web gồm có các lib trên

Thực hiện login đơn giản ,
- Khi nhập username và pasword giống nhau thì đăng nhập thành công và chuyển đến trang success.jsp hiển thị username vừa nhập 
- Ngược lại trả về trang login (index.jsp) 
        
  1. Trong file struts-config.xml ta có:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE struts-config PUBLIC
              "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
              "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">
    <struts-config>
        <form-beans>
            <form-bean name="loginActionForm" type="pack.LoginActionForm"></form-bean>
        </form-beans>
       
        <action-mappings>
            <action path="/login" name="loginActionForm" scope="session" type="pack.LoginAction">
            <forward name="failed" path="/index.jsp"></forward>
            <forward name="success" path="/success.jsp"></forward>
            </action>
          
        </action-mappings>   
    </struts-config>
  2. File index.jsp
    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%>
         <%@taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>login</title>
    </head>
    <body>
    <center>
    <h2>Login</h2>
    <html:form action="/login">
    User name: <html:text property="usern" name="loginActionForm"/><br>
    Password : <html:text property="pass" name="loginActionForm"/><br>
    <html:submit/>
    </html:form>
    </center>
    </body>
    </html>
  3. Trang success.jsp

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%>
        <%@taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>

    <h2>Login successful</h2>
    Your user name is : <bean:write property="usern" name="loginActionForm" />
  4. Trong LoginActionForm
    package pack;
    import org.apache.struts.action.ActionForm;

    public class LoginActionForm extends ActionForm {
        private static final long serialVersionUID = 1L;
        private String usern;
        private String pass;

        public String getUsern() {
            return usern;
        }
        public void setUsern(String usern) {
            this.usern = usern;
        }
        public String getPass() {
            return pass;
        }
        public void setPass(String pass) {
            this.pass = pass;
        }
    }
  5. LoginAction
    package pack;

    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    import org.apache.struts.action.Action;
    import org.apache.struts.action.ActionForm;
    import org.apache.struts.action.ActionForward;
    import org.apache.struts.action.ActionMapping;

    public class LoginAction extends Action {
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        // TODO Auto-generated method stub
       
        LoginActionForm frm=(LoginActionForm)form;
        if(frm.getUsern().equals(frm.getPass()) && !frm.getUsern().equals("") && !frm.getPass().equals("")){
        return mapping.findForward("success");
        }else{
            return mapping.findForward("failed");}
       
    }
    }


     

Thứ Ba, 28 tháng 5, 2013

Hàm DAY, MONTH, YEAR trong SQL


Hàm DAY, MONTH, YEAR trả về ngày, tháng, năm

hiển thị tháng sinh của nhân viên với đk là năm sinh > 1960

SELECT LastName, 
MONTH(BirthDate) as 'thang sinh'
FROM dbo.Employees 
WHERE  
YEAR(BirthDate)>1960




SUBSTRING () trong SQL

SUBSTRING ( expression ,start , length )
 
SELECT SUBSTRING(cast(GETDATE() as varchar),1,19)  
 
kết quả: 
 May 28 2013  9:09PM 
 
---> hàm getdate() : lấy ngày/ tháng/ năm giờ :phút: giây hiện tại
CAST() là hàm ép kiểu về kiểu chuỗi varchar,
 SUBSTRING() : lấy chuỗi bắt đầu từ 1 đến ký tự thứ 19

Hàm cộng ngày giờ trong SQL server,

Cộng tháng
 
SELECT DATEADD(month,1,DATEADD(day,1,'8/28/2010'))

kết quả : 2010-09-29 00:00:00.000

SELECT DATEADD(month,-(10/2), SYSDATETIME());

Cộng ngày và giờ
select DATEADD(D,1,DATEADD(HH,1,'2013-05-28 8:45')) as 'gio'

kết quả: 2013-05-29 09:45:00.000

Chủ Nhật, 10 tháng 3, 2013

HÀM TRONG PHP


HÀM TRONG PHP

HÀM XỬ LÝ CHUỖI
1.     Hàm strlen: Đếm số ký tự của chuỗi
strlen:
<? $str = 'abcdef';
   echo strlen($str);
?> <br />
1.     Hàm strpos, stripos: Tìm chuỗi con trong chuỗi mẹ
strpos:
<? $me="abcDabcd";
   $con="d";
   echo strpos($me, $con);
?> <br />
2.     Hàm str_replace, str_ireplace: Thay thế chuỗi con bằng một chuỗi khác
str_replace:
<? $str="ab_AB_ab";
   echo str_replace("ab","xy",$str);
?> <br />
3.     Hàm number_format : Định dạng số
number_format 1234.5678 :
<? $n = 1234.5678;
echo "en = ", number_format($n, 2, '.'',' );
echo "vi = ", number_format($n, 2 , ',' , '.' );
?> <br/>
4.     Hàm strip_tags: Cắt bỏ cc tag html
strip_tags: <? echo strip_tags("<b><i>PHP</i></b>"); ?> <br />
5.     Hàm strstr: Trích chuỗi con
strstr: <? echo strstr("teo@yahoo.com","@");?> <br />
6.     Hàm substr: Trích chuỗi con
substr: <? echo substr("teo@yahoo.com",4,50);?> <br />
7.     Hàm mb_convert_case: Đổi chữ hoa<->thường MB_CASE_LOWER, MB_CASE_UPPER, MB_CASE_TITLE
mb_convert_case:
<? echo mb_convert_case("lập trình ứng dỤng",MB_CASE_TITLE,'utf-8'); ?> <br />
8.     Hàm md5: Mã hóa 1 chuỗi
md5: <? echo md5("123");?> <br />
9.     Hàm sha1: Mã hóa 1 chuỗi
sha1: <? echo sha1("123");?> <br />

HÀM THỜI GIAN
10. Hàm time: Cho giờ hiện hành
time: <? echo time() ?> <br />
11. Hàm checkdate: Kiểm tra ngày hợp lệ hay không
checkdate:
<? echo (checkdate(13,17,2010)==true)? "Ngày hợp lệ":"Ngày không hợp lệ";?>
12. Hàm date: Định dạng ngày
date:<br />
Now:  <? echo date("d/m/Y H:i:s");?> <br />
Thứ trong tuần: <? echo date("w");?> <br />
Tên thứ trong tuần: <? echo date("D") ;?> <br />
Tên thứ trong tuần: <? echo date("l");?> <br />
Tên tháng: <? echo date("M") ;?> <br />
Tên tháng:<? echo date("F");?> <br />
Ngày trong năm: <? echo date("z");?> <br />
13. Hàm strtotime: Đổi chuỗi thành dạng timestamp
strtotime:<br />
Now: <? echo strtotime("now");?> <br />
+1 day: <? echo date("d/m/Y H:i:s", strtotime("+1 day"));?> <br />
+1 week: <? echo date("d/m/Y H:i:s", strtotime("+1 week") );?> <br />
+1 week 2 days 4 hours 2 seconds: <? echo date("d/m/Y H:i:s",strtotime("+1 week 2 days 4 hours 2 seconds")) ?> <br>
next Thursday: <? echo date("d/m/Y", strtotime("next Thursday") )?> <br />
last Monday: <? echo date("d/m/Y", strtotime("last Monday") )?> <br />
10 September 2000: <? echo date("d/m/Y",strtotime("10 September 2000") )?> <br/>

HÀM XỬ LÝ FILE
14. Hàm basename: Lấy tên file
basename:
<? $file = "/php01/hamPHP.php";
   echo basename($file);
?> <br />
15. Hàm filesize: Lấy kích thước file
filesize:
<? $f = "php02.sql";
   echo filesize($f).' bytes';
?> <br />
16. Hàm file_exists: Kiểm tra 1 file có tồn tại hay không
file_exists:
<?  $f = "php02.sql";
    if ( file_exists($f) == true) echo "Có file"; else echo "Không có file";
?> <br/>
17. Hàm fopen, fclose, fread: Mở, đóng, đọc nội dung file text
fopen, fclose, fread:
<? $file = "readme.txt";
   $f = fopen($file,"r+");
   $str = fread($f, filesize($file) );
   fclose($f);echo $str;
?> <br />
18. Hàm unlink: Xóa file
unlink: <? unlink("readme2.txt");?> <br />
19. Hàm mkdir: Tạo folder
mkdir: <? mkdir("hinh/dulich",0777,true);?> <br />
20. Hàm copy: Sao chép file
copy: <? copy("php02.sql", "php02bis.sql") ;?> <br />
21. Hàm rename: Đổi tên file
rename: <? rename("php02bis.sql","abc.sql");?> <br />
22. Hàm file_get_contents: Đọc toàn bộ nội dung 1 file text
file_get_contents:
<? $str = file_get_contents("readme.txt");
   echo $str;
?> <br />

CÁC HÀM KHÁC
23. Hàm isset: Kiểm tra sự tồn tại của 1 biến
isset:
<?php $x=1;
if (isset($x)==true) echo "Có biền x"; else echo "Không có biến x ";
if (isset($y)==true) echo "Có biền y"; else echo "Không có biến y ";
?>
24. Hàm unset: Hủy bỏ 1 biến
unset:
<?php $x=1; $y=2;
unset($x);
if (isset($x)==true) echo "Có biền x"; else echo "Không có biến x ";
if (isset($y)==true) echo "Có biền y"; else echo "Không có biến y ";
?>