好文档 - 专业文书写作范文服务资料分享网站

Java - Servlet入门教程 

天下 分享 时间: 加入收藏 我要投稿 点赞

out.println(\ out.println(\

HttpSession session=request.getSession(true); //获取客户的会话对象 String item =request.getParameter(\称。

session.removeAttribute(item); //删除商品。 //将购物车中的商品显示给客户:

out.println(\ Enumeration enum=session.getAttributeNames(); while(enum.hasMoreElements())

{ String name=(String)enum.nextElement();

out.print(\ } }

public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException { doPost(request,response); } }

7.8.3 猜数字

在第3章、第6章讲述JSP内置对象以及javabeans时,曾分别举过猜数字的例子。在这里,我们再使用servlet来实现猜数字这个小游戏,这样,我们就用3种方式实现了这个小游戏:直接由JSP页面来实现、通过javabeans来实现、通过servlet来实现。

当客户访问servlet:GetNumber时,随机分配给客户一个1到100之间的数,然后将这个数字存在客户的session对象中。客户在表单里输入一个数,来猜测分配给自己的那个数字。客户输入一个数字后,提交给servlet:Result,该servlet负责判断这个数是否和客户的session对象中存放的那个数字相同,如果相同就连接到servlet:Success;如果不相同就连接到servlet: Large或Small。然后,客户在这些servlet中重新提交数字到Result。

servlet源文件 GetNumber.java: import java.io.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*;

public class GetNumber extends HttpServlet

{ public void init(ServletConfig config) throws ServletException {super.init(config);

46

}

public void doPost(HttpServletRequest request,HttpServletResponse response)

throws ServletException,IOException { response.setContentType(\

ServletOutputStream out=response.getOutputStream();

out.print(\ HttpSession session=request.getSession(true); session.setAttribute(\

int number=(int)(Math.random()*100)+1; //获取一个随机数。 session.setAttribute(\ out.print(\ out.print(\ out.print(\ out.print(\ }

public void doGet(HttpServletRequest request,HttpServletResponse response)

throws ServletException,IOException { doPost(request,response); } }

Result.java: import java.io.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*;

public class Result extends HttpServlet

{ public void init(ServletConfig config) throws ServletException {super.init(config); }

public void doPost(HttpServletRequest request,HttpServletResponse response)

throws ServletException,IOException { response.setContentType(\

ServletOutputStream out=response.getOutputStream(); HttpSession session=request.getSession(true);

47

String str=request.getParameter(\ if(str==null) {str=\ }

int guessNumber=Integer.parseInt(str);

Integer integer=(Integer)session.getAttribute(\ int realnumber=integer.intValue(); if(guessNumber==realnumber)

{ int n=((Integer)session.getAttribute(\ n=n+1;

session.setAttribute(\ response.sendRedirect(\ }

else if(guessNumber>realnumber)

{ int n=((Integer)session.getAttribute(\ n=n+1;

session.setAttribute(\ response.sendRedirect(\ }

else if(guessNumber

{ int n=((Integer)session.getAttribute(\ n=n+1;

session.setAttribute(\ response.sendRedirect(\ } }

public void doGet(HttpServletRequest request,HttpServletResponse response)

throws ServletException,IOException { doPost(request,response); } }

Larger.java: import java.io.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*;

48

public class Larger extends HttpServlet

{ public void init(ServletConfig config) throws ServletException {super.init(config); }

public void doPost(HttpServletRequest request,HttpServletResponse response)

throws ServletException,IOException { response.setContentType(\

ServletOutputStream out=response.getOutputStream();

out.print(\。 out.print(\ost name=form>\ out.print(\ out.print(%ubmit value=Enter>\ out.print(\ }

public void doGet(HttpServletRequest request,HttpServletResponse response)

throws ServletException,IOException { doPost(request,response); } }

Smaller.java import java.io.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*;

public class Smaller extends HttpServlet {

public void init(ServletConfig config) throws ServletException {super.init(config); }

public void doPost(HttpServletRequest request,HttpServletResponse response)

throws ServletException,IOException { response.setContentType(\

ServletOutputStream out=response.getOutputStream();

out.print(\

49

out.print(\ out.print(\ out.print(\ out.print(\ }

public void doGet(HttpServletRequest request,HttpServletResponse response)

throws ServletException,IOException { doPost(request,response); } }

Success.java import java.io.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*;

public class Success extends HttpServlet {

public void init(ServletConfig config) throws ServletException {super.init(config); }

public void doPost(HttpServletRequest request,HttpServletResponse response)

throws ServletException,IOException { response.setContentType(\

ServletOutputStream out=response.getOutputStream(); HttpSession session=request.getSession(true);

int count=((Integer)session.getAttribute(\ int num=((Integer)session.getAttribute(\ long startTime=session.getCreationTime(); long endTime=session.getLastAccessedTime(); long spendTime=(endTime-startTime)/1000; out.println(\ out.println(\ out.println(\ out.println(\ }

50

Java - Servlet入门教程 

out.println(\out.println(\HttpSessionsession=request.getSession(true);//获取客户的会话对象Stringitem=request.getParameter(\称。session.removeAttr
推荐度:
点击下载文档文档为doc格式
3hele9681999g5n14byc
领取福利

微信扫码领取福利

微信扫码分享