《JSP笔记》之六JSP综合实例Word文档格式.docx

上传人:b****2 文档编号:4738963 上传时间:2023-05-04 格式:DOCX 页数:47 大小:137.05KB
下载 相关 举报
《JSP笔记》之六JSP综合实例Word文档格式.docx_第1页
第1页 / 共47页
《JSP笔记》之六JSP综合实例Word文档格式.docx_第2页
第2页 / 共47页
《JSP笔记》之六JSP综合实例Word文档格式.docx_第3页
第3页 / 共47页
《JSP笔记》之六JSP综合实例Word文档格式.docx_第4页
第4页 / 共47页
《JSP笔记》之六JSP综合实例Word文档格式.docx_第5页
第5页 / 共47页
《JSP笔记》之六JSP综合实例Word文档格式.docx_第6页
第6页 / 共47页
《JSP笔记》之六JSP综合实例Word文档格式.docx_第7页
第7页 / 共47页
《JSP笔记》之六JSP综合实例Word文档格式.docx_第8页
第8页 / 共47页
《JSP笔记》之六JSP综合实例Word文档格式.docx_第9页
第9页 / 共47页
《JSP笔记》之六JSP综合实例Word文档格式.docx_第10页
第10页 / 共47页
《JSP笔记》之六JSP综合实例Word文档格式.docx_第11页
第11页 / 共47页
《JSP笔记》之六JSP综合实例Word文档格式.docx_第12页
第12页 / 共47页
《JSP笔记》之六JSP综合实例Word文档格式.docx_第13页
第13页 / 共47页
《JSP笔记》之六JSP综合实例Word文档格式.docx_第14页
第14页 / 共47页
《JSP笔记》之六JSP综合实例Word文档格式.docx_第15页
第15页 / 共47页
《JSP笔记》之六JSP综合实例Word文档格式.docx_第16页
第16页 / 共47页
《JSP笔记》之六JSP综合实例Word文档格式.docx_第17页
第17页 / 共47页
《JSP笔记》之六JSP综合实例Word文档格式.docx_第18页
第18页 / 共47页
《JSP笔记》之六JSP综合实例Word文档格式.docx_第19页
第19页 / 共47页
《JSP笔记》之六JSP综合实例Word文档格式.docx_第20页
第20页 / 共47页
亲,该文档总共47页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

《JSP笔记》之六JSP综合实例Word文档格式.docx

《《JSP笔记》之六JSP综合实例Word文档格式.docx》由会员分享,可在线阅读,更多相关《《JSP笔记》之六JSP综合实例Word文档格式.docx(47页珍藏版)》请在冰点文库上搜索。

《JSP笔记》之六JSP综合实例Word文档格式.docx

//login_flag标记为null,则说明不是从其它网页跳转而来。

//login_flag标记为false,则说明是从其它网页跳转来的。

out.println("

fontcolor=red>

请从首页登录!

/font>

"

}

}

%>

formaction="

method=postname=form1>

用户:

inputtype=textname=usernamesize=8maxlength=16>

(用户名admin)

<

p>

密码:

inputtype="

password"

name="

size="

6"

maxlength="

12"

(口令pass)

inputtype=resetname=reset1value=重置/>

inputtype=submitname=submit1value=提交>

/form>

