JSP 面試問題與答案

JSP 面試問題對於準備 Java 面試非常重要。JSP 是任何 Java EE Web 應用程式的重要組成部分,最近我寫了很多關於 JSP 不同功能的文章。我們從 JSP 的基礎開始,介紹了 JSP 腳本,並以 JSP 自定標籤結束,以避免在 JSP 頁面中使用 Java 代碼。一些您可能感興趣的教程包括:

  1. 初學者的 JSP 教程
  2. JSP 隱含對象
  3. JSP 指令
  4. JSP 异常處理
  5. JSP 操作標籤
  6. JSP EL
  7. JSTL 核心標籤
  8. JSP 自定標籤

JSP 面試問題

這裡我提供了一些重要的JSP面試問題及答案,以幫助你應對面試。這些都是對有經驗的程序員和初學者都很好的JSP面試問題和答案。在你閱讀答案之前,我建議你先閱讀上面的教程,並試著自己回答問題,測試一下對JSP的理解。

  1. 什麼是JSP,我們為什麼需要它?
  2. JSP的生命周期階段是什麼?
  3. JSP的生命周期方法有哪些?
  4. 哪些JSP生命周期方法可以被重寫?
  5. 如何避免客戶端瀏覽器直接訪問JSP頁面?
  6. JSP中有哪些不同類型的註釋?
  7. 在JSP中,Scriptlet、Expression和Declaration分別是什麼?
  8. JSP隱含對象有哪些?
  9. 我們可以在JSP聲明中定義的方法中使用JSP隱含對象嗎?
  10. 哪個隱含對象在普通JSP頁面中不可用?
  11. PageContext隱含對象的好處是什麼?
  12. 我們如何配置JSP的初始參數?
  13. 為什麼不建議在JSP中使用腳本元素?
  14. 我們可以在JSP頁面中定義類嗎?
  15. 如何在JSP頁面中禁用Java代碼或腳本?
  16. 解釋JSP動作元素或動作標記?
  17. 包含指令和jsp:include動作之間的區別是什麼?
  18. 什麼是JSP表達式語言,它有什麼好處?
  19. 什麼是JSP EL隱式對象,它與JSP隱式對象有什麼不同?
  20. 如何使用JSP EL獲取HTTP方法名稱?
  21. 什麼是JSP標準標籤庫,提供一些示例用法?
  22. JSTL標籤的類型有哪些?
  23. 什麼是JSP自定義標籤,它的組成部分是什麼?
  24. 舉個需要使用JSP自定義標籤的例子?
  25. 為什麼我們不需要在web.xml中配置JSP標準標籤?
  26. 如何處理JSP服務方法拋出的異常?
  27. 我們如何使用JSTL捕獲異常並處理它?
  28. 我們如何在JSP中打印“<br>在HTML中創建一個新行”?
  29. jsp-config在部署描述符中是什麼?
  30. 如何忽略JSP中的EL表達式評估?
  31. 容器何時初始化多個JSP/Servlet對象?
  32. 我們可以在JSP頁面中使用JavaScript嗎?
  33. 如何防止在JSP中隱式創建會話?
  34. JspWriter和Servlet PrintWriter之間的區別是什麼?
  35. 如何擴展JSP技術?
  36. 提供一些JSP最佳實踐?

