天天看點

jsp 通過cookie 記住使用者名

具體思路:

登入界面如下:

jsp 通過cookie 記住使用者名

 在背景(servlet 或struts 的action)中進行判斷,若登入成功則通過httpservletresponse 添加一個cookie.

在登入的jsp頁面中,通過request 擷取cookie數組,然後周遊,若找到相應的cookie,則把cookie的value設定到表單的對應文本框中.

具體代碼如下:

登入的jsp頁面中(核心代碼):

jsp 通過cookie 記住使用者名

<body>  

<%  

cookie[] cookies = request.getcookies();  

string username33="";  

if (cookies != null) {  

    for (cookie c : cookies) {  

        /*if ("password22".equals(c.getname())) {  

            user.setpassword(urldecoder.decode(c.getvalue(), "utf-8"));  

            continue;  

        }*/  

        if ("useremail".equals(c.getname())) {  

            username33=urldecoder.decode(c.getvalue(), "utf-8");  

            break;  

        }  

    }  

}  

%>  

<script type="text/javascript">  

window.onload=function(){  

var username1='<%=username33 %>';  

//alert("username1:"+username1);  

if(username1){  

    if(username1!='' && username1!=null &&username1!=undefined){  

        document.getelementsbyname("user.username")[0].value=username1;  

</script>  

...  

</body>  

 背景的action:

jsp 通過cookie 記住使用者名

if (islogin) {  

            //儲存使用者名(目前是郵箱)  

                string emaicookiename = "useremail";  

                httpservletrequest request=servletactioncontext.getrequest();  

                cookie[] cookies = request.getcookies();  

                boolean flag = false;  

                cookie emailcook = null;  

                if (cookies != null)  

                {  

                    system.out.println("cookie 不為空");  

                    for (cookie c : cookies)  

                    {  

                        if (emaicookiename.equals(c.getname()))  

                        {  

                            system.out.println("找到了 "+emaicookiename);  

                            system.out.println("cookie的值為 "+c.getvalue());  

                            try {  

                                c.setvalue(urlencoder.encode(this.user.getusername(), "utf-8"));  

                            } catch (unsupportedencodingexception e) {  

                                e.printstacktrace();  

                            }  

                            emailcook = c;  

                            flag = true;  

                            break;  

                        }  

                    }  

                }  

                if (!flag)  

                    system.out.println("沒有找到 "+emaicookiename);  

                    try {  

                        emailcook = new cookie(emaicookiename, urlencoder.encode(  

                                this.user.getusername(), "utf-8"));  

                    } catch (unsupportedencodingexception e) {  

                        e.printstacktrace();  

                    if (null != emailcook)  

                         httpservletresponse response=servletactioncontext.getresponse();  

                        if(!valuewidget.isnullorempty(issave) && issave.equalsignorecase("save")){  

                            emailcook.setmaxage(10000000);  

                            response.addcookie(emailcook);  

                            system.out.println("儲存cookie");  

                         }else{  

                                system.out.println("讓 cookie 失效");  

                                emailcook.setmaxage(0);  

                                response.addcookie(emailcook);  

                         }  

            return action.success;  

 源代碼見附件

說明:

該項目使用maven 建構;

ide:eclipse

登入位址:http://localhost:8080/shop_goods/user/logininput.action