jsp复习
1.注释和语法
2.page指令
3.在jsp中连接数据库
4.包含指令和跳转指令
**********1。语法*******************
<% %>:声明变量,表达式
<%!%>:声明常量,写方法,还可以写类(一般情况下不能写)
<%=%>:输出表达式及其他详细
<%!%>:声明常量,写方法,还可以写类(一般情况下不能写)
<%=%>:输出表达式及其他详细
写个简单的练习哈 --- 动态打印表格
工具:myeclipse7.0+tomcat6.0
3-1.jsp
<%
@ page language
=
"
java
"
import
=
"
java.util.*
"
pageEncoding
=
"
GB18030
"
%>
<html>
<body>
动态打印表格
< form action = " 3-1.jsp " method = " post " >
输入行: < input type = " text " name = " rows " >< br >
输入列: < input type = " text " name = " cols " >< br >
< input type = " submit " value = " 打印表单 " >
</ form >
</ body >
动态打印表格
< form action = " 3-1.jsp " method = " post " >
输入行: < input type = " text " name = " rows " >< br >
输入列: < input type = " text " name = " cols " >< br >
< input type = " submit " value = " 打印表单 " >
</ form >
</ body >
</html>
3-1.jsp
<%
@ page language
=
"
java
"
import
=
"
java.util.*
"
pageEncoding
=
"
GB18030
"
%>
<body>
<% int row = Integer.parseInt(request .getParameter( " rows " ));
int cols = Integer.parseInt(request.getParameter( " cols " ));
%>
< table border = " 2 " bordercolor = " #eeeeee " >
<% for ( int i = 1 ;i <= cols;i ++ ){ %>
< tr >
<% for ( int j = 1 ;j <= row;j ++ ){ %>
< td ><%= i * j %></ td >
<% } %>
</ tr >
<% } %>
</ table >
</ body >
</ html >
<body>
<% int row = Integer.parseInt(request .getParameter( " rows " ));
int cols = Integer.parseInt(request.getParameter( " cols " ));
%>
< table border = " 2 " bordercolor = " #eeeeee " >
<% for ( int i = 1 ;i <= cols;i ++ ){ %>
< tr >
<% for ( int j = 1 ;j <= row;j ++ ){ %>
< td ><%= i * j %></ td >
<% } %>
</ tr >
<% } %>
</ table >
</ body >
</ html >
************2.page指令************************
page表示当前的jsp页面对象.
作用:设置MIME类型--<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%@ page contentType="text/html"%>
导包指令 <%@page import="java.util.*" %>
其他指令:错误页,禁止session,页面的缓冲等
----------------连接数据库mysql--------
1
<%
@ page language
=
"
java
"
import
=
"
java.util.*
"
pageEncoding
=
"
GB18030
"
%>
2 <% @ page import = " java.sql.* " %>
3 <%
4 String path = request.getContextPath();
5 String basePath = request.getScheme() + " :// " + request.getServerName() + " : " + request.getServerPort() + path + " / " ;
6 %>
7
8 <! DOCTYPE HTML PUBLIC " -//W3C//DTD HTML 4.01 Transitional//EN " >
9 < html >
10 < head >
11 < base href = " <%=basePath%> " >
12 </ head >
21 < body >
22 <%!
23 public static final String driver = " com.mysql.jdbc.Driver " ;
24 public static final String dburl = " jdbc:mysql://localhost:3306/bbs " ;
25 public static final String username = " root " ;
26 public static final String password = " wszf " ;
27 %>
28
29 <%
30 Connection conn = null ; // 数据库连接
31 PreparedStatement pst = null ; // 数据库操作
32 ResultSet rs = null ; // 结果集
33 %>
34 <%
35 Class.forName(driver).newInstance(); // 加载数据库驱动
36 conn = DriverManager.getConnection(dburl,username,password);
37 String sql = " select id,username,password from user_tb " ;
38 pst = conn.prepareStatement(sql);
39 rs = pst.executeQuery();
40 %>
41 < center >
42 < table border = " 1 " width = " 20% " bordercolor = " #aaa " >
43 < tr >
44 < td > id </ td >
45 < td > 姓名 </ td >
46 < td > 密码 </ td >
47 </ tr >
48 <%
49 while (rs.next()){
50 int id = rs.getInt( 1 );
51 String name = rs.getString( 2 );
52 String pass = rs.getString( 3 );
53
54 %>
55 < tr >
56 < td ><%= id %></ td >
57 < td ><%= name %></ td >
58 < td ><%= pass %></ td >
59 </ tr >
60 <% } %>
61 <%
62 rs.close();
63 pst.close();
64 conn.close();
65 %>
66 </ table >
67 </ center >
68 </ body >
69 </ html >
70
2 <% @ page import = " java.sql.* " %>
3 <%
4 String path = request.getContextPath();
5 String basePath = request.getScheme() + " :// " + request.getServerName() + " : " + request.getServerPort() + path + " / " ;
6 %>
7
8 <! DOCTYPE HTML PUBLIC " -//W3C//DTD HTML 4.01 Transitional//EN " >
9 < html >
10 < head >
11 < base href = " <%=basePath%> " >
12 </ head >
21 < body >
22 <%!
23 public static final String driver = " com.mysql.jdbc.Driver " ;
24 public static final String dburl = " jdbc:mysql://localhost:3306/bbs " ;
25 public static final String username = " root " ;
26 public static final String password = " wszf " ;
27 %>
28
29 <%
30 Connection conn = null ; // 数据库连接
31 PreparedStatement pst = null ; // 数据库操作
32 ResultSet rs = null ; // 结果集
33 %>
34 <%
35 Class.forName(driver).newInstance(); // 加载数据库驱动
36 conn = DriverManager.getConnection(dburl,username,password);
37 String sql = " select id,username,password from user_tb " ;
38 pst = conn.prepareStatement(sql);
39 rs = pst.executeQuery();
40 %>
41 < center >
42 < table border = " 1 " width = " 20% " bordercolor = " #aaa " >
43 < tr >
44 < td > id </ td >
45 < td > 姓名 </ td >
46 < td > 密码 </ td >
47 </ tr >
48 <%
49 while (rs.next()){
50 int id = rs.getInt( 1 );
51 String name = rs.getString( 2 );
52 String pass = rs.getString( 3 );
53
54 %>
55 < tr >
56 < td ><%= id %></ td >
57 < td ><%= name %></ td >
58 < td ><%= pass %></ td >
59 </ tr >
60 <% } %>
61 <%
62 rs.close();
63 pst.close();
64 conn.close();
65 %>
66 </ table >
67 </ center >
68 </ body >
69 </ html >
70