无法从jsp中的servlet获取值

Dhr*_*ruv 1 java jsp servlets

我尝试从servlet中获取值到我的JSP中,但它会抛出一个NullPointerException或其他一些错误.

这是从JSP获取值的servlet:

buildingprofilerequest.java

package asset.management.arms.buildingprofilemodule;

import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.sun.xml.internal.ws.client.SenderException;
/**
 * Servlet implementation class buildingprofilerequest
 */
public class buildingprofilerequest extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        try{

            buildingservice building = new buildingservice();
            building.setBuilding_name(request.getParameter("combobox"));

            building = BuildingDAO.build(building);

            request.setAttribute("abcd", building);      


                 RequestDispatcher dispatcher =                  
                 getServletContext().getRequestDispatcher("/building_profile_details.jsp");

            dispatcher.include(request, response);

        }
    catch (Throwable theException)      
        {
             System.out.println(theException); 
        }

    }
Run Code Online (Sandbox Code Playgroud)

我的bean看起来像这样:

buildingservice.java

package asset.management.arms.buildingprofilemodule;

public class buildingservice {

        private String building_name;

                public String getBuilding_name() {
            return building_name;
        }

        public void setBuilding_name(String newbuilding_name) {
            this.building_name = newbuilding_name;
        }

              //and has many more parameters and there getters and setters
        }
Run Code Online (Sandbox Code Playgroud)

我的另一课是BuildingDAO.java,这里所有的计算都完成了:

package asset.management.arms.buildingprofilemodule;

import java.sql.*;
import asset.management.arms.loginmodule.ConnectionManager;

public class BuildingDAO {
    static Connection currentCon = null;
    static ResultSet rs = null;  

    public static buildingservice build(buildingservice bean) {

           String building_name = bean.getBuilding_name(); 

           String searchQuery = "select * from buildings";
         try{
            //connect to DB 
            currentCon = ConnectionManager.getConnection();
             stmt=currentCon.createStatement();
             rs = stmt.executeQuery(searchQuery);           

             while(rs.next()){

            //retreiving building parameters from database   
             String buildingname = rs.getString("building_name");
             String buildingnumber = rs.getString("building_number");
             int buildarea = rs.getInt("build_area");

                  //setting building parameters
                 bean.setBuilding_name(buildingname);
                 bean.setBuilding_number(buildingnumber);
                 bean.setBuild_area(buildarea);
                 bean.setBuilt_year(builtyear);
            catch (Exception ex) 
          {
             System.out.println(" " + ex);
          } 

            finally 
          {
             if (rs != null)    {
                try {
                   rs.close();
                } catch (Exception e) {}
                   rs = null;
                }

             if (stmt != null) {
                try {
                   stmt.close();
                } catch (Exception e) {}
                   stmt = null;
                }

             if (currentCon != null) {
                try {
                   currentCon.close();
                } catch (Exception e) {
                }

                currentCon = null;
             }
          }

        return bean;
    }

}
Run Code Online (Sandbox Code Playgroud)

我的JSP是这样的:

building_profile_details.jsp

<%@ page language="java" contentType="text/html; charset=iso-8859-1"
    pageEncoding="ISO-8859-1" import="asset.management.arms.buildingprofilemodule.buildingservice"%>

<%  buildingservice hello = (buildingservice) request.getAttribute("abcd"); %>

<table width="1150" height="176" border="1" align="center" bordercolor="lightslategray">
      <tr>
        <td width="107"><div align="center"><b>Building Number</b> </div></td>
        <td width="325"><div align="center"><b>Building Name </b></div></td>
        <td width="70"><div align="center"><b>area</b></div></td>
        <td width="146"><div align="center"><b>built year</b></div></td> 
          </tr>

<tr>
    <td><div align="center"><%=hello.getBuilding_number()%></div></td>
    <td><div align="center"><%= hello.getBuilding_name()%></div></td>
<td><div align="center"><%=hello.getBuild_area()%></div></td>
 <td><div align="center"><%= hello.getBuilt_year()%></div></td>
</tr>
</table>
Run Code Online (Sandbox Code Playgroud)

在JSP中我甚至尝试过其他方式:

<jsp:useBean id="hello" class="asset.management.arms.buildingprofilemodule.buildingservice">
Run Code Online (Sandbox Code Playgroud)

然后在表格的各个块中:

<jsp:getProperty name="hello" name="building_name">
Run Code Online (Sandbox Code Playgroud)

但没有任何作用,它会引发错误

org.apache.jasper.JasperException: Exception in JSP: /building_profile_details.jsp:82

82:     <td><div align="center"><%=hello.getBuilding_number()%></div></td>
Run Code Online (Sandbox Code Playgroud)

和其他线路类似.

这是怎么造成的,怎么解决这个问题?

Bal*_*usC 6

在您的servlet中,替换

dispatcher.include(request, response);
Run Code Online (Sandbox Code Playgroud)

通过

dispatcher.forward(request, response);
Run Code Online (Sandbox Code Playgroud)

在JSP中,删除

<%  buildingservice hello = (buildingservice) request.getAttribute("abcd"); %>
Run Code Online (Sandbox Code Playgroud)

并替换

<td><div align="center"><%=hello.getBuilding_number()%></div></td>
<td><div align="center"><%= hello.getBuilding_name()%></div></td>
<td><div align="center"><%=hello.getBuild_area()%></div></td>
<td><div align="center"><%= hello.getBuilt_year()%></div></td>
Run Code Online (Sandbox Code Playgroud)

通过

<td><div align="center">${abcd.building_number}</div></td>
<td><div align="center">${abcd.building_name}</div></td>
<td><div align="center">${abcd.build_area}</div></td>
<td><div align="center">${abcd.built_year}</div></td>
Run Code Online (Sandbox Code Playgroud)

也可以看看:


顺便提一下,代码中存在许多其他严重问题,但它们与当前的具体问题无关.然而,我会尝试总结最重要的一些:

  • 代码将DB资源保存为static变量.这是一个主要的线程安全问题!
  • 代码不以合理的方式处理异常.这不是开发人员也不是用户友好的.
  • 代码不遵守Java命名约定.这会导致开发人员混淆和可维护性问题.
  • 代码(特别是buildingserviceBuildingDAO)使用非常奇怪的方法/模式/流程.它看起来像是由不懂面向对象编程概念的程序程序员编写的.
  • JSP使用自2003年以来不鼓励使用的scriptlet.让自己保持最新状态.Java代码属于Java类,JSP应该只包含HTML,JSP标记和EL.
  • HTML使用不推荐使用的属性.让自己保持最新状态.学习CSS.

也是如此.