JSP面試問題與答案

  1. 什麼是 JSP,我們為什麼需要它?

    JSP 意為 JavaServer Pages。JSP 是 Java 服務器端技術,用於創建動態網頁。JSP 是 Servlet 技術的擴展,幫助開發人員使用類似 HTML 的語法創建動態頁面。我們也可以在 Servlet 中創建用戶視圖,但代碼會變得非常醜陋且容易出錯。此外,網頁上的大多數元素都是靜態的,因此 JSP 頁面更適合用於網頁。我們應該避免在 JSP 頁面中包含業務邏輯,並且只用於視圖目的。JSP 腳本元素可用於在 JSP 頁面中編寫 Java 代碼,但最好避免使用它們,而是使用 JSP 動作元素、JSTL 標籤或自定義標籤來實現相同的功能。JSP 的另一個好處是大多數容器支援 JSP 頁面的熱部署。只需在 JSP 頁面中進行所需的更改,將舊頁面替換為更新後的 jsp 頁面,放入部署目錄中,容器將加載新的 JSP 頁面。我們不需要編譯項目代碼或重新啟動服務器,而如果我們更改 Servlet 代碼,則需要重新構建整個項目並部署它。儘管大多數容器現在都提供應用程序的熱部署支援,但仍需要比 JSP 頁面更多的工作。

  2. JSP生命周期的阶段是什么?

    当您查看JSP页面代码时,它看起来像HTML,与Java类完全不同。实际上,JSP容器负责翻译JSP页面并创建用于Web应用程序的Servlet类。JSP生命周期的阶段包括:

    1. 翻译 – JSP容器检查JSP页面代码并解析它以生成Servlet源代码。例如,在Tomcat中,您会在TOMCAT/work/Catalina/localhost/WEBAPP/org/apache/jsp目录找到生成的Servlet类文件。如果JSP页面名称为home.jsp,通常生成的Servlet类名称为home_jsp,文件名为home_jsp.java。
    2. 编译 – JSP容器在此阶段编译jsp类源代码并生成类文件。
    3. 类加载 – 容器在此阶段将类加载到内存中。
    4. 实例化 – 容器调用生成类的无参数构造函数将其加载到内存并实例化。
    5. 初始化 – 容器调用JSP类对象的init方法,并使用在部署描述符中配置的init参数初始化servlet配置。此阶段之后,JSP已准备好处理客户端请求。通常从JSP的翻译到初始化发生在首次请求JSP时,但我们可以配置它像Servlet一样在部署时加载和初始化,使用load-on-startup元素。
    6. 请求处理 – 这是JSP页面的最长生命周期阶段,JSP页面处理客户端请求。处理是多线程的,类似于Servlets,对于每个请求,会生成一个新线程,并创建ServletRequest和ServletResponse对象,然后调用JSP的service方法。
    7. 销毁 – 这是JSP生命周期的最后阶段,在此阶段卸载JSP类从内存中。通常在应用程序被卸载或服务器关闭时发生。
  3. 什麼是 JSP 生命周期方法?

    JSP 生命周期方法包括:

    1. jspInit(): 此方法在JspPage中聲明,由 JSP 容器實現。此方法在 JSP 生命周期中僅調用一次,用於使用部署描述符中配置的配置參數對其進行初始化。我們可以使用 JSP 声明腳本元素覆蓋此方法,以初始化在 JSP 頁面中要使用的任何資源。
    2. _jspService(): 這是 JSP 方法,由 JSP 容器為每個客戶端請求調用,通過傳遞請求和響應對象。請注意,方法名稱以下劃線開頭,以區分它與其他生命周期方法,因為我們無法覆蓋此方法。所有 JSP 代碼都放在此方法中,並且默認情況下進行覆蓋。我們不應試圖使用 JSP 声明腳本元素覆蓋它。此方法在 HttpJspPage 接口中定義。
    3. jspDestroy(): 當 JSP 從記憶體中卸載,如關閉應用程序或容器時,容器會調用此方法。此方法在 JSP 生命周期中僅調用一次,我們應該覆蓋此方法以釋放在 JSP 初始化方法中創建的任何資源。
  4. 哪些 JSP 生命周期方法可以被覆盖?

    我们可以使用 JSP 声明脚本元素覆盖 jspInit() 和 jspDestroy() 方法。我们应该覆盖 jspInit() 方法来创建在 JSP 服务方法中使用的共享资源,并覆盖 jspDestroy() 方法来释放这些共享资源。

  5. 如何避免客户端浏览器直接访问 JSP 页面?

We know that anything inside the WEB-INF directory can't be accessed directly in a web application, so we can place our JSP pages in WEB-INF directory to avoid direct access to JSP page from the client browser. But in this case, we will have to configure it in deployment descriptor just like Servlets. The sample configuration is given below code snippet of web.xml file.

```
<servlet>
  <servlet-name>Test</servlet-name>
  <jsp-file>/WEB-INF/test.jsp</jsp-file>
  <init-param>
    <param-name>test</param-name>
    <param-value>Test Value</param-value>
  </init-param>
</servlet>
   
<servlet-mapping>
  <servlet-name>Test</servlet-name>
  <url-pattern>/Test.do</url-pattern>
</servlet-mapping>
```
  1. 在 JSP 中有哪些不同類型的註釋?

