Servlet学习以及使用

近期任务安排

今天的内容是继续理解昨天的CRUD的写法,然后采用类似的方法重写猜数字的游戏,以巩固认知。今天是最后一次集中练习javaweb的内容了。
上午我讲一下反射的基本内容,在马士兵的视频里有相关章节。
明天我们就按照类似现在的crud的模式,结合反射和xml操作,自己写一个简单的mvc框架。
后天就正式开始学习struts2.



今天主要使用Servlet重新写了猜数字游戏:
自己写的源代码:
List.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
   
    <title>欢迎进入猜数字游戏</title>
   
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">   
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

  </head>
 
  <body>
   <div align=center><h1>NumberGuess</h1></div>
   <p></p>
   <p></p>
    <hr width=80%>
    <div align="center"><a href="<%=request.getContextPath() %>/NumberGuessServlet?act=NewGuess">新游戏</a></div>
    <div align=center>
       <form name=f action="<%=request.getContextPath() %>/NumberGuessServlet?act=guess" method="post">
          <input type="text" size=8  name="number">
          <input type="submit" value="guess">
       </form>
    </div>
  </body>
</html>


Result.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
   
    <title>My JSP 'result.jsp' starting page</title>
   
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">   
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

  </head>
 
  <body>
  <div align=center><P>结果显示</P></div>
  <p></p>
  <p></p>
  <hr width="80%"></hr>
  <div align=center>${content }</div>
  <div align=center> 你总共猜了${GuessTimes }次</div>
  <div align=center>总共有${times}人玩过此游戏</div>
  <div align=center><a href='<%=request.getContextPath()%>/NumberGuessServlet?act=new'>返回重新猜</a></div>
 
 
 
  </body>
</html>


Servlet文件:
NumberGuessServlet1.java(此servlet写的繁琐了)

package com.yuqiao.qiu;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Random;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class NumberGuessServlet1 extends HttpServlet {

public  NumberGuessServlet1(){

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



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


// request.getSession().removeAttribute("randomNo");

String act=request.getParameter("act");
// Integer randomNo1=(Integer)request.getSession().getAttribute("randomNo");

if(null==request.getSession().getAttribute("randomNo")||"NewGuess".equals(act))
{
Random random=new Random();
Integer randomNo=random.nextInt(101);
System.out.print(randomNo);
request.getSession().removeAttribute("GuessTimes");
request.getSession().setAttribute("randomNo", randomNo);

if(null==this.getServletContext().getAttribute("times")){

this.getServletContext().setAttribute("times", 0);
}
else{
Integer times=(Integer)this.getServletContext().getAttribute("times");
++times;
this.getServletContext().setAttribute("times",times);
}
if("NewGuess".equals(act))
{
// this.getServletContext().setAttribute("times",times);
request.getRequestDispatcher("/list.jsp").forward(request, response);
}
}

if("guess".equals(act))
{
Integer number=Integer.parseInt(request.getParameter("number").trim());
request.getSession().setAttribute("number", number);
Integer GuessTimes=(Integer)request.getSession().getAttribute("GuessTimes");
if(GuessTimes == null)GuessTimes = 0;
GuessTimes+=1;
request.getSession().setAttribute("GuessTimes", GuessTimes);

if(number<(Integer)request.getSession().getAttribute("randomNo")){
request.setAttribute("content", "对不起你猜的数字小了");
}
if(number>(Integer)request.getSession().getAttribute("randomNo")){
request.setAttribute("content", "对不起你猜的数字大了");
}
if(number==(Integer)request.getSession().getAttribute("randomNo")){
// request.getSession().setAttribute("number", number);
request.getSession().removeAttribute("randomNo");
request.setAttribute("content", "恭喜你,你猜对了");
}
request.getRequestDispatcher("/result.jsp").forward(request, response);
}
if("new".equals(act))
{
request.getRequestDispatcher("/list.jsp").forward(request, response);
}

}


@Override
// protected void service(HttpServletRequest request, HttpServletResponse response)
// throws ServletException, IOException {
//
// }
//
// public Integer StringToInt(String number)
// {
// return Integer.parseInt(number);
// }

public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
public void init() throws ServletException {
// Put your code here
}


}


Web.xml配置文件

  <servlet>
    <servlet-name>NumberGuessServlet</servlet-name>
    <servlet-class>com.yuqiao.qiu.NumberGuessServlet1</servlet-class>
  </servlet>
 
 
  <servlet-mapping>
    <servlet-name>NumberGuessServlet</servlet-name>
    <url-pattern>/NumberGuessServlet</url-pattern>
  </servlet-mapping>
 


其中写的时候遇到的问题挺多:
1. 对于Servlet路径的问题!(超链接如何链接到servlet,还有request.getRequestDispatcher(路径).farward();的路径问题)
2. 其中超链接是使用doGet()方式提交的 所以要重写doGet()方法,不然会报错。
doGet()方法里面直接调用doPost()方法。
3. 出现java.lang.IllegalStateException:   Cannot   forward   after   response   has   been   committed 错误。主要原因是进行了两次转向。
4.其他都是小问题(就不写了)


你可能感兴趣的:(java,游戏,jsp,mvc,servlet)