- 先做一隻程式(class),一輸入的參數隨機生出一組數字來,回傳這一組數字(是一個list),這個class叫LotteryBean
- 再做一隻程式(servlet)名叫LotteryServlet,他被召喚後會呼叫LotteryBean、傳入值域(最大49最小值1、一組要幾個數字6)後獲得一組數字,他會把獲得的list放入一個Collection中,再用setAttribute把這個Collection傳入JSP裡。用getRequestDispatcher跟forward來設定要傳到哪一個JSP中跟傳甚麼(/ch01/goodLuck.jsp)
再來著作接收跟傳入資訊的JSP檔
- 第一個queryLottery.jsp,也就是傳出資料的頁面有一個表單form,<Form action="LotteryServlet" method="POST">,表示將表單的資料傳給LotteryServlet這個Servlet來執行
- 第二個goodLuck.jsp,接收LotteryServlet計算好的資料並顯示於螢幕,用${visitName}、${luckyNumber}來抓標記把值放出來
package ch01;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
public class LotteryBean {
private int lowerBound;
private int upperBound;
private int ballNumber;
public LotteryBean() {
}
public void setLowerBound(int lowerBound) {
this.lowerBound = lowerBound;
}
public void setUpperBound(int upperBound) {
this.upperBound = upperBound;
}
public void setBallNumber(int ballNumber) {
this.ballNumber = ballNumber;
}
public Collection<Integer> getLuckyNumbers() {
Set<Integer> set = new TreeSet<Integer>();
//TreeSet是一个有序集合,她的元素按照升序排列,默认是按照自然顺序排列
while (set.size() < ballNumber ) {
int num = (int)(Math.random()* (upperBound-lowerBound+1) + lowerBound);
set.add(num);
}
return set;
}
public Collection<Integer> getFourStars() {
List<Integer> list = new ArrayList<Integer>();
while (list.size() < ballNumber ) {
int num = (int)(Math.random()* (upperBound-lowerBound+1) + lowerBound);
list.add(num);
}
return list;
}
}
package ch01;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Collection;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class LotteryServlet
* HttpServlet繼承自GenericServlet,GenericServlet是Servlet這個Interface的實作
*/
@WebServlet("/ch01/LotteryServlet")//別人呼叫這個class時需要使用的關鍵字?
public class LotteryServlet extends HttpServlet {
private static final long serialVersionUID = 1L;//不懂這句用意
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processRequest(request,response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processRequest(request,response);
}
private void processRequest(HttpServletRequest request,//為何自動生成private?
HttpServletResponse response) throws ServletException, IOException {
try {request.setCharacterEncoding("UTF-8");//說明傳送到本程式的資料內碼
String name=request.getParameter("visitor");//讀取瀏覽器送來的資料?
if(name==null||name.trim().length()==0){
//String java.lang.String.trim()
// Returns a string whose value is this string,
// with any leading and trailing whitespace removed.
name="訪客";
}
//如果讀不到使用者傳送的資料,將name設為"訪客"
LotteryBean lottery = new LotteryBean();
lottery.setLowerBound(1);//最小數字1
lottery.setUpperBound(49);//最大數字49
lottery.setBallNumber(6);//出現六個數字一組
Collection coll = lottery.getLuckyNumbers();//產生所需結果
request.setAttribute("visitName", name);
request.setAttribute("luckyNumber", coll);//把用servlet算好的結果放入HttpServletRequest的request中
RequestDispatcher rd = request.getRequestDispatcher("/ch01/goodLuck.jsp");
//再建一個goodLuck.jsp檔
//Dispatcher:發送者
rd.forward(request, response);
//Forward:發送
return;//forward之後會有一個return敘述←?
}catch (UnsupportedEncodingException e) {
throw new ServletException(e);
}
}
}
<%@ 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>查詢明牌</title>
</head>
<body>
<h2>查詢明牌</h2>
<Form action="LotteryServlet" method="POST">
<!-- action是表單(form)專用的方法,用以連結到接受資料的程式
method有post跟get,GET的資料會顯示在url上、post的不會-->
訪客姓名:<input type="text" name="visitor" size="10"><p/>
<!-- name主要是用於獲取提交表單的某表單域信息, 作為可與伺服器交互數據的HTML元素的服務器端的標記 -->
<input type="submit" value="確定"><p/>
</Form>
</body>
</html>
<%@ 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>報明牌</title><!-- 這是結果回傳頁 -->
</head>
<body>
<H2>卜籤求明牌</H2>
${visitName},您好,<BR>
您的明牌為:${luckyNumber}<!-- 金錢符號用法類似window.onload,就是先載入執行再繼續的意思 -->
</body>
</html>