基于JavaWeb的产生指定范围随机数的小程序

效果图:界面很简洁,没有对界面进行修饰

结构图:

jiekou.java

package abc;

public interface jiekou {
	
	public int cssjs(int a,int b);		//产生随机数
	
}

jiekoushixian.java

package abc;
import java.util.Random;


public class jiekoushixian implements jiekou{

	@Override
	public int cssjs(int a, int b) {
		// TODO Auto-generated method stub
		Random rand = new Random();  
		int zhi;
//		zhi=rand.nextInt(b)+(a+1);
		zhi = rand.nextInt(b)%(b-a+1) + a;
		
		System.out.println("zhi:"+a);
		
	  /*    for(int i=0; i<10; i++) {  
	    	  
	       System.out.println(rand.nextInt(100) + 1);  
	      }  */
		return zhi;
	}

}

quedingAction.java

package action;

import java.io.IOException;

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

import org.apache.struts2.interceptor.ServletRequestAware;

import abc.jiekoushixian;

import com.opensymphony.xwork2.ActionSupport;

public class quedingAction extends ActionSupport implements ServletRequestAware{
	HttpServletRequest request;
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public String registUser() throws Exception{
		
		String a = request.getParameter("one");				//以字符串的形式获取前台所给出的值
		String b = request.getParameter("two");
	
	//	System.out.println("one:"+a+" two:"+b);
		
		jiekoushixian jiekoushixian1 = new jiekoushixian();		//实例化实现类
		int a1=Integer.valueOf(a);								//将获取的字符串形式的数据转换成int
		int b1=Integer.valueOf(b);
//		System.out.println("(int)_one:"+a+" two:"+b);
		int zhi = jiekoushixian1.cssjs(a1, b1);					//调用方法求随机数
		String zhi1 = String.valueOf(zhi);						
		System.out.println("action:"+zhi1);
		request.setAttribute("kind", zhi1);						//将所求的随机数传给前台
		
		request.setAttribute("one", a);	
		request.setAttribute("two", b);	
		
		
		request.setAttribute("frist", "YES");					//传给前台frist变量为YES,表示非非第一次显示index界面
		return "regist";										
	}

	@Override
	public void setServletRequest(HttpServletRequest request) {
		// TODO Auto-generated method stub
		this.request = request;
	}
}

struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
	"http://struts/apache.org/dtds/struts-2.0.dtd">
	
	<struts>
	<package name ="ch10" namespace="/user" extends="struts-default">
	<action name = "regist" class = "action.quedingAction" method = "registUser">
		<result name = "regist">/index.jsp</result>
	</action>
		
	</package>
	
	</struts>

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>


	
	<form action="user/regist" method="post">
	选择范围
	

  	
	
<!-- 	<input type = "text" name = "zhi" value = "53"> -->


<% 	
	int a=1;
	String frist = (String)request.getAttribute("frist");
	if(frist!="YES"){
		System.out.println("NO");
		%>
		<input type="text" name="one">~<input type = "text" name = "two"><br>
		<input type="submit" value="确定" /><br>
		结果:
		<input type = "text" name = "zhi" >
		<% 
	}
	else if(frist.equals("YES")){
	  	String kind = (String)request.getAttribute("kind");
	 	System.out.println("jsp:"+kind);
	 	int zhi=Integer.valueOf(kind);
	 	String Sone = (String)request.getAttribute("one");
	 	String Stwo = (String)request.getAttribute("two");
	 	
	 	int ione=Integer.valueOf(Sone);
	 	int itwo=Integer.valueOf(Stwo);
%>
	<input type="text" name="one" value = "<%=ione%>">~<input type = "text" name = "two" value = "<%=itwo%>"><br>
	<input type="submit" value="确定" /><br>
	结果:
	<input type = "text" name = "zhi" value = "<%=zhi%>">
<%
	}
%>
</form>
	
	
</body>
</html>

<!-- <form action="user/regist" method="post"> -->

<!--   <input type="submit" value="Submit" /> -->
<!-- </form> -->

猜你喜欢

转载自blog.csdn.net/weixin_38891150/article/details/79944166