诺訫吧 关注:9贴子:158
  • 1回复贴,共1

【诺訫】WEB实验4 Application对象Session对象

只看楼主收藏回复

一、实验目的
1.掌握JSP的Application对象Session对象对象的用法,基本掌握JSP的开发技巧。
2.在JDK和Eclipse环境下,完成下列实验。
二、实验要求:
1.独立完成实验
2.书写实验报告书
三、实验内容:
1.请仔细阅读下列程序语句,理解每条语句的作用。源程序清单如下:
<%@ page contentType="text/html;charset=gb2312"%>
<html>
<head><title>网页计数器</title><head>
<body>
<% if (application.getAttribute("counter")==null)
application.setAttribute("counter","1");
else{
String strnum=null;
strnum=application.getAttribute("counter").toString();
int icount=0;
icount=Integer.valueOf(strnum).intValue();
icount++; application.setAttribute("counter",Integer.toString(icount));
} %>
您是第<%=application.getAttribute("counter")%>位访问者!
</body>
</html>
2.上述计数器当进行刷新时也会自动加1,试编写程序count.jsp,实现防刷新文本计数器。
诺訫版代码:
<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>诺訫防刷新计数器</title>
</head>
<body>
<%if(application.getAttribute("counter")==null)
application.setAttribute("counter","1");
else{
String strnum=null;
strnum=application.getAttribute("counter").toString();
int icount=0;
icount=Integer.valueOf(strnum).intValue();
if(session.isNew()){
icount++;}
application.setAttribute("counter",Integer.toString(icount));}%>
您是第<%=application.getAttribute("counter")%>位访客!
</body>
</html>
3.编写程序register.htm和register.jsp,做一个用户注册的界面,要求对用户填写的部分进行合法性检验,然后提交到register.jsp进行注册检验,若用户名为user开头的,就提示“该用户名已被注册”,若用户名为admin,就提示“欢迎您,管理员”,否则,就显示“注册成功”。
诺訫版代码:
register.htm
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>诺訫合法性检验</title>
<style>
.td1{
text-align:right;
}
.cen{
text-align:center;
font-weight:bold;
}
</style>
<script>
function sub(){
if(document.form1.name1.value==""){
alert("用户名不能为空!请重新输入用户名");
document.form1.name1.focus();
return false;
}
if(document.form1.password1.value==""){
alert("密码不能为空!请重新输入密码");
document.form1.password1.focus();
return false;
}
if(document.form1.password2.value!=document.form1.password1.value){
alert("两次密码不一致!请重新输入密码");
document.form1.password2.focus();
return false;
}
}
</script>
</head>
<body>
<form name="form1" onsubmit="return sub()" action="register.jsp" method="post">
<table align="center">
<tr><td colspan="2" class="cen">用户注册</td></tr>
<tr><td class="td1">用户名:</td><td><input type="text" value="" name="name1"/></td></tr>
<tr><td class="td1">密码:</td><td><input type="password" value="" name="password1"/></td></tr>
<tr><td class="td1">确认密码:</td><td><input type="password" value="" name="password2"/></td></tr>
<tr><td colspan="2" class="cen"><input type="submit" value="提交" />&nbsp&nbsp&nbsp<input type="reset" value="重置"/></td></tr>
</table>
</form>
</body>
</html>
register.jsp
<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>诺訫注册检验</title>
</head>
<body>
<%
String str=request.getParameter("name1");
if(str.startsWith("user")){
out.println("该用户已被注册");
}
else if(str.equals("admin")){
out.println("欢迎您管理员");
}
else{
out.println("注册成功");
}%>
</body>
</html>


1楼2013-12-27 21:23回复



    5楼2013-12-27 21:38
    回复