.jsp,如果为其他,则使用〈%@include file = .html>交互

qyzbxkaa  于 2023-02-10  发布在  其他
关注(0)|答案(1)|浏览(134)

演示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
在.jsp文件中,<try>部分可以在同一个html页面中提取不同的数据,

如何使if else与<%@include file = .html>一起工作

  • 尝试.jsp,如果存在其他不同的包含文件(第55 - 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"%> ");
%>

为了使这个.jsp能够处理更多场景,请给予我一点帮助

  • 更新尝试代码_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>
  • 错误讯息
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语句上(我猜)

5fjcxozz

5fjcxozz1#

试试这个:
更新:
(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"%>
<%
    }
%>

相关问题