This article shares the specific code of JSP to implement the millionaire number guessing game for your reference. The specific content is as follows
Design a web app, each time generate a number less than 30, and give 5 chances for customers to guess this number:
1) If the number guessed by the customer is larger than the generated number value, it will indicate that it is "large".
2) If the number guessed by the customer is smaller than the generated number value, the "small dot" is prompted
If you guess correctly, you will pass the level. If you guess wrong, Game Over will give players a chance to replay.
JSP code:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http:///TR/html4/"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <% String result=(String)("result"); if(result!=null){ ("<font color='red'>"+result+"'</font>"); } %> <% Integer times=(Integer)("times"); if(times!=null){ ("You still have it"+(5-times)+"One chance!"); } %> <br/> <form action="/zxz/zxz" method="POST"> Please enter your number(20the following):<input type="text" name="Lucy" /><br/> <% if(times!=null){ %> <input type="hidden" name="times" value="<%=times %>"/> <% } %> <input type="submit" value="Guess" /> </form> </body> </html>
Servlet code:
package hah; import ; import ; import ; import ; import ; import ; import ; /** * Servlet implementation class zxz */ @WebServlet("/zxz") public class zxz extends HttpServlet { private static final long serialVersionUID = 1L; int answer; public void newGame() { Random random=new Random(); answer=(20); } public zxz() { newGame(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ("text/html;charset=utf-8"); String lucyStr=("Lucy"); Integer lucyNb=null; ("Answer:"+answer); if(!("")) { lucyNb=(lucyStr); } Integer times=1; String timeStr=("times"); if(timeStr!=null&&!("")) { times=(timeStr)+1; } if(times<5) { String result=""; if(lucyNb>answer) { result="Big"; }else if(lucyNb<answer) { result="Small"; }else if(lucyNb==answer) { result="Seemed"; times=null; } ("times", times); ("result", result); }else { newGame(); ().write("game over<a href='"+()+"/'>One more</a>"); return; } ("/").forward(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
Summarize:
a. Use the tag hidden to implicitly pass data without being discovered by the user. It can be used to record times such as:
<input type="hidden" name="times" value="<%=times %>"/>
b. Servlets are used to jump and execute logical code, and JSP is used to display data
c. ("Lucy"); if the parameter does not exist, return the string value of null
d There are two ways to jump. One is to jump the page. The address needs to be written with the project name +jsp or servlet.
The other isForwarding the domain object that shared the request, you can write jsp or servlet directly on the address. Don't have the project name. Moreover, you must add "/" before the project name and jsp or servlet. Otherwise, it will be the relative position.
like:
<form action="/zxz/zxz" method="POST"> //Forward("/"). forward(request, response);
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.