JSP作业二.docx
- 文档编号:30664587
- 上传时间:2023-08-19
- 格式:DOCX
- 页数:20
- 大小:92.47KB
JSP作业二.docx
《JSP作业二.docx》由会员分享,可在线阅读,更多相关《JSP作业二.docx(20页珍藏版)》请在冰豆网上搜索。
JSP作业二
JSP作业
4-2编写一个实现页面计数的计数器,要求当刷新页面时,不增加计数。
程序代码
<%@pagecontentType="text/html;charset=gb2312"%>
<%!
intnum=0;
synchronizedvoidadd(){
num++;
}
%>
<%if(session.isNew())
add();
%>
截图
4-3编写一个简易购物车,实现向购物车内添加商品,移除指定商品及清空购物车功能。
程序代码
<%@pagecontentType="text/html;charset=gb2312"%>
<%@pageimport="java.util.ArrayList"%>
<%!
intm=0;
intId=8888;
ArrayList
ArrayList
ArrayList
ArrayList
ArrayList
%>
<%floattotal=0;%>
<%
if(Id==8888)
{
goodslist.add(0,"荔枝");
goodslist.add(1,"火龙果");
goodslist.add(2,"西瓜");
goodslist.add(3,"柚子");
pgoodslist.add(0,8.8f);
pgoodslist.add(1,9.6f);
pgoodslist.add(2,5.2f);
pgoodslist.add(3,4.8f);
}
%>
<%
session.setAttribute("goodslist",goodslist);
session.setAttribute("pgoodslist",pgoodslist);
%>
<%session.getAttribute("goodslist");
session.getAttribute("pgoodslist");%>
<%
if(goodslist==null||goodslist.size()==0)
{
%>
<%
}
else
{
for(intm=0;m { %><%=goodslist.get(m)%>
id=<%=m+1%>">购买
<%}
}
%>
**************************************************************************
<%
Stringstr=request.getParameter("id");
if(str==null||str.equals(""))
str="0";
try{
Id=Integer.parseInt(str);
}
catch(NumberFormatExceptione)
{
Id=0;
e.printStackTrace();
}
%>
<%
if(Id==1)
{
if(buylist.size()==0)
{
buylist.add(0,goodslist.get(0));
pbuylist.add(0,pgoodslist.get(0));
nbuylist.add(0,1);
}
else
{
intk=0;
for(;k { if(buylist.get(k).equals(goodslist.get(0))) { intw=nbuylist.get(k); nbuylist.set(k,w+1); break; } } if(k>=buylist.size()) { buylist.add(k,goodslist.get(0)); pbuylist.add(k,pgoodslist.get(0)); nbuylist.add(k,1); } } } if(Id==2) { if(buylist.size()==0) { buylist.add(0,goodslist.get (1)); pbuylist.add(0,pgoodslist.get (1)); nbuylist.add(0,1); } else { intk=0; for(;k { if(buylist.get(k).equals(goodslist.get (1))) { intw=nbuylist.get(k); nbuylist.set(k,w+1); break; } } if(k>=buylist.size()) { buylist.add(k,goodslist.get (1)); pbuylist.add(k,pgoodslist.get (1)); nbuylist.add(k,1); } } } if(Id==3) { if(buylist.size()==0) { buylist.add(0,goodslist.get (2)); pbuylist.add(0,pgoodslist.get (2)); nbuylist.add(0,1); } else { intk=0; for(;k { if(buylist.get(k)==goodslist.get (2)) { intw=nbuylist.get(k); nbuylist.set(k,w+1); break; } } if(k>=buylist.size()) { buylist.add(k,goodslist.get (2)); pbuylist.add(k,pgoodslist.get (2)); nbuylist.add(k,1); } } } if(Id==4) { if(buylist.size()==0) { buylist.add(0,goodslist.get(3)); pbuylist.add(0,pgoodslist.get(3)); nbuylist.add(0,1); } else { intk=0; for(;k { if(buylist.get(k)==goodslist.get(3)) { intw=nbuylist.get(k); nbuylist.set(k,w+1); break; } } if(k>=buylist.size()) { buylist.add(k,goodslist.get(3)); pbuylist.add(k,pgoodslist.get(3)); nbuylist.add(k,1); } } } if(Id==10) { if(nbuylist.get(0)>1) { inth=nbuylist.get(0); nbuylist.set(0,h-1); } elseif(nbuylist.get(0)==1) { buylist.remove(0); pbuylist.remove(0); nbuylist.remove(0); } } if(Id==20) { if(nbuylist.get (1)>1) { inth=nbuylist.get (1); nbuylist.set(1,h-1); } elseif(nbuylist.get (1)==1) { buylist.remove (1); pbuylist.remove (1); nbuylist.remove (1); } } if(Id==30) { if(nbuylist.get (2)>1) { inth=nbuylist.get (2); nbuylist.set(2,h-1); } elseif(nbuylist.get (2)==1) { buylist.remove (2); pbuylist.remove (2); nbuylist.remove (2); } } if(Id==40) { if(nbuylist.get(3)>1) { inth=nbuylist.get(3); nbuylist.set(3,h-1); } elseif(nbuylist.get(3)==1) { buylist.remove(3); pbuylist.remove(3); nbuylist.remove(3); } } if(Id==888888) { buylist.clear(); pbuylist.clear(); nbuylist.clear(); total=0; } if(buylist==null||buylist.size()==0) { %> <% } else { for(intl=0;l { Stringname=buylist.get(l); floatprice=(float)pbuylist.get(l); intnum=(int)nbuylist.get(l); floatmoney=((int)((price*num+0.05f)*10))/10f; total+=money; %> id=<%=((l+1)*10)%>">移除<%=name%> <%=price%> <%=num%> <%=money%>
<%}
}%>
<%=total%>
id=888888">清空购物车 截图 5-3实现一个简单的登录程序。 要求应用JavaBean来接收用户输入的用户名和密码,然后判断输入的用户名是否为“admin”,密码是否为“000”;若是,则转发到success.jsp页面显示“欢迎登录”提示信息,否则转发到fault.jsp页面显示“登录失败”提示信息。 程序代码: User.java文件: packagecom.yxq.bean; publicclassUser{ privateStringusername; privateStringuserpass; publicStringgetUsername(){ returnusername; } publicvoidsetUsername(Stringusername){ this.username=username; } publicStringgetUserpass(){ returnuserpass; } publicvoidsetUserpass(Stringuserpass){ this.userpass=userpass; } } index.jsp文件: <%@pagecontentType="text/html;charset=gb2312"%> 用户名: 密 码: Dologon.jsp文件: <%@pagecontentType="text/html;charset=gb2312"%> useBeanid="user"class="com.yxq.bean.User"> setPropertyname="user"property="*"/> useBean> <% Stringusername=user.getUsername(); Stringuserpass=user.getUserpass(); if(username.equals("admin")&&userpass.equals("000")) { response.sendRedirect("success.jsp"); } else { response.sendRedirect("fault.jsp"); } %> success.jsp文件: <%@pagecontentType="text/html;charset=gb2312"%> <% out.println("欢迎登陆"); %> fault.jsp文件: <%@pagecontentType="text/html;charset=gb2312"%> <% out.println("登陆失败"); %> 运行结果: 6-1创建一个Servlet。 要求通过在浏览器地址栏中访问该Servlet后,输出一个一行一列表格,表格中的内容为“爱护环境,保护地球”。 程序代码 packagecom; importjava.io.IOException; importjava.io.PrintWriter; importjavax.servlet.ServletException; importjavax.servlet.http.HttpServlet; importjavax.servlet.http.HttpServletRequest; importjavax.servlet.http.HttpServletResponse; publicclassservletextendsHttpServlet { publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse) throwsServletException,IOException{ response.setContentType("text/html;charset=gb2312"); PrintWriterout=response.getWriter(); out.println(""); out.println(""); out.println(" out.println(" out.println(" out.println("爱护环境,保护地球"); out.println(" } } 截图 6-2实现一个简单的登录程序。 要求由Servlet接收用户输入的用户名和密码,然后输出到页面中。 程序代码 index.jsp <%@pagecontentType="text/html;charset=gb2312"%>"); ");"); 用户登录
用户名:
密 码:
servlet.java
packagecom;
importjava.io.IOException;
importjavax.servlet.ServletException;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importjava.io.PrintWriter;
publicclassservletextendsHttpServlet
{
publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)
throwsServletException,IOException
{
this.doGet(request,response);
}
publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)
throwsServletException,IOException
{
response.setContentType("text/html;charset=gb2312");
Stringusername=request.getParameter("username");
Stringuserpass=request.getParameter("userpass");
PrintWriterout=response.getWriter();
out.println("");
out.println("用户名:
");
out.println(username);
out.println("密码:
");
out.println(userpass);
out.println("");
}
}
截图
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- JSP 作业