随笔-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的所有操作都在一个页面内完成,一般不会出现什么问题,主要用来认识一下jsp页面的组成结构。
案例2 :准备工作:在d:盘建立一个名为count.txt的空文本文档。
<%@ page language="java" contentType="text/html; charset=gb2312"%>

<html>
<head>

<title>文字计数器</title>
</head>
<body bgcolor="#ffffff">
<%@page import="java.io.*" %>
<%
BufferedReader file;
//BufferedReader 对象用于读取文件数据
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>文字计数器</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实现了一个数据库连接。
案例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最常用的功能,实现用户登陆的问题。
案例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 静儿 阅读(13218) 评论(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 | 龚业
HTTP Status 500 - /counter.jsp(13,0) The value for the useBean class attribute Test.CountTest is invalid.
最后一个例子报这个错误,请问怎么解决,谢谢  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航: