如何调用部分jsp?

时间:2014-12-07 16:30:59

标签: java jsp servlets

以下是我的JSP页面:

       <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
           pageEncoding="ISO-8859-1"%>
       <%@page import="connection.*" %> 
       <%@page import="java.sql.*" %> 
       <%@page import="javax.servlet.*" %>  
        <!DOCTYPE html>
        <html>
        <head>
        <meta charset="ISO-8859-1">
        <title>VIEW REPORTS</title>
        </head>
        <body>
        <div align="center"><b>View Reports</b></div>
        <form action="GetRole" method="get">
        <p>Enter user id:
        <input type="text" name="userid"><br>
        <input type="submit" value="submit"><br>
        </form>

        </body>
        </html>
        <%Object obj=request.getAttribute("substr");%>  
        <%String[] substr=(String[])obj;
        System.out.println("In the second time execution of JSP after getting data from the servlet");
        for(int i=0;i<substr.length;i++)
        {
        System.out.println(""+substr[i]);
        }%>

此JSP正在与另一个servlet getRole进行交互。现在我的目标是使用来自getRole的数据在同一个jsp中使用。如果你观察到,</html>标记之后的jsp的结尾部分是处理来自servlet的数据。现在的问题是,当调用JSP时,它正在执行</html>的后期部分,并自然地抛出nullpointerexception

那么请根据我的要求建议如何执行部分jsp?

PS:我知道不建议在JSP中使用scriptlet,但这又是一个临时项目;所以我采取了一些自由。

更新我的servlet如下:

    package all_packages;
    import connection.*;
    import java.io.IOException;
    import java.sql.*;

    import javax.servlet.RequestDispatcher;
    import javax.servlet.ServletException;
    //import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    public class GetRole extends HttpServlet {
        private static final long serialVersionUID = 1L;
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            Connection con=getConnection.getConnectionBuilder();
            System.out.println(""+con.toString());
            String userid=request.getParameter("userid");
            String roleid="";
            String files="";
            System.out.println(" "+userid);

            PreparedStatement pstmt=con.prepareStatement("select role_id from users where user_id=?");
            pstmt.setString(1,userid);

            ResultSet rs=pstmt.executeQuery();
            if(rs!=null)
            {   
            while(rs.next())
            {
                System.out.println(" "+rs.getString(1));
                roleid=rs.getString(1);
            }
            pstmt.close();
            }
            PreparedStatement pstmt1=con.prepareStatement("select files from access_files where role_id=?");
            pstmt1.setString(1,roleid);

            ResultSet rs1=pstmt1.executeQuery();
            System.out.println(""+rs1.toString());
            if(rs1!=null)
            {
                System.out.println("check");
            while(rs1.next())
            {
                System.out.println(" "+rs1.getString(1));
                files=rs1.getString(1);

            }
            pstmt1.close();
            }
            String substr[]=files.split(",");
            for (int j=0;j<substr.length;j++)
            {
                System.out.println(""+substr[j]);
            }
            request.setAttribute("substr", substr);
            RequestDispatcher rd=request.getRequestDispatcher("Reports.jsp");
            rd.include(request, response);
        } catch (SQLException e) {
            System.out.println("Connection unsuccessful");
            e.printStackTrace();
        }

    }
    }

堆栈跟踪

      In the second time execution of JSP after getting data from the servlet
      7 Dec, 2014 10:11:14 PM org.apache.catalina.core.StandardWrapperValve invoke
      SEVERE: Servlet.service() for servlet [jsp] in context with path [/Final_Bank] threw exception [An exception occurred processing JSP page /Reports.jsp at line 24

      21:   <%Object obj=request.getAttribute("substr");%>  
      22:   <%String[] substr=(String[])obj;
      23:   System.out.println("In the second time execution of JSP after getting data from the servlet");
      24:   for(int i=0;i<substr.length;i++)
      25:   {
      26:   System.out.println(""+substr[i]);
      27:   }%>


      Stacktrace:] with root cause
      java.lang.NullPointerException
        at org.apache.jsp.Reports_jsp._jspService(Reports_jsp.java:90)
        at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
        at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
        at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
        at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
        at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
        at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
        at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
        at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
        at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:314)
        at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
        at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
        at java.lang.Thread.run(Unknown Source)

0 个答案:

没有答案