我尝试将值从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);
}
}
我的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
}
我的另一个班级是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;
}
}
我的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>
在JSP中我甚至尝试过其他方式:
<jsp:useBean id="hello" class="asset.management.arms.buildingprofilemodule.buildingservice">
然后在表的各个块中:
<jsp:getProperty name="hello" name="building_name">
但没有任何作用,它会抛出错误,说
org.apache.jasper.JasperException: Exception in JSP: /building_profile_details.jsp:82
82: <td><div align="center"><%=hello.getBuilding_number()%></div></td>
和其他行类似。
这是如何引起的,我该如何解决?
答案 0 :(得分:6)
在您的servlet中,替换
dispatcher.include(request, response);
通过
dispatcher.forward(request, response);
在JSP中,删除
<% buildingservice hello = (buildingservice) request.getAttribute("abcd"); %>
并替换
<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>
通过
<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>
您的代码中存在许多其他严重问题,但它们与当前的具体问题无关。然而,我会尝试总结最重要的一些:
static
个变量。这是一个主要的安全问题!buildingservice
和BuildingDAO
)使用非常奇怪的方法/模式/流程。它看起来像是由一个不懂面向对象编程概念的程序程序员编写的。也可以这样做。