選單頁面:

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

   pageEncoding="UTF-8"%>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html"; charset=UTF-8>

<title>JSP課堂作業一</title>

</head>

<body>

   <h2>SP課堂作業一</h2>

   <a href='ch01/queryLottery.jsp'>問單一明牌</a><br>

   <!-- 從本資料夾(webapp)往下找ch01裡面的queryLottery.jsp -->

   <a href='ch01/queryLottery2.jsp'>問多種明牌</a><br>

   <a href='ch02/InsertMemberForm.jsp'>加入會員</a><br>

   <a href='ch04_01/InsertMemberForm.jsp'>加入會員(MVC FileIO)</a><br>

   <a href='ch04_02/InsertMemberForm.jsp'>加入會員(MVC DAO)</a><br>

</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>

<Form action="LotteryServlet" method="POST">

<!-- action是表單(form)專用的方法,用以連結到接受資料的程式

methodpostgetGET的資料會顯示在url上、post的不會

post代表會呼叫到doPost這個方法裡-->

 

訪客姓名:<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>

<Form  action="LotteryServlet2" method="POST">

      訪客姓名:<input type="text" name="visitor"  size="10"><BR><P/>

      <!-- 下面是一個單選表單,被選中的才會回傳value

      servlet中用getParameter這個方法取的他的value -->

   <input type="radio" name="numberType" value="BIG" CHECKED="">大樂透(六個介於1-49的數字)<br>

   <input type="radio" name="numberType" value="MID">中樂透(六個介於1-42的數字)<br>

   <input type="radio" name="numberType" value="SMALL">小樂透(五個介於1-39的數字)<br>

   <input type="radio" name="numberType" value="FOURSTARS">四星彩(四個可重複,介於0-9的數字)<br>  

   <P/>

   <input type="submit" value="確定"> 

</Form>

</body>

</html>


單一明牌servlet:

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繼承自GenericServletGenericServletServlet這個Interface的實作

 */

@WebServlet("/ch01/LotteryServlet")//別人呼叫這個class時需要使用的關鍵字、URL

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");

      //讀取瀏覽器送來的資料,也就是在queryLottery.jspname="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算好的結果放入HttpServletRequestrequest

//    之後在goodLuck.jsp裡面,${visitName}的地方放name${luckyNumber}的地方放coll

      RequestDispatcher rd = request.getRequestDispatcher("/ch01/goodLuck.jsp");

      //Dispatcher:發送者、分派者

      //getRequestDispatcher這個方法將收到的結果集傳送到/ch01/goodLuck.jsp

      //斜線開頭的路徑為絕對路徑,所以不會因為本servlet(LotteryServlet.java)換位置而出錯

      rd.forward(request, response);

      //forward:發送

      return;//forward之後會有一個return敘述,使不論是否出錯都於此停止,因為要跳頁了

      //就算出錯也不管了

      }catch (UnsupportedEncodingException e) {

          throw new ServletException(e);

      }

   }

 

}


多明牌servlet

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;

 

@WebServlet("/ch01/LotteryServlet2")

public class LotteryServlet2 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,

          HttpServletResponse response) throws ServletException, IOException {

      try {

          request.setCharacterEncoding("UTF-8");

          String name = request.getParameter("visitor");

          if (name == null || name.trim().length() == 0) {

             name = "訪客";

          }

          String type = request.getParameter("numberType");

          //numberType是一個單選欄位,取得他的欄位內容(取得使用者的選項)

          //所以type中存放的是被選重的numberTypevalue

          Collection<Integer> coll = null;

          LotteryBean lottery = new LotteryBean();

          if (type.equalsIgnoreCase("FOURSTARS")) {

             //依使用者的選擇不同而輸出不同結果

             //equalsIgnoreCase:將此 String 與另一個 String 比較,不考慮大小寫。

             lottery.setLowerBound(0);

             lottery.setUpperBound(9);

             lottery.setBallNumber(4);

             coll = lottery.getFourStars();

          } else if (type.equalsIgnoreCase("BIG")) {

             lottery.setLowerBound(1);

             lottery.setUpperBound(49);

             lottery.setBallNumber(6);

             coll = lottery.getLuckyNumbers();

          } else if (type.equalsIgnoreCase("MID")) {

             lottery.setLowerBound(1);

             lottery.setUpperBound(42);

             lottery.setBallNumber(6);

             coll = lottery.getLuckyNumbers();

          } else if (type.equalsIgnoreCase("SMALL")) {

             lottery.setLowerBound(1);

             lottery.setUpperBound(39);

             lottery.setBallNumber(5);

             coll = lottery.getLuckyNumbers();

          }

 

          request.setAttribute("visitName", name);

          request.setAttribute("luckyNumber", coll);

          RequestDispatcher rd = request.getRequestDispatcher("/ch01/goodLuck.jsp");//將收到的資料傳送到這個地方

          rd.forward(request, response);

          //把結果傳送出去,並跳到goodLuck.jsp頁面

          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>

${visitName},您好,

您的明牌為:${luckyNumber}<!-- 金錢符號用法類似window.onload,就是先載入執行再繼續的意思 -->

</body>

</html>

arrow
arrow
    全站熱搜

    乙方 發表在 痞客邦 留言(0) 人氣()