out.println(channels[i]);
}
}
二,Servlet
1:
内置对象
A;
Jsp中
Servlet中
request
request
session
request.getSession()
appllication
request.getSession().
getServletContext()
B:
使用相应的类直接调用;
2:
二个常用方法
doGet(),doPost()
分别由表单method的值决定在那个方法里写代码
如果meth=post就在doPost()方法中写
如果meth=get就在doGet()中写代码
3,注意在取url时要尽量的短,
如:
url=项目名/自定的名
三,数据库(参考java2实用教程(第三版)第15章)
1
2,连接数据库
privatestaticfinalStringcls="com.microsoft.sqlserver.jdbc.SQLServerDriver";
privatestaticfinalStringurl="jdbc:
sqlserver:
//localhost:
1433;DatabaseName=newsdatabase";
privatestaticfinalStringuser="sa";
privatestaticfinalStringpassword="123456";
intfail=0;
publicstaticConnectioncon;
publicConnectiongetConnection()
{
try{
Class.forName(cls);
}catch(ClassNotFoundExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
try{
con=DriverManager.getConnection(url,user,password);
}catch(SQLExceptione){
//TODOAuto-generatedcatchblock
fail=1;
e.printStackTrace();
System.out.println("连接错误1");
}
returncon;
}
3,更新
A:
Statementstmt=con.createStatement();
stmt.executeUpdate(sql);
例子:
ConnectionManagercon=newConnectionManager();
Stringsql="select*fromfirsttitle";
Statementstmt=con.getConnection().createStatement();
ResultSetrs=stmt.executeQuery(sql);
while(rs.next())
{
Stringname=rs.getString
(2);
}
con.closeAll();
Stmt.close();
Rs.close();
B:
PreparedStatementpstmt=con.prepareStatement(sql);
pstmt.executeUpdate();
例子:
publicFirsts(Stringname,Stringcreator)
{
this.name=name;
this.creator=creator;
try{
con=newConnectionManager();
if(con.fail==1)fail=1;
Stringselectsql="select*fromfirsttitle";
Stringsql="insertintofirsttitlevalues(?
?
?
getdate())";
//查询
stmt=con.getConnection().createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs=stmt.executeQuery(selectsql);
rs.last();
intidname=rs.getRow();
idname++;
if(stmt!
=null)
{
stmt.close();
stmt=null;
}
//插入
pstmt=con.getConnection().prepareStatement(sql);
pstmt.setInt(1,idname);
pstmt.setString(2,name);
pstmt.setString(3,creator);
pstmt.executeUpdate();
if(pstmt!
=null)
{
pstmt.close();
pstmt=null;
}
con.closeall();
}catch(SQLExceptione){
//TODOAuto-generatedcatchblock
fail=1;
System.out.println("连接错误3");
e.printStackTrace();
}
}
4,查询
A;PreparedStatementpstmt=con.prepareStatement(sql);
pstmt.executeQuery();
B:
Statementstmt=con.createStatement();
stmt.executeQuery(sql);
例子:
con=ConnectionManager.getConnection();
StringsqlStr="selectmax(id)fromFirstLevelTitle";
pstmt=con.prepareStatement(sqlStr);
resSet=pstmt.executeQuery();
if(resSet.next()){
id=resSet.getInt
(1);
}
四,JavaBean
useBeanid="创建对象名(任意)"calss="包.类"scope="制定对象的范围"/>
setPropertyname=“JavaBean中的id”property=“JavaBean中的属性名”param=“参数名”或value=“值”/>
五,EL
1.El
A:
${对象.属性}
B:
(原样输出)\${对象.属性}
jsp
el
page
pageScope
request
requestScope,param
session
sessionScope
application
applicationScope
2.用法
A中情况:
<%@pagecontentType="text/html;charset=gbk"%>
EL的使用
useBeanid="bean"class=“cn.edu.bzu.jw.bean.entity.Simplebean"
scope="request"/>
setPropertyname="bean"property="name"value="${param.name}"/>
setPropertyname="bean"property="age"value="25"/>
姓名:
${bean.name}
年龄:
${bean.age}
B中情况:
如在login.jsp中表单控件名为username和password,
则在doLogin.jsp页面中可以使用如下方法获取请求参数
用户名:
${param.username}.
密码:
${param.password}.
C中情况
举例:
School有属性:
Teacher和Student,分别为Teacher对象和Student对象
Teacher有属性:
teacherName和intresting
Student有属性:
studentName和score
在JSP页面中使用EL表示下面属性
${school.teacher.teacherName}
${school.teacher.intresting}
${school.student.studentName}
${school.student.score}
3EL隐式对象
六JSTL
1:
通用标签
<%@tagliburi="prefix="c"%>
setvar="example"value="${100+1}"scope="session"/>
outvalue="${example}"/>
removevar="example"scope="session"/>
2:
语法格式
A:
种out>
outvalue=“要显示的数据对象”[escapeXML=“true|false”][default=“默认值”]/>
outvalue=“要显示的数据对象”
[escapeXML=“true|false”]>默认值
out>
B:
种set>
setvalue=“值1”var=“变量名1”
[scope=“page|request|session|application”]>
set>
setvar=“变量名2”
[scope=“page|request|session|application”]>值2
set>
setvalue=“值3”target=“JavaBean对象”property=“属性名”/>
settarget=“JavaBean对象”property=“属性名”>值4
set>
C:
种remove>标签
removevar=“变量名”scope=“page|request|session|application”]/>
D:
种catch>标签
catchvar=“变量名”>
容易产生异常的代码
catch>
3:
if>条件标签
iftest=“条件”var=“name”[scope=“page|request|session|application”]>
……
if>
例子:
<%@tagliburi="prefix="c"%>
useBeanid="user"class="cn.edu.bzu.jw.bean.entity.User"scope="request"/>
useBeanid="userManager"class=“cn.edu.bzu.jw.bean.biz.UserBiz"
scope="request"/>
setPropertyname="user"property="name"param="loginName"/>
setPropertyname="user"property="password"param="password"/>
iftest="${userManager.valid}">
forwardpage=“success.jsp"/>
if>
iftest="${!
userManager.valid}">
forwardpage="register.html"/>
if>
4,choose>、when>和otherwise>标签
choose>
whentest=“条件1”>
表达式//业务逻辑1
when>
whentest=“条件2”>
表达式//业务逻辑2
when>
……
otherwise>
表达式//业务逻辑n
otherwise>
choose>
5.迭代标签forEach>
forEachvar="varName"items="collection"
[varStatus=“statusName”][begin=“begin”end=“end”step=“step”]>
//循环内容
forEach>
例子:
A例=B例
A例
useBeanid=“firstBean"class=“cn.edu.bzu.jw.bean.FirstLevelTitleDB"/>
setvar=“firstList"value="${firstBean.allFlt}"scope="session"/>
forEachvar=“first"items="${sessionScope.firstList}">
parentId=${first.id}">
${first.titleName}
forEach>
B例
<%for(inti=0;ifirst=(FirstLevelTitle)firstList.get(i);
%>
parentId=${first.id}">
${first.titleName}
<%}%>
6.SQL标签(看课件吧)