JSP pages provide two types of comments that we can use:
-   **HTML Comments**: Since JSP pages are like HTML, we can use HTML comments like `<!-- HTML Comment -->`. These comments are sent to client also and we can see it in HTML source. So we should avoid any code level comments or debugging comments using HTML comments.
-   **JSP Comments**: JSP Comments are written using scriptlets like `<%-- JSP Comment --%>`. These comments are present in the generated servlet source code and doesn't sent to client. For any code level or debugging information comments we should use JSP comments.
  1. 在 JSP 中,Scriptlet、Expression 和 Declaration 是什麼?

Scriptlets, Expression and Declaration are scripting elements in JSP page using which we can add java code in the JSP pages. A scriptlet tag starts with `<%` and ends with `%>`. Any code written inside the scriptlet tags go into the `_jspService()` method. For example;

```
<%
Date d = new Date();
System.out.println("Current Date="+d);
%>
```

Since most of the times we print dynamic data in JSP page using _out.print()_ method, there is a shortcut to do this through JSP Expressions. JSP Expression starts with `<%=` and ends with `%>`. `<% out.print("Pankaj"); %>` can be written using JSP Expression as `<%= "Pankaj" %>` Notice that anything between `<%= %>` is sent as parameter to `out.print()` method. Also notice that scriptlets can contain multiple java statements and always ends with semicolon (;) but expression doesn't end with semicolon. JSP Declarations are used to declare member methods and variables of servlet class. JSP Declarations starts with `<%!` and ends with `%>`. For example we can create an int variable in JSP at class level as `<%! public static int count=0; %>`.
  1. JSP隱含物件有哪些?

**JSP implicit objects** are created by container while translating JSP page to Servlet source to help developers. We can use these objects directly in **scriptlets** that goes in service method, however we can't use them in JSP Declaration because that code will go at class level. We have 9 implicit objects that we can directly use in JSP page. Seven of them are declared as local variable at the start of _\_jspService()_ method whereas two of them are part of _\_jspService()_ method argument that we can use.
1.  out Object
2.  request Object
3.  response Object
4.  config Object
5.  application Object
6.  session Object
7.  pageContext Object
8.  page Object
9.  exception Object
10.  JSP Implicit Objects Example

詳細閱讀每個隱含物件的資料,請參考JSP隱含物件。19.###我們可以在JSP Declaration中定義的方法中使用JSP隱含物件嗎?

No we can't because JSP implicit objects are local to service method and added by JSP Container while translating JSP page to servlet source code. JSP Declarations code goes outside the service method and used to create class level variables and methods and hence can't use JSP implicit objects.
  1. 哪個隱含物件在普通的JSP頁面中不可用?

JSP exception implicit object is not available in normal JSP pages and it's used in JSP error pages only to catch the exception thrown by the JSP pages and provide useful message to the client.
  1. PageContext隱含對象有哪些好處?

JSP pageContext implicit object is instance of javax.servlet.jsp.PageContext abstract class implementation. We can use pageContext to get and set attributes with different scopes and to forward request to other resources. pageContext object also hold reference to other implicit object. This is the only object that is common in both JSP implicit objects and in JSP EL implicit objects.
  1. 我們如何為JSP配置初始化參數?

We can configure init params for JSP similar to servlet in web.xml file, we need to configure JSP init params with servlet and servlet-mapping element. The only thing differs from servlet is jsp-file element where we need to provide the JSP page location.
  1. 為什麼在 JSP 中使用腳本元素是不鼓勵的?

JSP pages are mostly used for view purposes and all the business logic should be in the servlet or model classes. We should pass parameters to JSP page through attributes and then use them to create the HTML response in JSP page. Most part of the JSP page contains HTML code and to help web designers to easily understand JSP page and develop them, JSP technology provides action elements, JSP EL, JSP Standard Tag Library and custom tags that we should use rather than scripting elements to bridge the gap between JSP HTML part and JSP java part.
  1. 我們可以在 JSP 頁面中定義一個類嗎?

It's not a good practice though, but we can define a class inside a JSP Page. Below is the sample code for this:

```
<%!
private static class NestedClass { //static is better because Servlet is multi-threaded
  private final int num = 0;
  public int getNum() {
    return num;
  }
}
%>
```

Or

```
<%      
    class Person { 
        //這將放在方法體內,所以不能是public
    }
%>
```
  1. 我們如何在JSP頁面中禁用Java代碼或腳本?

We can disable scripting elements in JSP pages through deployment descriptor configuration like below.

