從頭開始學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
靜兒 閱讀(13263)
評論(8) 編輯 收藏 所屬分類:
技術