1. 程式人生 > >JSP技術2

JSP技術2

頁面定時重新整理或者自動跳轉

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'ch03_12.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    當前時間是:<%=new Date().toLocaleString()%><br>
    <hr>
    <%response.setHeader("refresh","1")%>
  </body>
</html>

 

建立及獲取客戶的會話資訊

  <body>
    <hr>
    session的建立時間是<%=new Date(session.getCreationTime()) %><br>
    session的ID號:<%=session.getId() %><br>
    客戶最近一次訪問的時間是:
    <%=new java.sql.Time(session.getLastAccessedTime()) %><br>
    兩次請求時間間隔多長session將被取消(ms)
    <%=session.getMaxInactiveInterval() %><br>
    是否新建立的session<%=session.isNew()?"是":"否"%>
  <hr>
  </body>

案例——統計網站訪問人數

 <body>
	<%!Integer YourNumber=new Integer(0);%>
	<% 
	   if(session.isNew()){//如果是一個新的會話
	    Integer number=(Integer)application.getAttribute("Count");
	    if(number==null)//如果是第一個訪問本站
	     number=new Integer(1);
	    else
	    number=new Integer(number.intValue()+1);
	    application.setAttribute("Count", number);
	    YourNumber=(Integer)application.getAttribute("Count");
	   }
	 %>
	 歡迎訪問本站,您是第<%=YourNumber%>個訪問使用者。
  </body>

Out物件

<body>
	利用out物件輸出的頁面資訊:<br>
	<hr>
	<% 
	out.print("張小黑<br/>嘿嘿嘿");
	out.print("<br/>使用者名稱或密碼不正確,請重新 <a href='http://www.baidu.com'> <font size='15' color='red'>登陸</font></a>");
	out.print("<br><a href='javascript:history.back()'>後退</a>.......");
	%>

JSP應用程式設計綜合例項

 網上答題及自動測評系統

ch03_17_1.jsp

 <body>
  <form action="ch03_17_2.jsp"  method="post">
    一      2+3=?<br>
    <input type="radio" name="r1" value="2" checked="checked">2&nbsp;
    <input type="radio" name="r1" value="3">3&nbsp;&nbsp;
    <input type="radio" name="r1" value="4">4&nbsp;
    <input type="radio" name="r1" value="5">5<br>
    二     下列哪些是偶數?<br>&nbsp;&nbsp;
    <input type="checkbox" name="c1" value="2" checked="checked">2&nbsp;
    <input type="checkbox" name="c1" value="3">3&nbsp;
    <input type="checkbox" name="c1" value="4">4&nbsp;
    <input type="checkbox" name="c1" value="5">5<br>
    三     下列哪些是動態網頁?<br>&nbsp;&nbsp;
	 <select size="4" name="list1" multiple="multiple">
	  <option value="asp">ASP</option>
	  <option value="php">PHP</option>
	  <option value="htm">HTM</option>
	  <option value="jsp">JSP</option>
	  <option value="xyz" selected="selected">XYZ</option>
	  </select><br>
	四     下列元件那個是伺服器端的?<br>&nbsp;&nbsp;
	 <select size="1" name="list5">
	  <option value="jsp">JSP</option>
	  <option value="servlet"> SERVLET</option>
	  <option value="java">JAVA</option>
	  <option value="jdbc">JDBC</option>
	  </select><br>
	五     在伺服器端用來接受使用者請求的物件是:
	  <input type="text" size="20" name="text1"><br>
	  <div align="left">
	  <blockquote>
		  <input type="submit" value="提交" name="button1">
		  <input type="reset" value="重置" name="button2">
		  </blockquote>
	  </div>
    </form>
  </body>