```
<jsp-config>
	<jsp-property-group>
		<url-pattern>*.jsp</url-pattern>
		<scripting-invalid>true</scripting-invalid>
	</jsp-property-group>
</jsp-config>
```

Above URL-pattern will disable scripting for all the JSP pages but if you want to disable it only for a specific page, you can give the JSP file name itself.
  1. 解釋JSP動作元素或動作標籤?

JSP action elements or action tags are HTML like tags that provide useful functionalities such as working with Java Bean, including a resource, forwarding the request and to generate dynamic XML elements. JSP action elements always starts with jsp: and we can use them in JSP page directly without the need to import any tag libraries or any other configuration changes. Some of the important action elements are jsp:useBean, jsp:getProperty, jsp:setProperty, jsp:include and jsp:forward. Read more in details about these at [**JSP Action Elements**](/community/tutorials/jsp-action-tags-jsp-usebean-include-forward).
  1. include指令和jsp:include動作之間有什麼區別?

The difference between JSP include directive and include action is that in include directive the content to other resource is added to the generated servlet code at the time of translation whereas with include action it happens at runtime. Another difference is that in JSP include action, we can pass params to be used in the included resource with `jsp:param` action element but in JSP include directive we can't pass any params. When the included resource is static such as header, footer, image files then we should use include directive for faster performance but if the included resource is dynamic and requires some parameters for processing then we should use include action tag.
  1. JSP表達式語言是什麼,它有什麼好處?

Most of the times we use JSP for view purposes and all the business logic is present in servlet code or model classes. When we receive client request in the servlet, we process it and then add attributes in request/session/context scope to be retrieved in JSP code. We also use request params, headers, cookies and init params in JSP to create response views. We can use scriptlets and JSP expressions to retrieve attributes and parameters in JSP with java code and use it for view purpose. But for web designers, java code is hard to understand and that’s why JSP Specs 2.0 introduced Expression Language (EL) through which we can get attributes and parameters easily using HTML like tags. Expression language syntax is `${name}` and we can use EL implicit objects and EL operators to retrieve the attributes from different scopes and use them in JSP page. Read more about JSP EL with example program at [**JSP EL Tutorial**](/community/tutorials/jsp-expression-language-el-example-tutorial).
  1. 什麼是 JSP EL 隱含對象,它與 JSP 隱含對象有何不同?

JSP Expression Language provides many implicit objects that we can use to get attributes from different scopes and parameter values. Note that these are different from JSP implicit objects and contains only the attributes in given scope. The only common implicit object in JSP EL and JSP page is pageContext object. Below table provides a list of implicit object in JSP EL.

| JSP EL Implicit Objects | Type | Description |
| --- | --- | --- |
| pageScope | Map | A map that contains the attributes set with page scope. |
| requestScope | Map | Used to get the attribute value with request scope. |
| sessionScope | Map | Used to get the attribute value with session scope. |
| applicationScope | Map | Used to get the attributes value from application scope. |
| param | Map | Used to get the request parameter value, returns a single value |
| paramValues | Map | Used to get the request param values in an array, useful when request parameter contain multiple values. |
| header | Map | Used to get request header information. |
| headerValues | Map | Used to get header values in an array. |
| cookie | Map | Used to get the cookie value in the JSP |
| initParam | Map | Used to get the context init params, we can't use it for servlet init params |
| pageContext | pageContext | Same as JSP implicit pageContext object, used to get the request, session references etc. example usage is getting request HTTP Method name. |
  1. 如何使用 JSP EL 獲取 HTTP 方法名稱?

We can use pageContext JSP EL implicit object to get the request object reference and use dot operator to get the HTTP method name in JSP page. The JSP EL code for this will be `${pageContext.request.method}`.
  1. JSP 標準標籤庫是什麼,提供一些使用示例?

JSP Standard Tag Library or JSTL is more versatile than JSP EL or Action elements because we can loop through a collection or escape HTML tags to show them like text in response. JSTL is part of the Java EE API and included in most servlet containers. But to use JSTL in our JSP pages, we need to download the JSTL jars for your servlet container. Most of the times, you can find them in the example projects and you can use them. You need to include these libraries in the project WEB-INF/lib directory. These jars are container specific, for example in Tomcat, we need to include jstl.jar and standard.jar jar files in the project build path. Read more about JSTL tags with example program at [**JSTL Tutorial**](/community/tutorials/jstl-tutorial-jstl-tags-example).
  1. JSTL 標籤有哪些類型?

