天天看點

Java Web簡明教程–Servlet篇[6]–漂亮的終結篇

OK,在Servlet篇的最後,貓哥介紹兩個東西${}和<c:forEach>。

${},學名“EL表達式”,功能強大,具體負責将Servlet中request和response對象中的屬性值取出來。

舉個栗子:

在Servlet中有這麼一句request.setAtrribute("className","二年級三班");

在網頁中可以這樣取值${className}

太簡單了!

1

2

3

4

5

OK,當然,最常用的還是循環,借助EL表達式和<c:forEach>,就可以解決。

<c:forEach>屬于JSP标準标簽庫(JSTL),用來周遊。

好的,現在舉個例子,有三個檔案,classStudent.html用來選擇班級,ClassServlet用來處理classStudent.html送出的請求,classStudentList.jsp用來顯示結果,三個檔案代碼如下:

<!DOCTYPE html>

<html>

 <head>

   <title>classStudent.html</title>

   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><!-- 本行用來設定編碼 -->

 </head>

 <body>

    <form name="mainForm" method="post" action="/ServletDemo/ClassServlet"><!-- 表單送出給ClassServlet-->

       請選擇要查詢的班級:

       <select name="class">

        <!--此處注意分别對應class_info表的class_id和class_name-->

        <!--實際上這三個班級資訊也應該是從資料庫中查出,此處為了友善示範直接寫死,因為我們要示範的重點是Servlet輸出資料到網頁-->

           <option value="1">一班</option>

           <option value="2">二班</option>

           <option value="3">三班</option>

       </select>

       <br/>

       <input type="submit"></input>

   </form>

 </body>

</html>

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

package servlet;

import java.io.IOException;

import java.io.PrintWriter;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import entity.StudentInfo;

import operation.StudentOperation;

/**

* ClassServlet類

* @author 貓哥

*/

public class ClassServlet extends HttpServlet {

public ClassServlet() {

 super();

}

public void doPost(HttpServletRequest request, HttpServletResponse response)

  throws ServletException, IOException {

 response.setContentType("text/html");//設定輸入格式

 response.setContentType("text/html");

    request.setCharacterEncoding("utf-8");

    response.setCharacterEncoding("utf-8");

    String classId=request.getParameter("class");//擷取要查詢的班級id

    //此處要從資料庫中查詢classId對應班級的學生資訊

    StudentOperation stuOper=new StudentOperation();

    //将班級的學生資訊輸出

 List students=stuOper.selectByClassId(classId);

 request.setAttribute("students", students);

 //隻能跳轉本Web項目網頁,可以request.setAttribute傳值,看見後面request作為參數了?

 request.getRequestDispatcher("/classStudentList.jsp").forward(request,response);

 //跳轉任何路徑,無法使用request.setAttribute來傳值,是以此處使用不行。

 //response.sendRedirect("/classStudentList.jsp");

}

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %><!-- 使用c:标簽需要添加本行代碼 -->

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

   <title>classStudentList.jsp</title>

  該班的學生有:<br/>

   <c:forEach items="${students}" var="item">

    姓名:${item.studentName}  

    班級:${item.studentClass.className}<br/>

   </c:forEach>

   該班的學生有:<br/>

   <table border="1">

    <c:forEach items="${students}" var="item">

    <tr>

    <td>姓名:${item.studentName}</td>

    <td>${item.studentClass.className}</td>

   </table>

可見使用了JSP标準标簽庫和EL表達式後,網頁代碼中隻剩各類标簽和表達式,可以說基本實作了前後端的分離,從此JSP或HTML負責前台顯示及從背景取數,Servlet負責邏輯處理及資料庫查詢或修改操作。各司其職。