JavaWeb 课件资料整理.docx

上传人:b****2 文档编号:2412685 上传时间:2023-05-03 格式:DOCX 页数:18 大小:950.69KB
下载 相关 举报
JavaWeb 课件资料整理.docx_第1页
第1页 / 共18页
JavaWeb 课件资料整理.docx_第2页
第2页 / 共18页
JavaWeb 课件资料整理.docx_第3页
第3页 / 共18页
JavaWeb 课件资料整理.docx_第4页
第4页 / 共18页
JavaWeb 课件资料整理.docx_第5页
第5页 / 共18页
JavaWeb 课件资料整理.docx_第6页
第6页 / 共18页
JavaWeb 课件资料整理.docx_第7页
第7页 / 共18页
JavaWeb 课件资料整理.docx_第8页
第8页 / 共18页
JavaWeb 课件资料整理.docx_第9页
第9页 / 共18页
JavaWeb 课件资料整理.docx_第10页
第10页 / 共18页
JavaWeb 课件资料整理.docx_第11页
第11页 / 共18页
JavaWeb 课件资料整理.docx_第12页
第12页 / 共18页
JavaWeb 课件资料整理.docx_第13页
第13页 / 共18页
JavaWeb 课件资料整理.docx_第14页
第14页 / 共18页
JavaWeb 课件资料整理.docx_第15页
第15页 / 共18页
JavaWeb 课件资料整理.docx_第16页
第16页 / 共18页
JavaWeb 课件资料整理.docx_第17页
第17页 / 共18页
JavaWeb 课件资料整理.docx_第18页
第18页 / 共18页
亲,该文档总共18页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

JavaWeb 课件资料整理.docx

《JavaWeb 课件资料整理.docx》由会员分享,可在线阅读,更多相关《JavaWeb 课件资料整理.docx(18页珍藏版)》请在冰点文库上搜索。

JavaWeb 课件资料整理.docx

JavaWeb课件资料整理

Javaweb

一,本学期内容

1,jsp

2,servlet

3,数据库

4,EL和JSTL

二,jsp

1,内置对象

A,page:

在一个页面范围内(仅仅在一个页面内使用,仅仅能一次请求,仅

仅在一个浏览器内使用)

B,request:

在一次服务器请求范围内(能在多个页面内使用,仅仅进行一

次请求,仅仅在一个浏览器内使用)

C,session;在一个会话范围内(能在多个页面内使用,能进行多次请求,仅

仅在一个浏览器内使用)

D,application:

在一个服务范围内使用(能在多个页面内使用,能进行多次

请求,能在多个浏览器内使用

例子:

已访问人数统计

inti=0;

application.setAttribute("num",++i);

intnum=(Integer)application.getAttribute("num");

2,所有内置对象公有方法

setAttribute(Stringkey,Objextobject)

getAttribute(Stringkey)

removeAttribute(Stringkey)

(注意:

page时使用pageContext

如:

pageContext.setAttribute("num","nin"));

3,跳转

A:

forward:

无条件跳转,服务器跳转(遇到就跳转)

a;标签形式

forwardpage="url"/>

forwardpage="url"value="值">

forwardpage="url">

param=""value=""/>

forward>

b;代码形式

RequestDispatcher

rd=request.getRequestDispatcher("url").forward(request,response)

B:

response客户端跳转(相应程序执行完再跳转)

response.sendRedirect("url")

C:

超链接

变量名="值"&变量名="值"……/>

(注意:

第二种方式可以使用request获取值

例子:

页面a.html

nam="……"/>

页面b.jsp

Stringnum=request.getParmeter("num");

D:

设置头信息

response.setHeader(“头信息内容”,”头信息参数”);

(注意:

最有用的一个头信息:

刷新(refresh)

应用:

通过setHeader可以实现跳转功能

例:

两秒后跳到另一个页面上

response.setHeader(“refresh”,”2;URL=另一页面名称”);

4:

include

A,静态包含格式:

<%@includefile="url"%>

(注意:

a:

include语句可以包含任意文件包括*.txt,*.html,*.jsp,*.inc

包含的都是只读文件

b:

在一个jsp页面中只能出现一组,因

此在开发时被包含的文件,只写显示部分的html代码,

主题代码不要写

c:

只是将页面的内容静态的包含进来,而不管被包含的页面

文件后缀如何

d:

执行时,先将所有内容包含进来,一起编译处理完之后,

再将所有的内容发送给客户端

例子:

<%@includefile="a.txt"%>

建立文件取名a.txt

内容:

文本

B,动态包含格式

includepage="url"/>

includepage="url">

paramname="参数名"value="值"/>

include>

(注意:

a:

如果传递参数,测被包含的页面必须是jsp

例子:

includepage="a.jsp">

paramname="参数名"value="值"

include>

建立jsp取名a.jsp

<%

Stringstr=request.getParameter("参数名");

%>

<%=str%>

C:

代码形式

JspRuntimeLibrary.include(request,response,"url",out,false);

(注意:

include>标签还有一个flush属性,可选值是true和

false。

如果flush属性为true,就表示源组件在包含目标组

件之前,先把已经生成的形影正文提交给客户。

Flush属性

的默认属性是false

5:

基本语法

A;jsp页面组成

例:

<%@pagelanguage="java"import="java.util.*,cn.bzu.bean.entity.User"pageEncoding="ISO-8859-1"%>

B:

request常用方法

(注意:

防止乱码的形式

(1),request.setCharacterEncoding("GBK");

(2),将乱码的字符串linkname转化为汉字

Stringlinks=newString(linkname.getBytes("ISO-8859-1"),"GBK");

例:

获取多选按钮数据

a.html

用户名

密码

你从哪里知道本论坛

报刊

网络

朋友推荐

电视

--以下是提交、取消按钮-->

b.jsp

String[]channels=request.getParameterValues("channel");

if(channels!

=null){

for(inti=0;i

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;i

first=(FirstLevelTitle)firstList.get(i);

%>

parentId=${first.id}">

${first.titleName}

<%}%>

6.SQL标签(看课件吧)

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 医药卫生 > 基础医学

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2