最新3030郑文龙job12.docx

上传人:b****0 文档编号:9446934 上传时间:2023-05-19 格式:DOCX 页数:18 大小:117KB
下载 相关 举报
最新3030郑文龙job12.docx_第1页
第1页 / 共18页
最新3030郑文龙job12.docx_第2页
第2页 / 共18页
最新3030郑文龙job12.docx_第3页
第3页 / 共18页
最新3030郑文龙job12.docx_第4页
第4页 / 共18页
最新3030郑文龙job12.docx_第5页
第5页 / 共18页
最新3030郑文龙job12.docx_第6页
第6页 / 共18页
最新3030郑文龙job12.docx_第7页
第7页 / 共18页
最新3030郑文龙job12.docx_第8页
第8页 / 共18页
最新3030郑文龙job12.docx_第9页
第9页 / 共18页
最新3030郑文龙job12.docx_第10页
第10页 / 共18页
最新3030郑文龙job12.docx_第11页
第11页 / 共18页
最新3030郑文龙job12.docx_第12页
第12页 / 共18页
最新3030郑文龙job12.docx_第13页
第13页 / 共18页
最新3030郑文龙job12.docx_第14页
第14页 / 共18页
最新3030郑文龙job12.docx_第15页
第15页 / 共18页
最新3030郑文龙job12.docx_第16页
第16页 / 共18页
最新3030郑文龙job12.docx_第17页
第17页 / 共18页
最新3030郑文龙job12.docx_第18页
第18页 / 共18页
亲,该文档总共18页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

最新3030郑文龙job12.docx

《最新3030郑文龙job12.docx》由会员分享,可在线阅读,更多相关《最新3030郑文龙job12.docx(18页珍藏版)》请在冰点文库上搜索。

最新3030郑文龙job12.docx

最新3030郑文龙job12

------------------------------------------作者xxxx------------------------------------------日期xxxx

 

30320330-郑文龙-job12

软件开发基础(Java)作业

作业名称

第12次作业

Java知识

JSP实验

学号

2

姓名

郑文龙

学院

数学与信息学院

专业

计算机科学与技术

评分

备注:

评分标准

作业部分

所占比例

1、课程内容阅读

45%

2、课程程序编写

45%

3、课外程序

10%

(以下空白)

一、ﻬ课程内容阅读

1、对课程补充材料及课本内容阅读后的总结

答:

我们知道JSP技术的设计目的是使得构造基于Web的应用程序更加容易和快捷,而这些应用程序能够与各种Web服务器、应用服务器、浏览器和开发工具共同工作。

 JSP中,我们需要注意的是jsp指令、jsp属性、jsp内置对象.在规范的JSP2.0中提供了三种指令:

page、include和tablib,这些指令中又包含了许多的jsp属性,当然,在jsp这个章节内容中,我学到的最多的是page指令,对于page中对应的相关指令我不一一列举了,接着讲的是jsp内置对象。

由于jsp是嵌入式的语言,不能显式地把一些必要的参数传递进来,比如Request和Response对象等,所以在jsp规范中提供了几个内置的对象来实现此功能。

常用的内置对象:

application、session、request、response、out、page、exception、pageContext.

二、课程程序编写

题目:

编写一个程序,功能如下:

1.编写login.jsp,登录时只输入一个昵称。

但要检查昵称是否已经被其他用户使用。

 2.编写game.jsp,每次游戏程序随机产生一个0—9之间的整数,要求玩家输入自

   己猜的数字,并对用户输入数字进行检查,进行如下提示:

(1)如果用户猜对了,则提示:

恭喜你,猜对了.结束本次游戏。

(2)如果用户猜错了,则提示:

你猜的数字太(大或小)了。

要求用户继续猜。

如果连续3次没有猜对,则提示:

游戏失败。

一次游戏结束时,将用户本次猜数字情况记入“排行榜”。

然后询问用户是否继续新的游戏,果用户选择继续,则开始新一次游戏。

3。

排行榜页面,显示所有参加游戏的用户的游戏情况。

 

    昵称 游戏次数 成功次数

 ABC 10    8

  XYX 11      7

 ...。

.。

4. 必须登录后才能进入游戏页面,而进入登录页面和排行榜页面,无须登录。

