демо-адрес: http://192.168.0.222/english/product_0207_sec.jsp?pid=en_1-02http://192.168.0.222/english/product_0207_sec.jsp?pid=en_999
внутри файла .jsp часть <try> может извлекать разные данные на одной и той же html-странице,
как я могу заставить if else работать с <%@include file = .html>
<%@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"%> ");
%>
так что этот .jsp может иметь дело с гораздо большим количеством сценариев, пожалуйста, дайте мне руку
<%@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>
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.
часть try { } раньше работала нормально, поэтому ошибка возникает в операторе if else (я думаю)
Мне пытались поставить <%@include file = "02_product_categories_detail.html"%> до того, как </html> работало, я думал, что это нормально. Я исправлю это, не беспокойтесь, что я просто изучаю jsp в течение нескольких дней, потому что я хочу, чтобы он работал, а также поэтому мой вопрос кажется немного простым.
Моя вина, я не заметил остальной код... Ответ был обновлен




Попробуй это:
ОБНОВЛЯТЬ:
(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"%>
<%
}
%>
Я только что попробовал ваше предложение, все еще возникает ошибка, связанная с обновлением в почтовом вопросе, пожалуйста, посмотрите
Моя вина, я не заметил остальной код... Ответ был обновлен.
я просто публикую обновленный пробный код_02, причина показала Duplicate local variable id, поэтому я не поставил String id = request.getParameter("pid"); в первую попытку
удалить строку 58, удалить String id = request.getParameter("pid");
</html>— закрывающий тег, конец одной html-страницы. Почему вы ставите контент, который хотите добавить, после</html>?