ch03_17_2.jsp

 <body>
    <% 
     String s1=request.getParameter("r1");
     if(s1!=null){
     out.println("一  解答為:2+3="+s1+"   ");
     if(s1.equals("5"))
        out.println("正確!"+"<br>");
     else
        out.println("錯誤!"+"<br>");
     }
     else
       out.println("一   沒有解答!");
     out.println("--------------------------------<br>");
     String[] s21=request.getParameterValues("c1");
     if(s21!=null){
        out.println("二   解答:偶數有:");
        for(int i=0;i<s21.length;++i){
           out.println(s21[i]+"   ");
        }
        if(s21.length==2&&s21[0].equals("2")&&s21[1].equals("4"))
           out.println("正確! "+"<br>");
        else
          out.println("錯誤!"+"<br>");
     }
     else
       out.println("二   沒有解答!");
      out.println("--------------------------------<br>");
      String[] s31=request.getParameterValues("list1");
      if(s31!=null){
         out.println("三   解答為:動態網頁有:");
         for(int i=0;i<s31.length;++i){
           out.println(s31[i]+"   ");
          }
          if(s31.length==3&&s31[0].equals("asp")&&s31[1].equals("php")
      &&s31[2].equals("jsp"))
       out.println("正確!"+"<br>");
      else
        out.println("錯誤!"+"<br>");
      }
      else
        out.println("三  沒有解答! ");
       out.println("--------------------------------<br>"); 
       String s4=request.getParameter("list5");
       if(s4!=null){
         out.println("四   解答為:伺服器端的元件有:");
       out.println(s4+"   ");
       if(s4!=null&&s4.equals("servlet"))
         out.println("正確!"+"<br>");
       else
         out.println("錯誤!"+"<br>");
       }
      else
        out.println("四   沒有正確答案!");
     out.println("--------------------------------<br>");  
     String s5=request.getParameter("text1");
     if(s5!=null){
       out.println("五   解答為:");
     out.println(s5+"   ");
     if(s5!=null&&s5.equals("request"))
        out.println("正確!"+"<br>");
     else
       out.println("錯誤!"+"<br>");
     }
     else
       out.println("五  沒有解答!");
     out.println("--------------------------------<br>"); 
    %>
  </body>

設計簡單的購物車應用案例

ch03_17_1.jsp

<body>
    <% 
       request.setCharacterEncoding("UTF-8");
       if(request.getParameter("c1")!=null)
          session.setAttribute("s1",request.getParameter("c1"));
       if(request.getParameter("c2")!=null)
          session.setAttribute("s2",request.getParameter("c2"));
       if(request.getParameter("c3")!=null)
          session.setAttribute("s3",request.getParameter("c3"));       
    %>
    各類肉大甩賣一律十塊<br>
    <form method="post" action="ch03_18_1.jsp">
    <p>
    <input type="checkbox"  name="c1" value="豬肉">豬肉&nbsp;
    <input type="checkbox"  name="c2" value="牛肉">牛肉&nbsp;
    <input type="checkbox"  name="c3" value="羊肉">羊肉
    </p>
    <p>
    <input type="submit" value="提交" name="B1">
    <a href="ch03_17_2.jsp">買點別的</a>&nbsp;
    <a href="ch03_17_3.jsp">檢視購物車</a>
    </p>
    </form>
  </body>

ch03_17_2.jsp

<body>
    <% 
       request.setCharacterEncoding("UTF-8");
       if(request.getParameter("b1")!=null)
          session.setAttribute("s4",request.getParameter("b1"));
       if(request.getParameter("b2")!=null)
          session.setAttribute("s5",request.getParameter("b2"));
       if(request.getParameter("b3")!=null)
          session.setAttribute("s6",request.getParameter("b3"));       
    %>
    各種球大甩賣,一律八塊:<br>
    <form method="post" action="ch03_18_2.jsp">
    <p>
    <input type="checkbox"  name="b1" value="籃球">籃球&nbsp;
    <input type="checkbox"  name="b2" value="足球">足球&nbsp;
    <input type="checkbox"  name="b3" value="排球">排球
    </p>
    <p>
    <input type="submit" value="提交" name="x1">
    <a href="ch03_17_1.jsp">買點別的</a>&nbsp;
    <a href="ch03_17_3.jsp">檢視購物車</a>
    </p>
    </form>
  </body>

ch03_17_3.jsp

 <body>
    你選擇的結果是:<br>
    <%
     request.setCharacterEncoding("UTF-8");
     String  str="";
     if(session.getAttribute("s1")!=null){
       str=(String)session.getAttribute("s1");
       out.println(str+"<br>");
     }
     
      if(session.getAttribute("s2")!=null){
       str=(String)session.getAttribute("s2");
       out.println(str+"<br>");
     }
     
      if(session.getAttribute("s3")!=null){
       str=(String)session.getAttribute("s3");
       out.println(str+"<br>");
     }
     
      if(session.getAttribute("s4")!=null){
       str=(String)session.getAttribute("s4");
       out.println(str+"<br>");
     }
     
      if(session.getAttribute("s5")!=null){
       str=(String)session.getAttribute("s5");
       out.println(str+"<br>");
     }
     
      if(session.getAttribute("s6")!=null){
       str=(String)session.getAttribute("s6");
       out.println(str+"<br>");
     }
     
     %>
  </body>