萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網路程式設計 >> JSP 程式設計

JSP 基礎語法與簡單表單處理完整教學

JSP 語法要素速查

1. 指令元素

指令屬性說明
pagelanguage="java"指定腳本語言(唯一支援 java)
contentType="text/html;charset=UTF-8"回應 MIME 類型與編碼
import="java.util.*, com.example.*"匯入 Java 套件
pageEncoding="UTF-8"JSP 檔案原始碼編碼
errorPage="error.jsp"例外導向錯誤頁
isELIgnored="false"啟用 EL 表達式
includefile="path"編譯期靜態引入檔案
taglibprefix="c" uri="https://java.sun.com/jsp/jstl/core"引入標籤庫

2. 標準動作元素

標籤功能關鍵屬性
jsp:forward伺服器端轉發page
jsp:include動態引入頁面輸出page, flush
jsp:param傳遞參數name, value
jsp:useBean建立/尋找 Beanid, scope, class, type
jsp:setProperty設定 Bean 屬性name, property, value, param
jsp:getProperty輸出 Bean 屬性name, property
jsp:pluginJava Applet 部署(已淘汰)type, code, codebase

3. 內建物件

物件類型常用方法
requestHttpServletRequestgetParameter(), getAttribute(), setAttribute(), getHeader(), getRemoteAddr()
responseHttpServletResponsesendRedirect(), setContentType(), addCookie(), setHeader()
outJspWriterprint(), println(), clear(), flush()
sessionHttpSessionsetAttribute(), getAttribute(), invalidate(), getId()
applicationServletContextsetAttribute(), getAttribute(), getRealPath(), getResourceAsStream()
configServletConfiggetInitParameter(), getServletContext()
pageContextPageContextfindAttribute(), getRequest(), getSession(), forward(), include()
pageObject (this)當前 Servlet 實例
exceptionThrowable錯誤頁面可用

完整表單處理範例

<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" %>
<html><head><title>表單處理示範</title></head><body>
<form name="frm" method="POST" action="process.jsp">
  <table>
    <tr><td>用戶名:</td><td><input type="text" name="username"></td></tr>
    <tr><td>密碼:</td><td><input type="password" name="password"></td></tr>
    <tr><td>性別:</td><td>
      <input type="radio" name="gender" value="M" checked> 男
      <input type="radio" name="gender" value="F"> 女
    </td></tr>
    <tr><td>興趣:</td><td>
      <input type="checkbox" name="hobby" value="sport"> 運動
      <input type="checkbox" name="hobby" value="art"> 美術
      <input type="checkbox" name="hobby" value="music"> 音樂
    </td></tr>
    <tr><td>專業:</td><td>
      <select name="major">
        <option value="cs">資訊工程</option>
        <option value="lit">文學</option>
        <option value="math">數學</option>
      </select>
    </td></tr>
    <tr><td>備註:</td><td><textarea name="remark" rows="4" cols="30"></textarea></td></tr>
    <tr><td colspan="2"><input type="submit" value="送出"></td></tr>
  </table>
</form>
</body></html>

process.jsp - 接收與顯示

<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" %>
<html><head><title>處理結果</title></head><body>
<%
  request.setCharacterEncoding("UTF-8"); // POST 必須設定
  
  String username = request.getParameter("username");
  String password = request.getParameter("password");
  String gender = request.getParameter("gender");
  String[] hobbies = request.getParameterValues("hobby"); // 核取方塊陣列
  String major = request.getParameter("major");
  String remark = request.getParameter("remark");
%>
<h3>提交資料:</h3>
<ul>
  <li>用戶名:<%= username %></li>
  <li>密碼:<%= password %></li>
  <li>性別:<%= "M".equals(gender) ? "男" : "女" %></li>
  <li>興趣:<%= (hobbies != null) ? String.join(", ", hobbies) : "無" %></li>
  <li>專業:<%= major %></li>
  <li>備註:<%= remark %></li>
</ul>
<a href="form.jsp">返回</a>
</body></html>

關鍵最佳實踐

  1. POST 請求必須 request.setCharacterEncoding("UTF-8") 在讀取參數前呼叫。
  2. 核取方塊/多選下拉選單 使用 getParameterValues 取得陣列。
  3. 避免在 JSP 寫入複雜業務邏輯,建議轉發至 Servlet/Controller 處理後再 forward 回 JSP 顯示。
  4. 使用 EL + JSTL 替代 Scriptlet:${param.username}、<c:forEach items="${paramValues.hobby}" var="h">${h}</c:forEach>。
  5. XSS 防護:輸出用戶輸入時使用 fn:escapeXml() 或 <c:out value="${username}" />。
copyright © 萬盛學電腦網 all rights reserved