Based on the JSTL functions, they are categorized into five types.
1.  Core Tags - Core tags provide support for iteration, conditional logic, catch exception, url, forward or redirect response etc.
2.  Formatting and Localization Tags - These tags are provided for formatting of Numbers, Dates and i18n support through locales and resource bundles.
3.  SQL Tags - JSTL SQL Tags provide support for interaction with relational databases such as Oracle, MySql etc.
4.  XML Tags - XML tags are used to work with XML documents such as parsing XML, transforming XML data and XPath expressions evaluation.
5.  JSTL Functions Tags - JSTL tags provide a number of functions that we can use to perform common operation, most of them are for String manipulation such as String Concatenation, Split String etc.
  1. 什麼是 JSP 自定標籤以及它的組成部分?

Sometimes JSP EL, Action Tags and JSTL tags are not enough and we might get tempted to write java code to perform some operations in JSP page. Fortunately JSP is extendable and we can create our own custom tags to perform certain operations. We can create JSP Custom Tags with following components:
-   JSP Custom Tag Handler
-   Creating Tag Library Descriptor (TLD) File
-   Deployment Descriptor Configuration for TLDWe can add custom tag library in JSP page using taglib directive and then use it.
  1. 給出一個您需要 JSP 自定標籤的例子?

Let’s say we want to show a number with formatting with commas and spaces. This can be very useful for user when the number is really long. So we want some custom tags like below: `<mytags:formatNumber number="123456.789" format="#,###.00"/>` Based on the number and format passed, it should write the formatted number in JSP page, for the above example it should print 123,456.79 We know that JSTL doesn’t provide any inbuilt tags to achieve this, so we will create our own custom tag implementation and use it in the JSP page. Read above example implementation at [**JSP Custom Tag**](/community/tutorials/jsp-custom-tags-example-tutorial).
  1. 為什麼我們不需要在web.xml中配置JSP標準標籤?

We don't need to configure JSP standard tags in web.xml because the TLD files are inside the META-INF directory of the JSTL jar files. When container loads the web application and finds TLD files inside the META-INF directory of the JAR file, it automatically configures them to be used directly in the application JSP pages. All we need to do it to include it in the JSP page using taglib directive.
  1. 我們如何處理由JSP服務方法拋出的異常?

To handle exceptions thrown by the JSP page, all we need is an error page and define the error page in JSP using page directive. To create a JSP error page, we need to set the page directive attribute isErrorPage value to true, then we can access exception implicit object in the JSP and use it to send a customized error message to the client. We need to define exception and error handler JSP pages in the deployment descriptor like below.

```
<error-page>
	<error-code>404</error-code>
	<location>/error.jsp</location>
</error-page>

<error-page>
	<exception-type>java.lang.Throwable</exception-type>
	<location>/error.jsp</location>
</error-page>
```

Read more with example program at [**JSP Exception Handling**](/community/tutorials/jsp-exception-handling-jsp-error-page).
  1. 我們如何使用JSTL捕捉異常並處理它?

We can use JSTL Core tags c:catch and c:if to catch exception inside the JSP service method and process it. c:catch tag catches the exception and wraps it into the exception variable and we can use c:if condition tag to process it. Below code snippet provide sample usage.

```
<c:catch var ="exception">
   <% int x = 5/0;%>
</c:catch>
 
<c:if test = "${exception ne null}">
   <p>Exception is : ${exception} <br />
   Exception Message: ${exception.message}</p>
</c:if>
```

Notice the use of JSP EL in the `c:if` condition.
  1. 我們如何在JSP中打印“<br>在HTML中創建新行”?

We can use c:out escapeXml attribute to escape the HTML elements so that it get's shown as text in the browser, for this scenario we will write code like below.

```
<c:out value="<br> creates a new line in HTML" escapeXml="true"></c:out>
```
  1. 在部署描述符中,jsp-config 是什麼?

jsp-config element is used to configure different parameters for JSP pages. Some of it's usage are:
-   Configuring tag libraries for the web application like below.
    
    ```
    <jsp-config>
    		<taglib>
    			<taglib-uri>https://journaldev.com/jsp/tlds/mytags</taglib-uri>
    			<taglib-location>/WEB-INF/numberformatter.tld</taglib-location>
    		</taglib>
    </jsp-config>
    ```
    
