首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> JSP 程式設計 >> 關於JSP

JSP基礎語法速查與簡單表單處理實戰

     編輯:關於JSP

JSP核心語法三大支柱:指令元素、標準動作、隱含對象。本文壓縮為速查手冊,附簡單表單提交處理完整示例,適合初學者快速上手與老手查漏補缺。

1. JSP指令元素(<%@ ... %>)

指令屬性說明
pagelanguage="java"唯一值java
contentType="text/html;charset=UTF-8"響應MIME類型與編碼
import="java.util.*,java.sql.*"導入Java包,多包用逗號分隔
session="false"禁用Session創建
errorPage="/error.jsp"指定錯誤處理頁
isErrorPage="true"聲明當前頁為錯誤頁,可用exception對象
includefile="header.jsp"靜態包含(編譯期合併文本)
taglibprefix="c" uri="https://java.sun.com/jsp/jstl/core"引入標簽庫

2. JSP標準動作元素

動作用法說明
jsp:forward<jsp:forward page="target.jsp"/>請求轉發,地址欄不變
jsp:include<jsp:include page="fragment.jsp"><jsp:param name="k" value="v"/></jsp:include>動態包含(運行期合併響應),可傳參
jsp:useBean<jsp:useBean id="bean" class="com.Foo" scope="page/request/session/application"/>實例化/查找JavaBean
jsp:setProperty<jsp:setProperty name="bean" property="*"/>自動映射請求參數到Bean屬性
jsp:getProperty<jsp:getProperty name="bean" property="name"/>輸出Bean屬性值
jsp:param<jsp:param name="k" value="v"/>為include/forward/plugin傳參
jsp:plugin<jsp:plugin type="applet" code="MyApplet.class" width="300" height="200">...</jsp:plugin>生成<object/embed>標簽部署Applet (已廢棄)

3. 九大隱含對象高頻API

對象類型核心方法
requestHttpServletRequestgetParameter() getParameterValues() getHeader() getCookies() getAttribute() setAttribute() getRemoteAddr() getRequestURI() setCharacterEncoding()
responseHttpServletResponsesendRedirect() setContentType() addCookie() setStatus() encodeRedirectURL()
outJspWriterprint() println() clear() flush() getBufferSize()
sessionHttpSessionsetAttribute() getAttribute() removeAttribute() invalidate() setMaxInactiveInterval() getId()
applicationServletContextsetAttribute() getAttribute() removeAttribute() getInitParameter() getRealPath() log()
pageContextPageContextsetAttribute(name,val,scope) findAttribute(name) include() forward() getRequest()/getSession()/getApplication()
configServletConfiggetInitParameter() getServletName() getServletContext()
pageObject當前Servlet實例(this)
exceptionThrowablegetMessage() printStackTrace() (需isErrorPage="true")

4. 表單處理完整示例

form.html

<form action="process.jsp" method="post">
    用戶名:<input type="text" name="username"><br>
    密碼:<input type="password" name="password"><br>
    性別:<input type="radio" name="gender" value="M">男 <input type="radio" name="gender" value="F">女<br>
    愛好:<input type="checkbox" name="hobby" value="reading">閱讀 <input type="checkbox" name="hobby" value="coding">編程<br>
    城市:<select name="city"><option value="bj">北京</option><option value="sh">上海</option></select><br>
    <input type="submit" value="提交">
</form>

process.jsp

<%@ page contentType="text/html;charset=UTF-8" %>
<% request.setCharacterEncoding("UTF-8"); %>
<html><body>
<h3>提交數據:</h3>
用戶名:<%= request.getParameter("username") %><br>
密碼:<%= request.getParameter("password") %><br>
性別:<%= request.getParameter("gender") %><br>
愛好:<%
    String[] hobbies = request.getParameterValues("hobby");
    if (hobbies != null) {
        for (String h : hobbies) out.print(h + " ");
    }
%><br>
城市:<%= request.getParameter("city") %><br>
客戶端IP:<%= request.getRemoteAddr() %>
</body></html>

5. 避坑清單

  • request.getParameter()返回null(參數不存在)或空字符串(參數存在無值),需!= null && !isEmpty()雙重判斷。
  • request.getParameterValues()返回數組,複選框未勾選時返回null而非空數組。
  • JSP默認ISO-8859-1解碼POST參數,必須request.setCharacterEncoding("UTF-8")在首次getParameter()前調用(Filter統一攔截最佳)。
  • <jsp:forward>轉發後原請求參數保留,response.sendRedirect()重定向會丟失參數(需手動攜帶)。
  • session.invalidate()銷毀後再調用getSession()會創建新Session,舊屬性不可用。

掌握指令、動作、隱含對象三大支柱,加上表單參數獲取與編碼處理,即具備JSP基礎開發能力。建議:新項目直接用Spring Boot + Thymeleaf,JSP作為維護老系統技能保留。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved