【Java web】猜字母-jsp与servlet的相互转换【上课作业】

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




首页


猜数字游戏 进入游戏

number.java

package lyy_2;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class number
 */
@WebServlet("/number")
public class number extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public number() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		HttpSession session = request.getSession();
		char rand = (char)(Math.random()*26+64);
		System.out.print(rand);
		session.setAttribute("rand", new Character(rand));
		request.setAttribute("message", "please guess the character");
		request.getRequestDispatcher("game.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}


game.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%
    String message = (String)request.getAttribute("message");
    %>




Insert title here


	

guess the character!

guess the character(a to z)
get some prompts <%=message%>
Remake the game Remake

guess.java

package lyy_2;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class guess
 */
@WebServlet("/guess")
public class guess extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public guess() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		HttpSession session = request.getSession();
		Character realc = (Character)session.getAttribute("rand");
		String str = request.getParameter("cc");
		if(str == null || str.equals("")) {
			request.setAttribute("message", "please guess the character");
		}else {
			Character guessc = str.trim().charAt(0);
			
			if(Character.isUpperCase(guessc)) {
				guessc = Character.toLowerCase(guessc);
			}
			if(guessc > realc) {
				request.setAttribute("message", "too big");
			}
			if(guessc < realc) {
				request.setAttribute("message", "too small");
			}
			if(guessc.equals(realc)) {
				request.setAttribute("message", "Right!");
			}
		}
		request.getRequestDispatcher("game.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}


【Java web】猜字母-jsp与servlet的相互转换【上课作业】_第1张图片
【Java web】猜字母-jsp与servlet的相互转换【上课作业】_第2张图片
【Java web】猜字母-jsp与servlet的相互转换【上课作业】_第3张图片
【Java web】猜字母-jsp与servlet的相互转换【上课作业】_第4张图片
【Java web】猜字母-jsp与servlet的相互转换【上课作业】_第5张图片

你可能感兴趣的:(前后端,servlet,java,前端)