.jsp中的if else与<%@include file = .html>交互

huangapple go评论105阅读模式
英文:

.jsp if else with <%@include file = .html> interaction

问题

  1. - 更新后尝试的代码_02

<%@page contentType="text/html;charset=UTF-8" %>
<%@include file="/include/gds_include.jsp" %>

<jsp:useBean class="com.gds.DBconnect" id="dbHelper" scope="page" />

<%@include file="01_product_categories_detail.html"%>

<%

String id = request.getParameter("pid");

try {

  1. dbHelper.setConnection(77);
  2. dbHelper.setStatementIndex(0);
  3. dbHelper.setPreparedStatement("select * from webpage where p_id = '"+id+"'");
  4. dbHelper.exePreparedStatement();
  5. resultSet = dbHelper.getSqlResultArray();
  6. if(resultSet != null) {
  7. for(int i=0; i<resultSet.length; i++) {
  8. out.print("<tr><td>" );
  9. out.print("<h1>" + resultSet[i][2] + "</h1>");
  10. out.print(resultSet[i][6]);
  11. }
  12. }

}
catch(Exception ex) {

  1. out.print("錯誤訊息:" + ex);

}
finally {

  1. dbHelper.doFinalize();

}

%>

<%
String id = request.getParameter("pid");

  1. if(!id.equals("en_1-01")) {

%>
<%@include file="02_product_categories_detail.html"%>
<%
} else if (id.equals("en_1-02")) {
%>
<%@include file="microchip_product_categories_detail.html"%>
<%
}
%>


```

  • 错误消息
  1. 消息 无法编译 JSP 类:
  2. 描述 服务器遇到意外情况,导致无法满足请求。
  3. 异常
  4. org.apache.jasper.JasperException: 无法编译 JSP 类:
  5. jsp 文件中的第 [58] 行出现错误: [/english/product_0207_sec.jsp]
  6. 重复的局部变量 id
  7. 55:
  8. 56:
  9. 57: <%
  10. 58: String id = request.getParameter("pid");
  11. 59:
  12. 60: if(!id.equals("en_1-01")) {
  13. 61: %>
  14. 堆栈跟踪:
  15. org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:102)
  16. org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:213)
  17. org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:509)
  18. org.apache.jasper.compiler.Compiler.compile(Compiler.java:397)
  19. org.apache.jasper.compiler.Compiler.compile(Compiler.java:367)
  20. org.apache.jasper.compiler.Compiler.compile(Compiler.java:351)
  21. org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:603)
  22. org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:399)
  23. org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:380)
  24. org.apache.jasper.servlet.JspServlet.service(JspServlet.java:328)
  25. jakarta.servlet.http.HttpServlet.service(HttpServlet.java:777)
  26. org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
  27. 完整的根本原因堆栈跟踪可在服务器日志中获得。

try { } 部分之前运行良好,因此错误出现在 if else 语句中(我猜测)。

  1. <details>
  2. <summary>英文:</summary>
  3. demo url:
  4. `http://192.168.0.222/english/product_0207_sec.jsp?pid=en_1-02`
  5. `http://192.168.0.222/english/product_0207_sec.jsp?pid=en_999`
  6. inside .jsp file, the `&lt;try&gt;` part can extract different data in the same html page,
  7. **how can I make the if else work with `&lt;%@include file = .html&gt;`**
  8. -----
  9. - tried .jsp if else present with different include file (in line 55 to 62)

<%@page contentType="text/html;charset=UTF-8" %>
<%@include file="/include/gds_include.jsp" %>

<jsp:useBean class="com.gds.DBconnect" id="dbHelper" scope="page" />

<%@include file="01_product_categories_detail.html"%>

<%

String id = request.getParameter("pid");