Stringusername=request.getParameter("

username"

Stringpassword=request.getParameter("

if(username!

=null&

&

password!

=null){

//若username和password都不为空,说明已接收到了输入。

if(username.equals("

)||password.equals("

)){

用户名或密码不能为空!

else{

if(username.equals("

admin"

)&

password.equals("

pass"

session.setAttribute("

u_name"

username);

p_word"

password);

"

ok"

<

h3>

欢迎来到本网站。

/h3>

单击<

ahref="

index1.jsp"

>

进入<

/a>

或5秒种后自动进入网站

%response.setHeader("

Refresh"

5;

url=index1.jsp"

}

else{

out.println("

用户名或密码错误!

请重新输入!

/body>

/html>

网页文件名check.jsp

检查登录状况<

Stringusername=(String)session.getAttribute("

Stringaccess=(String)session.getAttribute("

out.println(username);

out.println(access);

access!

(access.equals("

))){

欢迎"

+username);

访问本网站!

else{

out.println("

请先从login.jsp网页登录!

response.setHeader("

url=login.jsp?

access=false"

网页文件名index1.jsp

无标题文档<

h1>

主页<

/h1>

运行后的效果请同学们自己上机实践。

6.2[案例15]商品信息列表

用表格输出数据库表是不错的方法。

但有时,并不知道数据表的具体字段名和字段的个数。

这时可以用结果集ResultSet类的ResultSetMetaData类对象的方法,来完成不知道表的字段个数和字段名字的情况下输出数据表的任务。

相关方法见书第217至218页

这里,用到了

创建ResultSetMetaData类对象:

ResultSetMetaDatarsm=rs.getMetaData();

获取ResultSetMetaData类对象的字段个数的方法:

intcols=rsm.getColumnCount();

获取ResultSetMetaData类对象的字段名的方法:

out.print(rsm.getColumnName(th));

网页文件名listshop.jsp

!

--listshop.jsp-->

charset=GB2312"

import="

java.sql.*"

商品信息列表<

h2align="

center"

try{//启动异常处理

//连接数据库,

Class.forName("

com.microsoft.jdbc.sqlserver.SQLServerDriver"

).newInstance();

Stringurl="

jdbc:

microsoft:

sqlserver:

//localhost:

1433;

DatabaseName=jspdb"

;

Stringuser="

Stringpassword="

Connectionconn=DriverManager.getConnection(url,user,password);

//创建stmt对象

Statementstmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,

ResultSet.CONCUR_UPDATABLE);

//创建sql查询语句

Stringsql="

SELECT*FROMtrade"

//执行sql查询

ResultSetrs=stmt.executeQuery(sql);

if(rs!

=null)

{

ResultSetMetaDatarsm=rs.getMetaData();

intcols=rsm.getColumnCount();

//取得rs记录集字段个数

tableborder='

1'

//输出表格起始标签<

table>

//下面语句输出表的标题部分

tr>

//输出行起始标签<

for(intth=1;

th<

=cols;

th++)//循环输出一行中的标题格

//输出标题

th>

out.print(rsm.getColumnName(th));

//输出字段名

/th>

/tr>

//输出行结束标签<

//循环列出商品信息

introw=1;

while(rs.next())

//输出行起始标签<

for(intcol=1;

col<

col++)//循环输出一行中的单元格

{

//输出一个单元格

out.print("

td>

out.print(rs.getObject(col));

//输入指定字段内容

/td>

//输出行结束标签<

row++;

/table>

//输出表格结束标签<

rs.close();

//关闭rs

stmt.close();

//关闭stmt

conn.close();

//关闭conn

catch(ClassNotFoundExceptione)

{

驱动程序类异常!

br>

out.println(e.getMessage());

catch(SQLExceptione)

数据库连接或SQL查询异常!

catch(Exceptione)

其他异常!

运行后,效果如下:

6.3[案例16]访客留言板

本节是一个用数据库方式来存储的留言板。

和前面的留言板一样。

本例也由3个网页构成:

输入留言信息的网页1:

index.jsp

处理留言的网页2:

addmessages.jsp

查看留言的网页3:

viewmessage.jsp

其中,在网页1,可以通过表单提交去网页2,可以通过超链接去网页3。

在网页2,可以去网页3,也可以去网页1。

在网页3,可以去网页1。

数据存储在数据库jspdb的表messages中。

该表结构如下:

网页1:

文件名index.jsp

--index.jsp-->

HTML>

HEAD>

TITLE>

留言板<

/TITLE>

BODYbgcolor="

#88CCFF"

formname="

form1"

method="

post"

action="

addmessages.jsp"

tablewidth="

463"

height="

343"

border="

0"

align="

cellpadding="

cellspacing="

bgcolor="

#6699FF"

tdheight="

65"

colspan="

4"

fontcolor="

#FFFFFF"

留言板<

tdwidth="

102"

31"

right"

valign="

middle"

fontsize="

姓名:

tdcolspan="

3"

text"

name"

size=40>

E-mail:

inputname="

mail"

type="

id="

36"

主题:

title"

tdalign="

top"

留言:

textareaname="

content"

rows=12cols=40>

/textarea>

nbsp;

115"

submit"

value="

提交留言"

viewmessages.jsp"

#000000"

size=4>

查看留言<

144"

fontsize=2>

reset"

重新填写"

/p>

/BODY>

/HTML>

网页2:

文件名addmessages.jsp

--manageuser.jsp-->

language="

java"

import="

java.sql.*,java.util.*,java.text.SimpleDateFormat"

bodybgcolor="

#99CCFF"

>

request.setCharacterEncoding("

gb2312"

//将请求信息编码,解决中文乱码问题

//获取请求信息中的数据

Stringname=request.getParameter("

Stringmail=request.getParameter("

Stringtitle=request.getParameter("

Stringcontent=request.getParameter("

if(name==null)

name="

if(title==null)

title="

if(content==null)

content="

if(mail==null)

mail="

if(name.length()==0)

留言人姓名不能为空!

elseif(title.length()==0)

留言主题不能为空!

elseif(content.length()==0)

留言内容不能为空!

else

{

//通过Calendar对象得到当前时间

Calendarcl=Calendar.getInstance();

//设置格式

SimpleDateFormatsdf=newSimpleDateFormat("

yyyy-MM-ddHH:

mm:

ss"

//对日期格式化,转换成字符串

Stringdatetime=sdf.format(cl.getTime());

try{//启动异常处理

//连接数据库

//创建参数查询对象stm

insertintomessages(title,name,mail,times,content)values(?

?

)"

PreparedStatementstm=conn.prepareStatement(sql);

//准备参数

stm.setString(1,title);

stm.setString(2,name);

if(mail.length()==0)

stm.setString(3,null);

else

stm.setString(3,mail);

stm.setString(4,datetime);

stm.setString(5,content);

//更新数据库

stm.executeUpdate();

url=index.jsp"

%>

<

330"

1"

bordercolor="

#b7b7d7"

<

33"

#0099FF"

fontcolor=white>

b>

[新留言]<

/b>

106"

d6dff7"

li>

留言存储完成!

/li>

5秒钟后将自动<

index.jsp"

返回<

%

}

网页3:

文件名viewmessages.jsp

--view_message.jsp-->

java.sql.*,java.util.*"

访客留言<

/HEAD>

BODY>

palign="

所有访客留言<

%

try{//启动异常处理

//连接数据库

Class.forName("

Stringurl="

Stringuser="

Stringpassword="

Connectionconn=DriverManager.getConnection(url,user,password);

Stat

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

当前位置:首页 > 解决方案 > 学习计划

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

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