隨筆-60  評論-117  文章-0  trackbacks-0

                從頭開始學jsp,對它有興趣是第一要事。以下幾個案例非常的簡單,不需要用到別的知識。用來先對jsp有所感知是個不錯的注意。
          案例1 :displace.jsp
          <%@   page   contentType="text/html;   charset=GB2312"   %>  
            <form   name="form1"   action="displace.jsp"   method="post">  
            <br><br>  
            <input   type="text"   name="Vals"><input   type="text"   name="Amount">  
            <input   type="submit"   name="Submit"   value="Submit">  
            </form>  
            <%  
            int   intLocal_Vals,   intLocal_Amount;  
            if(request.getParameter("Vals")!=null   &&   request.getParameter("Amount")!=null)  
            {  
                intLocal_Vals   =   Integer.parseInt(request.getParameter("Vals"));  
                intLocal_Amount   =   Integer.parseInt(request.getParameter("Amount"));  
                //下面進行位移操作  
              intLocal_Vals=intLocal_Vals>>intLocal_Amount;  
                out.print("<br>位移后的值為:"   +intLocal_Vals);  
            }else{  
                out.print("位移值或位移量不能為空!");  
            }  
            %>
          案例1的所有操作都在一個頁面內完成,一般不會出現(xiàn)什么問題,主要用來認識一下jsp頁面的組成結構。
          案例2 :準備工作:在d:盤建立一個名為count.txt的空文本文檔。
          <%@ page language="java" contentType="text/html; charset=gb2312"%>

          <html>
          <head>

          <title>文字計數(shù)器</title>
          </head>
          <body bgcolor="#ffffff">
          <%@page import="java.io.*" %>
          <%
          BufferedReader file;
          //BufferedReader 對象用于讀取文件數(shù)據(jù)
          String countFile="d:/count.txt";
          //標示文件的地址
          file=new BufferedReader(new FileReader(countFile));
          //將file(BufferedRead的對象)指向文件的地址
          String readStr=null;
          //來存取文件的內容
          int writeStr=1;
          //寫入文件的變量 如果文件中訪問是0 則寫入為1
          try
          {
              readStr=file.readLine();//讀取文件內容
              }
          catch(IOException e){
              System.out.println("read wrong");
              }
          if(readStr==null) readStr="no record";
          else {
              writeStr=Integer.parseInt(readStr)+1;//讀取的內容+1
              }
          try{
              PrintWriter pw;
              //PrintWriter用于寫文件的一個類
              pw=new PrintWriter(new FileOutputStream(countFile));
              //指定文件
              pw.println(writeStr);
              //寫入變量writeStr的值
              pw.close();
          }
          catch(IOException e){
              out.println(e.getMessage());
          }
          %>
          <p align="center">
          <h1>文字計數(shù)器</h1>
          <h3>你是本站第</h3>
          <font color="ff0000" size="7">
          <%=readStr%></font>
          <h3>個讀者</h3>
          </body>
          </html>
          案例2主要是和外部文件進行了簡單的通訊,用到的主要是java代碼。

          案例3:準備工作:安裝mysql;將mysql的JDBC驅動器拷貝到Tomcat\common\lib和Tomcat\shared\lib 下。
          <%@ page contentType="text/html;charset=GB2312" %>
          <%@ page language="java" import="java.sql.*"%>
          <%
          Connection conn = null; //連接
          Class.forName("org.gjt.mm.mysql.Driver"); //驅動
          conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/db","root","8"); //建立連接
          if(conn==null){
          out.println("get Conn Error");
          }
          Statement stmt=conn.createStatement();
          ResultSet RS_result=null;
          %>
          <html>
          <head>
          <title>學習</title></head>
          <body>
          <%
          RS_result=stmt.executeQuery("select * from user");
          String Name,Password;
          while(RS_result.next())
          {
          Name=RS_result.getString("name");
          Password=RS_result.getString("password");
          %>
          <P><%=Name%>
          <%=Password%></p>
          <%
          }

          stmt.close();
          conn.close();
          %>
          </body>
          </html>
          案例3里其實只是用java實現(xiàn)了一個數(shù)據(jù)庫連接。
          案例4:
          login.jsp
          <%@   page   contentType="text/html;   charset=GB2312"   %>
          <html>
            <head>
             <title>login</title>
            </head>
           
            <body>
              <form name="Sayhi" method="post" action="Jsp2.jsp">
              <p>請輸入用戶信息:</p>
             <p>姓名 <input type="text" name="name" size="12"></p>
             <p>密碼 <input type="password" name="password" size="12"></p>
              <input type="submit" value="確認">
              <input type="reset" value="取消">
          </body>
          </html>
          handle.jsp
          <%@page import="java.sql.*" contentType="text/html;charset=gb2312"   %>  
            <html>  
            <head>
            <title>認證</title>  
            </head>  
            <body>  
            <%
            String   Name=request.getParameter("name");  
                  String   Password=request.getParameter("password");  
                  Class.forName("org.gjt.mm.mysql.Driver").newInstance();  
                  String   url="jdbc:mysql://localhost:3306/db";  
                  String   user="root";  
                  String   password="8";  
                  Connection   conn=DriverManager.getConnection(url,user,password);  
                  Statement   stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);  
                  String   sql="select * from   user   where   name='"+Name+"'   and   password='"+Password+"'";  
                  ResultSet   rs=stmt.executeQuery(sql);  
                  if(rs.next()){  
                  out.print("恭喜你,登陸成功!");  
                  }  
                  else{  
                  out.print("抱歉!登陸不成功!");  
                  }  
                  rs.close();  
                  stmt.close();  
                  conn.close();  
                  %>  
            </body>  
            </html>
          案例4是jsp最常用的功能,實現(xiàn)用戶登陸的問題。
          案例5:
          CountTest.java
          package Test;

          public class CountTest {
           private static int count = 0;
            
              public CountTest() {
           }
           
              public static int getCount() {
           count++;
           return count;
           }

            public static void setCount(int a) {
          count =a;
           }
          }
          counter.jsp
          <%@page import="Test.*"%>

          <HTML>
          <HEAD>
          <TITLE>
          counter
          </TITLE>
          </HEAD>
          <BODY>
          <H1>
          JBuilder Generated JSP
          </H1>
          <jsp:useBean id="bean0" scope="application" class="Test.CountTest" />
          <%
          out.println("The Counter is : " + bean0.getCount() + "<BR>");
          %>
          </BODY>
          </HTML>
          案例5是在java完成處理,在jsp里完成顯示的例子。

          posted on 2007-04-19 12:06 靜兒 閱讀(13265) 評論(8)  編輯  收藏 所屬分類: 技術

          評論:
          # re: jsp基礎案例 2007-04-19 14:43 | 山風小子
          學習JSP就好和Servlet結合起來學。
          JSP中最好不要有Java代碼。

          呵呵~你不會嫌我煩吧,幾乎每寫一篇文章,我都來唧唧歪歪 :)  回復  更多評論
            
          # re: jsp基礎案例 2007-04-19 14:50 | 靜兒
          哪里,哪里。能得到高人的指點和指正,可是我寫blog的重要意義之一啊。感謝您還來不及。@山風小子
            回復  更多評論
            
          # re: jsp基礎案例 2007-04-19 15:47 | 山風小子
          @靜兒
          高人可不敢當噢 :)  回復  更多評論
            
          # re: jsp入門的簡單例子 2007-04-27 10:46 | ddd
          偶也不習慣在jsp里面寫java代碼,(幾乎沒寫過)

          最多用用jstl tag來處理。。。

          這樣jsp看上去很清靜。。。  回復  更多評論
            
          # re: jsp入門的簡單例子 2007-04-27 14:54 | 靜兒
          呵呵,謝謝指教。我初學java和jsp,還不太懂它們之間的關系。以后會慢慢讓jsp看上去清靜些的。@ddd
            回復  更多評論
            
          # re: jsp入門的簡單例子[未登錄] 2010-10-12 16:58 | y
          怎么我運行第二個程序的時候(我是用Myeclipse運行的),顯示有錯誤:說writeStr cannot be resolved,為啥米呀?  回復  更多評論
            
          # re: jsp入門的簡單例子[未登錄] 2012-01-12 10:06 | a
          c  回復  更多評論
            
          # re: jsp入門的簡單例子 2012-11-27 11:46 | 龔業(yè)
          HTTP Status 500 - /counter.jsp(13,0) The value for the useBean class attribute Test.CountTest is invalid.
          最后一個例子報這個錯誤,請問怎么解決,謝謝  回復  更多評論
            
          主站蜘蛛池模板: 宜昌市| 高阳县| 麻阳| 沈丘县| 广东省| 横山县| 锦州市| 中阳县| 偏关县| 吴忠市| 双流县| 周宁县| 江门市| 长葛市| 沂源县| 莱州市| 竹北市| 弥渡县| 罗田县| 盐池县| 黄骅市| 汶上县| 陇南市| 灵丘县| 长泰县| 通山县| 鲁甸县| 汤原县| 安丘市| 文成县| 鹤岗市| 克东县| 尚义县| 汽车| 称多县| 定日县| 兴义市| 义乌市| 石景山区| 怀仁县| 申扎县|