1.JSP項目目錄:webapps(頁面 +WEB-INF目錄)—》WEB-INF(web.xml+classes目錄)
2.<table width="200" border="1">
<tr bgcolor=”33ffcc”> <td>姓名</td> <td>id</td> </tr> </table>
3.對復(fù)選框的的參數(shù)處理:String s[ ]=request.getParameterValues("fuxuan");
if(s!=null){out.print("復(fù)選框");for(int i=0;i<s.length;i++){out.print(s[i]+",");}}
getParameterValues只是對多個名字的屬性值以數(shù)組形式儲存
4.JSP腳本元素:a..注釋:<%--客戶端看不見--%> < --客戶端看可見且可插表達(dá)式-->
b.聲明,在編譯時可以被class識別為變量<%! %>
c.表達(dá)式:<%=new java.util.Date().to.LocaleString() %> 就不用帶;
d.Scriptlet:<% 包含jsp語句,方法,變量以及表達(dá)式 %>
5.jsp指令元素:用來定義JSP文件中的全局屬性,除import外其他指令只在第一次有效。
a.<%@ pagecontentType="text/html; charset=gb2312" language="java"
import="java.sql.*", import="java.io.*" errorPage="error.jsp" buffer="12kb" autoflush="true"info="這是一個頁面設(shè)置” isErrorpage="false" isThreadSafe=”true” %>
buffer的大小被out對象用于緩存處理執(zhí)行后的jsp對客戶瀏覽器的輸出,默認(rèn)為8kb.
autoFlush用來設(shè)置buffer溢出時是否需要強(qiáng)制輸出。默認(rèn)為true
若buffer 為none,auto flush必須為true; 沒有緩存,只有溢出
b.include指令元素: <%@ include file="head.jsp" %> <%@ include file="body.htm" %>
6.Jsp動作指令
a.Include 指令:包含的內(nèi)容是動態(tài)改變的,在執(zhí)行期時才確定(在調(diào)用時才包含)
//static.htm <form name="form1" method="post" action="jsp_include.jsp"> </form>
<%@ include file="static.htm"%>
<a href="two.jsp?param1=1¶m2=2">goto two</a><br>//靜態(tài)傳遞是固定的參數(shù)數(shù)值
include works
<jsp:include page="two.jsp" flush="true">
<jsp:param name="param1" value="<%=request.getParameter("name")%>"/>
<jsp:param name="param2" value="<%=request.getParameter("password")%>"/>
</jsp:include> 用name的參數(shù)名進(jìn)行動態(tài)傳遞
//two.jsp this is a1=<%=request.getParameter("param1")%><br>
this is a2=<%=request.getParameter("param2")%>
b. Forward 指令:在同一個項目內(nèi)使用跳轉(zhuǎn),轉(zhuǎn)向。
<% String userId=request.gsetParameter("userId");
String password=request.getParameter("password");if(userId.equals("hello")){%>
<jsp:forward page="success.jsp">
<jsp:param name="userId" value="<%=userId%>" ></jsp:param>
<jsp:param name="password" value="<%= password %>"></jsp:param>
</jsp:forward> <% }else { %> <jsp:forward page="error.jsp"/> <% } %>
//success.jsp 歡迎您:<%= request.getParameter("userId") %>
你的登陸密碼是:<%= request.getParameter("password") %>
c.useBean 指令: 在Bean中的屬性名,類型必須和request對象的參數(shù)名稱相同(表單傳來的都是String類型,JSP 內(nèi)在機(jī)制自動轉(zhuǎn)換為Bean屬性類型),如果request參數(shù)為空,則Bean屬性不設(shè)置值。Javabean getProperty/setProperty的getter/setter具有自動機(jī)制
package com.jsp.ch2;
public class Person{ private String name;private String sex;private String password;
private int age; public void setName(String n){name=n;}
public void setSex(String s){sex=s;} public void setPassword(String p){password=p;}
public void setAge(int a){age=a;}
public String getName( ){return name;} public String getSex(){return sex;}
public String getPassword(){return password;} public int getAge(){return age;}}
<jsp:useBean id="person" scope="page" class="com.jsp.ch2.Person"> //new了一個person對象 <jsp:setProperty name="person" property="*"></jsp:setProperty> </jsp:useBean>
//name相當(dāng)于對象名,property相當(dāng)于成員名,用setProperty進(jìn)行set關(guān)聯(lián)
<% request.setCharacterEncoding("gb2312"); %>
用戶名:<%= person.getName() %> 性別:<%= person.getSex() %><br>
密碼:<%= person.getPassword() %> 年齡:<%= person.getAge() %><br>
使用getProperty()方法:用戶名:<jsp:getProperty name="person" property="name" />
// getProperty()調(diào)用get方法:性別:<jsp:getProperty name="person" property="sex" />
密碼:<jsp:getProperty name="person" property="password" />
年齡:<jsp:getProperty name="person" property="age" />
7.JSP內(nèi)置對象:
a.Session的會話:
<% String name=request.getParameter("name");
String password=request.getParameter("password");
String type=request.getParameter("type");
if(name.equals("nihao")&&password.equals("nihao")){
session.setAttribute("name1",name);//將name對象的值添加進(jìn)去,且取鍵,通過鍵獲對象
session.setAttribute("type",type); response.sendRedirect("loginsucess.jsp");}
else {response.sendRedirect("session_login.htm");}%>
取回session:
登陸成功,歡迎您!<%= session.getAttribute("name1") %>
<% if(session.getAttribute("type").equals("manager")) { %> 進(jìn)入管理頁面
<% } else { %> 進(jìn)入使用界面 <% } %>
c.Exception對象
<%@ page contentType="text/html; charset=gb2312" language="java"
import="java.sql.*" import="java.io.*" iserrorPage="True" %>
<%= exception.getMessage()%>
<%exception.printStackTrace(new PrintWriter(out)); %>//內(nèi)部已經(jīng)有打印輸出
總結(jié):a.getAttribute返回Object類型的對象,getParameter返回String類型的對象
request雖然只能在兩個頁面之間傳遞,但是可以通過request.setattribute傳遞到第三方頁面。
b.getParameter傳遞的數(shù)據(jù)會從客戶端傳到web服務(wù)器端,代表http請求數(shù)據(jù)
getAttribute/setAttribute方法傳遞的數(shù)據(jù)只會存在于web容器內(nèi)部,在具有轉(zhuǎn)發(fā)關(guān)系的web組件之間傳播
session.setattribute與request.getattribute之間是無法取得值的
page:當(dāng)前頁面范圍;request:在一個jsp發(fā)出請求到另一個jsp網(wǎng)頁之間;session:一段用戶和服務(wù)器連接的時間;application:在服務(wù)器一開始啟動到關(guān)閉為止。
8: request在頁面間范圍存在,不用創(chuàng)建新的了,范圍正好才行:
javabean: package mypack;
public class CounterBean{ private int count=0; public CounterBean(){}
public int getCount(){ return count;}
public void setCount(int count){ this.count=count; } }
page: <%@ page import="mypack.CounterBean" %> //導(dǎo)入才能用類的類型定義
<jsp:useBean id="myBean" scope="request" class="mypack.CounterBean"/>
Current count value is:<jsp:getProperty name="myBean" property="count" />
<jsp:setProperty name="myBean" property="count"
value="<%= myBean.getCount()+1 %>"/> //此處設(shè)置了對象屬性名供get取出
<% CounterBean obj=null; String scope=null;
obj=(CounterBean)request.getAttribute("myBean"); if(obj!=null)scope="request";
obj=(CounterBean)session.getAttribute("myBean"); if(obj!=null)scope="session";
obj=(CounterBean)application.getAttribute("myBean");//將對象取出來
if(obj!=null)scope="application";
if(scope==null) scope="page";%> scope=<%= scope %>
<jsp:forward page="requestCounter1.jsp" /> //在url路徑信息是不顯示的,只是調(diào)用了使用過程requestCounter1.jsp:
<jsp:useBean id="myBean" scope="request" class="mypack.CounterBean"/>
Current count value is:<jsp:getProperty name="myBean" property="count" />
session 對象是永遠(yuǎn)存在的,只要用戶存在。application.getAttribute();也是有的。
9.JDBC:JAVA程序與數(shù)據(jù)庫系統(tǒng)通信的標(biāo)準(zhǔn)API(與數(shù)據(jù)庫進(jìn)行連接,發(fā)送SQL語句,處理結(jié)果)
常用的JDBC類與方法:
a.Mysql:Class類:返回與帶有給定字符串名的類或接口相關(guān)聯(lián)的Class對象
<% try{
Class.forName("com.mysql.jdbc.Driver");//一.加載驅(qū)動程序,在lib下由sun提供
String url="jdbc:mysql://127.0.0.1:3306/mydb"; String user="root";
String password="root";
Connection conn=DriverManager.getConnection(url,user,password);
//二.Connection負(fù)責(zé)維護(hù)JSP/JAVA數(shù)據(jù)庫程序和數(shù)據(jù)庫之間的聯(lián)機(jī), DriverManager 類會嘗試加載在 "jdbc.drivers" 系統(tǒng)屬性中引用的合適的驅(qū)動程序類.
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE); //參數(shù)不寫也行,默認(rèn)自帶,自動滾動和游標(biāo)
//三.Connection類createStatement方法創(chuàng)建一個 Statement 對象將 SQL 語句發(fā)送到數(shù)據(jù)庫
String sql="select * from mytable"; //stmt.executeQuery(sql);//四.執(zhí)行
ResultSet rs=stmt.executeQuery(sql);// executeQuery是Statement類方法
while(rs.next()){out.println("first "+rs.getInt("age"));//返回結(jié)果集在記錄表最上面
out.println("second "+rs.getString("name")+"<br>"); }
out.print("success!");rs.close();stmt.close();conn.close(); }
catch (Exception e){e.printStackTrace(); } %>
//五.取的執(zhí)行結(jié)果,用Statement來執(zhí)行sql語句,該語句返回單個 ResultSet 對象。
Statement sm=cn.createStatement();sm.executeQuery(sql);sm.executeUpdate(sql);
b.預(yù)處理語句:<% try{ PreparedStatement pstmt=conn.prepareStatement(
"insert into userinfo values(?,?)");
pstmt.setString(1,"nihao"); pstmt.setInt(2,3232); pstmt.execute(); pstmt.close();
out.print("success!");pstmt.close(); con.close()} catch (Exception e){ } %>