try {

  1. dbHelper.setConnection(77);
  2. dbHelper.setStatementIndex(0);
  3. dbHelper.setPreparedStatement(&quot;select * from webpage where p_id = &#39;&quot;+id+&quot;&#39;&quot;);
  4. dbHelper.exePreparedStatement();
  5. resultSet = dbHelper.getSqlResultArray();
  6. if(resultSet != null) {
  7. for(int i=0; i&lt;resultSet.length; i++) {
  8. out.print(&quot;&lt;tr&gt;&lt;td&gt;&quot; );
  9. out.print(&quot;&lt;h1&gt;&quot; + resultSet[i][2] + &quot;&lt;/h1&gt;&quot;);
  10. out.print( resultSet[i][6]);
  11. }
  12. }

}
catch(Exception ex) {

  1. out.print(&quot;error message:&quot; + ex);

}
finally {

  1. dbHelper.doFinalize();

}

%>

</html>

<%@include file="02_product_categories_detail.html"%>

<%
String id = request.getParameter("pid");

  1. if(pid != &quot;en_1-01&quot;)
  2. out.println(&quot; &lt;%@include file=&quot;footer_A.html&quot;%&gt; &quot;);
  3. else if (pid == &quot;en_1-02&quot;)
  4. out.println(&quot; &lt;%@include file=&quot;footer_B.html&quot;%&gt; &quot;);

%>

  1. so that this .jsp can deal with much more scenario, please give me a hand
  2. ---
  3. - updated tried code_02

<%@page contentType="text/html;charset=UTF-8" %>
<%@include file="/include/gds_include.jsp" %>

<jsp:useBean class="com.gds.DBconnect" id="dbHelper" scope="page" />

<%@include file="01_product_categories_detail.html"%>

<%

String id = request.getParameter("pid");

try {

  1. dbHelper.setConnection(77);
  2. dbHelper.setStatementIndex(0);
  3. dbHelper.setPreparedStatement(&quot;select * from webpage where p_id = &#39;&quot;+id+&quot;&#39;&quot;);
  4. dbHelper.exePreparedStatement();
  5. resultSet = dbHelper.getSqlResultArray();
  6. if(resultSet != null) {
  7. for(int i=0; i&lt;resultSet.length; i++) {
  8. out.print(&quot;&lt;tr&gt;&lt;td&gt;&quot; );
  9. out.print(&quot;&lt;h1&gt;&quot; + resultSet[i][2] + &quot;&lt;/h1&gt;&quot;);
  10. out.print( resultSet[i][6]);
  11. }
  12. }

}
catch(Exception ex) {

  1. out.print(&quot;錯誤訊息:&quot; + ex);

}
finally {

  1. dbHelper.doFinalize();

}

%>

<%
String id = request.getParameter("pid");

  1. if(!id.equals(&quot;en_1-01&quot;)) {

%>
<%@include file="02_product_categories_detail.html"%>
<%
} else if (id.equals("en_1-02")) {
%>
<%@include file="microchip_product_categories_detail.html"%>
<%
}
%>

</html>

  1. - error message

Message Unable to compile class for JSP:

Description The server encountered an unexpected condition that prevented it from fulfilling the request.

Exception

org.apache.jasper.JasperException: Unable to compile class for JSP:

An error occurred at line: [58] in the jsp file: [/english/product_0207_sec.jsp]
Duplicate local variable id
55:
56:
57: <%
58: String id = request.getParameter("pid");
59:
60: if(!id.equals("en_1-01")) {
61: %>

Stacktrace:
org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:102)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:213)
org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:509)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:397)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:367)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:351)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:603)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:399)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:380)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:328)
jakarta.servlet.http.HttpServlet.service(HttpServlet.java:777)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
Note The full stack trace of the root cause is available in the server logs.

  1. the `try { }` part works fine before, so the error occur on the if else statement (I guess)
  2. </details>
  3. # 答案1
  4. **得分**: 1

尝试这个:

更新:

(1)移除 String id = request.getParameter("pid");

(2)字符串比较必须使用 equals,而不是 ==

  1. &lt;%
  2. if(!id.equals("en_1-01")) {
  3. %&gt;
  4. &lt;%@include file="footer_A.html"%&gt;
  5. &lt;%
  6. } else if (id.equals("en_1-02")) {
  7. %&gt;
  8. &lt;%@include file="footer_B.html"%&gt;
  9. &lt;%
  10. }
  11. %&gt;
  1. <details>
  2. <summary>英文:</summary>
  3. Try this:
  4. UPDATE:
  5. (1) REMOVE `String id = request.getParameter(&quot;pid&quot;);`
  6. (2) String comparisons must use `equals`, not `==`.

<%
if(!id.equals("en_1-01")) {
%>
<%@include file="footer_A.html"%>
<%
} else if (id.equals("en_1-02")) {
%>
<%@include file="footer_B.html"%>
<%
}
%>

  1. </details>

huangapple
  • 本文由 发表于 2023年2月8日 14:45:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75382212.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定