5.提示,排行榜数据结构应该放在application中,用户登录状态放在session中.

程序代码:

登录页面login。

jsp

〈%@page language=”java”import=”java。

util。

*”pageEncoding="UTF—8”%〉

<%

Stringpath =request。

getContextPath();

String basePath= request.getScheme()+":

//”+request。

getServerName()+":

"+request。

getServerPort()+path+"/";

Stringexit=request.getParameter("exit”);

if(exit!

=null){

ﻩsession.setAttribute(”username”,null);//退出登录

if(session.getAttribute(”username”)!

=null){

ﻩresponse.sendRedirect("game.jsp”);

%〉

 

<!

DOCTYPEHTML PUBLIC”-//W3C//DTD HTML4.01Transitional//EN”〉

<html〉

〈base href=”<%=basePath%〉”〉

〈title>登录页面〈/title〉

 〈title〉MyJSP’login。

jsp'startingpage</title〉

ﻩ<metahttp-equiv=”pragma" content=”no—cache”>

ﻩ〈metahttp—equiv="cache—control” content=”no-cache">

 

ﻩ〈!

——

—-〉

 

</head>

〈script〉

functioncheck(){

vara=document。

getElementById(”n”)。

value;

if(a=="”){

ﻩalert(”亲,您还没输入,请重新输入”);

ﻩreturnfalse;

ﻩﻩ}

ﻩreturntrue;

 };

</script>

  〈body〉

 

 

  <tr〉

  〈th bgcolor=”green"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ; ;&nbsp; ;&nbsp;〈fontcolor=”red">猜数字游戏</font〉&nbsp; ; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; ;&nbsp;&nbsp;&nbsp; </th>

   

  〈tr>

    〈td〉请输入登录昵称:

〈br〉

    

 

 <formaction=”logincheck。

jsp” method=”post">

  <inputtype="text" name=”username”id=”n”/〉

 〈input type=”submit”value="登陆"onclick=”returncheck()”/〉

   </form>

  

     

jsp”>排行</a>〈/center〉

    </td>

</tr>

 〈/table〉

〈/center>

登陆检查logincheck.jsp

<%@page language=”java" import=”java。

util.*”pageEncoding=”UTF-8"%〉

〈%

Stringpath= request.getContextPath();

StringbasePath =request。

getScheme()+":

//"+request.getServerName()+":

”+request.getServerPort()+path+”/";

%〉

〈%

request。

setCharacterEncoding(”UTF—8”);//设置编码

Stringusername=request。

getParameter(”username”);//获取用户名

intflag=0;

//判断用户是否登录过

//application.removeAttribute("LOGINED_USER”);

 List loginedUsers = newArrayList<String>();

if(application。

getAttribute("LOGINED_USER") !

=null){

 loginedUsers = (ArrayList

getAttribute("LOGINED_USER");

   for(StringloginedUser:

loginedUsers)//存在过,

 

  if(username.equals(loginedUser))

 {

  flag=1;

 

  out.print("〈script>alert(’此昵称已有人使用过!

请重新输入');location。

href='login.jsp’;”);

 // response。

sendRedirect("login.jsp");

 

  }

 }

 

 if(flag==0){

 ListnewUser=new ArrayList〈String〉();

 loginedUsers。

add(username);

newUser.add(username);

 

 session.setAttribute(”LOGINED_USER",newUser);

session。

setAttribute("username”, username);//存储用户名,方便检测

application.setAttribute("LOGINED_USER",loginedUsers);

  response.sendRedirect(”game.jsp”);

 

}

 }

if(application。

getAttribute(”LOGINED_USER")==null){

ListnewUser=newArrayList<String〉();

 

 loginedUsers.add(username);

newUser。

add(username);

session.setAttribute(”LOGINED_USER”,newUser);

 session.setAttribute("username",username);//存储用户名,方便检测

 application。

setAttribute(”LOGINED_USER”,loginedUsers);

 response。

sendRedirect("game.jsp”);

  }

%>

游戏jsp中game。

jsp

〈%@page language="java"import=”java.util.*"pageEncoding=”UTF-8"%〉

〈%

Stringpath=request.getContextPath();

String basePath= request。

getScheme()+":

//"+request。

getServerName()+”:

”+request.getServerPort()+path+"/”;

StringnickName=(String)session。

getAttribute("username");

if(session。

getAttribute("username")==null){

ﻩresponse.sendRedirect("login。

jsp");

}

Objectgtime=application。

getAttribute(nickName+"gtime”);

if(gtime==null){//第一次

application。

setAttribute(nickName+”gtime",1+"");

}else{

ﻩint gt=Integer。

parseInt((String)gtime);

ﻩgt=gt+1;

ﻩapplication.setAttribute(nickName+"gtime”,gt+””);//增加次数

}

int number=(int)(Math.random()*10);

session.setAttribute("save”,number);

%〉

〈!

DOCTYPEHTMLPUBLIC”-//W3C//DTDHTML4。

01Transitional//EN”>

〈head〉

 〈base href=”〈%=basePath%〉">

  

 

〈metahttp—equiv="cache—control”content="no-cache”〉

<metahttp—equiv="expires”content=”0">

ﻩ〈metahttp—equiv=”keywords”content="keyword1,keyword2,keyword3”>

〈metahttp-equiv="description”content=”Thisismypage”〉

〈!

—-

〈link rel=”stylesheet"type=”text/css”href=”styles。

css"〉

-—>

〈/head>

<script〉

//varrandom=Math.floor(Math。

random()*10);

 //Integer integer=(Integer)session.getAttribute(”save");

varrandom=〈%=session。

getAttribute("save”)%〉;

document.write(random);

 vartime=0;

 functioncheck(){

ﻩvar answers=document。

getElementById(”answer").value;

if(random!

=answers){

  if(random〉answers){

  alert("Sorry!

你所猜的数比实际的数小");time=time+1;

   }else{alert("Sorry!

你所猜的数比实际的数大”);time=time+1; }

 }else{

  alert("恭喜你!

猜对了”);location.href='request。

jsp?

success=true';

}

   if(time==3){

    alert("Sorry!

游戏失败”);location。

href='request。

jsp';

 }

  <body>

 〈center>

 

   请输入你所猜的数字:

  〈FORMname=form > 

〈inputtype="text”id=”answer”/〉

〈inputtype="button"value="送出"onclick=”check()"/〉

  </FORM〉

  </center〉

  〈/body>

〈/html>

排行榜jsp中rank。

jsp

<%@pagelanguage=”java"import="java。

util。

*”pageEncoding=”UTF-8"%>

<%

Stringpath=request。

getContextPath();

String basePath= request.getScheme()+":

//”+request.getServerName()+”:

”+request.getServerPort()+path+"/”;

StringnickName=(String)session。

getAttribute(”user");

if(request。

getParameter("success”)!

=null){

ﻩObjectstime=application。

getAttribute(nickName+"stime”);

if(stime==null){//第一次

ﻩﻩapplication.setAttribute(nickName+"stime”,1+"");

ﻩ}else{

intst=Integer.parseInt((String)stime);

ﻩst=st+1;

ﻩﻩapplication.setAttribute(nickName+"stime”,st+””);//增加次数

}

%〉

<!

DOCTYPEHTML PUBLIC ”-//W3C//DTDHTML4.01Transitional//EN”〉

<html>

 〈head〉

  〈basehref="〈%=basePath%>"〉

  

  〈title〉My JSP ’request.jsp’startingpage

ﻩ〈metahttp—equiv="pragma” content="no-cache"〉

ﻩ〈metahttp—equiv=”expires”content=”0"〉

〈metahttp—equiv=”keywords” content=”keyword1,keyword2,keyword3”〉

ﻩ〈metahttp-equiv="description"content=”Thisismypage”>

——

ﻩ〈linkrel="stylesheet” type="text/css”href="styles。

css">

-->

〈/head>

 <body〉

  〈center〉

   是否继续游戏?

〈br>

 

jsp”>是</a〉 ;&nbsp;&nbsp;

  <ahref="login。

jsp?

exit=true”〉退出

 </center〉

 〈/body〉

结果截图:

1、登陆页面

2、游戏页面:

3、排行榜:

 

 

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

当前位置:首页 > 工程科技 > 交通运输

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

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