-   We can control scripting elements in JSP pages.
-   We can control JSP Expression Language (EL) evaluation in JSP pages.
-   We can define the page encoding for URL pattern.
-   To define the buffer size to be used in JSP page out object.
-   To denote that the group of resources that match the URL pattern are JSP documents, and thus must be interpreted as XML documents.
  1. 如何忽略 JSP 中的 EL 表達式評估?

We can ignore EL evaluation in JSP page by two ways.
1.  Using page directive as `<%@ page isELIgnored="true" %>`
2.  Configuring in web.xml - better approach when you want to disable EL evaluation for many JSP pages.
    
    ```
    <jsp-config>
    	<jsp-property-group>
    		<url-pattern>*.jsp</url-pattern>
    		<el-ignored>true</el-ignored>
    	</jsp-property-group>
    </jsp-config>
    ```
  1. 容器何時會初始化多個 JSP/Servlet 對象?

If we have multiple servlet and servlet-mapping elements in deployment descriptor for a single servlet or JSP page, then container will initialize an object for each of the element and all of these instances will have their own ServletConfig object and init params. For example, if we configure a single JSP page in web.xml like below.

```
<servlet>
  <servlet-name>Test</servlet-name>
  <jsp-file>/WEB-INF/test.jsp</jsp-file>
  <init-param>
    <param-name>test</param-name>
    <param-value>Test Value</param-value>
  </init-param>
</servlet>
   
<servlet-mapping>
  <servlet-name>Test</servlet-name>
  <url-pattern>/Test.do</url-pattern>
</servlet-mapping>
   
<servlet>
  <servlet-name>Test1</servlet-name>
  <jsp-file>/WEB-INF/test.jsp</jsp-file>
</servlet>
   
<servlet-mapping>
  <servlet-name>Test1</servlet-name>
  <url-pattern>/Test1.do</url-pattern>
</servlet-mapping>
```

那麼,如果我們可以通過 URI 模式訪問相同的 JSP 頁面,並且兩者都將擁有自己的 init 參數值。65. ### 我們可以在 JSP 頁面中使用 JavaScript 嗎?

Yes why not, I have seen some developers getting confused with this. Even though JSP is a server-side technology, it's used to generate a client-side response and we can add javascript or CSS code like any other HTML page.
  1. 我們如何防止在 JSP 中隱式創建會話?

By default JSP page creates a session but sometimes we don’t need session in JSP page. We can use JSP page directive session attribute to indicate compiler to not create session by default. It’s default value is true and session is created. To disable the session creation, we can use it like below.

```
<%@ page session="false" %>
```
  1. JspWriter 和 Servlet PrintWriter 有什麼區別?

PrintWriter is the actual object responsible for writing the content in response. JspWriter uses the PrintWriter object behind the scene and provide buffer support. When the buffer is full or flushed, JspWriter uses the PrintWriter object to write the content into response.
  1. 我們如何擴展 JSP 技術?

We can extend JSP technology with custom tags to avoid scripting elements and java code in JSP pages.
  1. 提供一些 JSP 最佳實踐?

Some of the JSP best practices are:
1.  Avoid scripting elements in JSP pages. If JSP EL, action elements and JSTL not serve your needs then create custom tags.
2.  Use comment properly, use JSP comments for code level or debugging purpose so that it's not sent to client.
3.  Avoid any business logic in JSP page, JSP pages should be used only for response generation for client.
4.  Disable session creation in JSP page where you don't need it for better performance.
5.  Use page, taglib directives at the start of JSP page for better readability.
6.  Proper use of jsp include directive or include action based on your requirements, include directive is good for static content whereas include action is good for dynamic content and including resource at runtime.
7.  Proper exception handling using JSP error pages to avoid sending container generated response incase JSP pages throw exception in service method.
8.  If you are having CSS and JavaScript code in JSP pages, it's best to place them in separate files and include them in JSP page.
9.  Most of the times JSTL is enough for our needs, if you find a scenario where it's not then check your application design and try to put the logic in a servlet that will do the processing and then set attributes to be used in JSP pages.

如果我漏掉了任何重要的 JSP 面试问题,请告诉我,我将在未来添加更多到列表中。

Source:
https://www.digitalocean.com/community/tutorials/jsp-interview-questions-and-answers