英文:
.jsp if else with <%@include file = .html> interaction
问题
- 更新后尝试的代码_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 {
dbHelper.setConnection(77);
dbHelper.setStatementIndex(0);
dbHelper.setPreparedStatement("select * from webpage where p_id = '"+id+"'");
dbHelper.exePreparedStatement();
resultSet = dbHelper.getSqlResultArray();
if(resultSet != null) {
for(int i=0; i<resultSet.length; i++) {
out.print("<tr><td>" );
out.print("<h1>" + resultSet[i][2] + "</h1>");
out.print(resultSet[i][6]);
}
}
}
catch(Exception ex) {
out.print("錯誤訊息:" + ex);
}
finally {
dbHelper.doFinalize();
}
%>
<%
String id = request.getParameter("pid");
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"%>
<%
}
%>
```
- 错误消息
消息 无法编译 JSP 类:
描述 服务器遇到意外情况,导致无法满足请求。
异常
org.apache.jasper.JasperException: 无法编译 JSP 类:
在 jsp 文件中的第 [58] 行出现错误: [/english/product_0207_sec.jsp]
重复的局部变量 id
55:
56:
57: <%
58: String id = request.getParameter("pid");
59:
60: if(!id.equals("en_1-01")) {
61: %>
堆栈跟踪:
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)
注 完整的根本原因堆栈跟踪可在服务器日志中获得。
try { }
部分之前运行良好,因此错误出现在 if else 语句中(我猜测)。
<details>
<summary>英文:</summary>
demo url:
`http://192.168.0.222/english/product_0207_sec.jsp?pid=en_1-02`
`http://192.168.0.222/english/product_0207_sec.jsp?pid=en_999`
inside .jsp file, the `<try>` part can extract different data in the same html page,
**how can I make the if else work with `<%@include file = .html>`**
-----
- 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 {
dbHelper.setConnection(77);
dbHelper.setStatementIndex(0);
dbHelper.setPreparedStatement("select * from webpage where p_id = '"+id+"'");
dbHelper.exePreparedStatement();
resultSet = dbHelper.getSqlResultArray();
if(resultSet != null) {
for(int i=0; i<resultSet.length; i++) {
out.print("<tr><td>" );
out.print("<h1>" + resultSet[i][2] + "</h1>");
out.print( resultSet[i][6]);
}
}
}
catch(Exception ex) {
out.print("error message:" + ex);
}
finally {
dbHelper.doFinalize();
}
%>
</html>
<%@include file="02_product_categories_detail.html"%>
<%
String id = request.getParameter("pid");
if(pid != "en_1-01")
out.println(" <%@include file="footer_A.html"%> ");
else if (pid == "en_1-02")
out.println(" <%@include file="footer_B.html"%> ");
%>
so that this .jsp can deal with much more scenario, please give me a hand
---
- 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 {
dbHelper.setConnection(77);
dbHelper.setStatementIndex(0);
dbHelper.setPreparedStatement("select * from webpage where p_id = '"+id+"'");
dbHelper.exePreparedStatement();
resultSet = dbHelper.getSqlResultArray();
if(resultSet != null) {
for(int i=0; i<resultSet.length; i++) {
out.print("<tr><td>" );
out.print("<h1>" + resultSet[i][2] + "</h1>");
out.print( resultSet[i][6]);
}
}
}
catch(Exception ex) {
out.print("錯誤訊息:" + ex);
}
finally {
dbHelper.doFinalize();
}
%>
<%
String id = request.getParameter("pid");
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"%>
<%
}
%>
</html>
- 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.
the `try { }` part works fine before, so the error occur on the if else statement (I guess)
</details>
# 答案1
**得分**: 1
尝试这个:
更新:
(1)移除 String id = request.getParameter("pid");
(2)字符串比较必须使用 equals
,而不是 ==
。
<%
if(!id.equals("en_1-01")) {
%>
<%@include file="footer_A.html"%>
<%
} else if (id.equals("en_1-02")) {
%>
<%@include file="footer_B.html"%>
<%
}
%>
<details>
<summary>英文:</summary>
Try this:
UPDATE:
(1) REMOVE `String id = request.getParameter("pid");`
(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"%>
<%
}
%>
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论