jsp製作計數器,自動登錄,定時刷新等簡單小應用

頁面訪問量計數器

<body>

<h1>實現網頁計數器</h1>

<%

 out.print("設置計數值");

Integer c;

if(application.getAttribute("count")==null){

   c=1;

}else{

   c=(Integer.parseInt(application.getAttribute("count").toString()));

}

application.setAttribute("name","zyq");

application.setAttribute("count",c);

out.print("set name = zyq");

out.print("</br>set count ="+c+"</br>");

%>

<ahref="<%=basePath%>jsp/jsp/gateppatter.jsp">計數器頁面</a>

</body>

 

gateppatter.jsp

<body>

計數器頁面

<br>

<%

int c=(Integer.valueOf(application.getAttribute("count").toString()).intValue());  

out.print(c);

application.setAttribute("count",Integer.toString(c+1));

 

%>

</body>

 

自動登錄(用戶名和密碼:)<使用cookie技術>

 

<body>

<%

String welcome="首次訪問" ;

String[] info = new String[]{"",""};

Cookie[] cookie =request.getCookies();

if(cookie!=null){

  

   for(inti=0;i<cookie.length;i++){

     if(cookie[i].getName().equals("zyqcookie")){

        info =cookie[i].getValue().split("#");

     }

   } 

}

 

%>

<formaction ="<%=basePath%>jsp/jsp/valid.jsp"method="post">

      <table>

         <tr><th>用戶名</th>

                <td><inputtype="text"name="name"id="name"value="<%=info[0]%>"/></td>

         </tr>

         <tr><th>密碼</th>

                <td><inputtype="text"name="password"id="password"value="<%=info[1]%>"/></td>

         </tr>

         <tr>

            <inputtype="submit"value="提交"/>

         </tr>

 

      </table>

</form>

 

</body>

 

valid.jsp

 

<body>

<%

 String name=request.getParameter("name");

Stringpassword=request.getParameter("password");

if(name.equals("zyq")&& password.equals("123")){

   Cookiezyqcookie = newCookie("zyqcookie",name+"#"+password);

   zyqcookie.setMaxAge(60*60*24*30);//一個月時間

   response.addCookie(zyqcookie);

%>

<script>

alert("用戶登錄成功");

</script>

<%

}

else{

   %>

<script>

alert("用戶登錄失敗");

<%

}

%>

</script>

</body>

 

定時刷新頁面

<body>

<h1>自動刷新頁面</h1>

<%

response.setHeader("Refresh","60");

out.print(new Date());

%>

</body>

發佈了35 篇原創文章 · 獲贊 6 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章