Not able to fetch values from servlet in jsp
I try to fetch values from servlet into my JSP, but it throws a NullPointerException
or some other error.
This is the servlet which gets values from JSP:
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(bui开发者_开发知识库lding);
request.setAttribute("abcd", building);
RequestDispatcher dispatcher =
getServletContext().getRequestDispatcher("/building_profile_details.jsp");
dispatcher.include(request, response);
}
catch (Throwable theException)
{
System.out.println(theException);
}
}
My bean looks in brief like this:
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
}
My other class is BuildingDAO.java
, here all the calculations are done:
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;
}
}
My JSP is this:
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>
In the JSP I even tried other ways like:
<jsp:useBean id="hello" class="asset.management.arms.buildingprofilemodule.buildingservice">
and then in individual blocks of table:
<jsp:getProperty name="hello" name="building_name">
But nothing works, it throws error which says
org.apache.jasper.JasperException: Exception in JSP: /building_profile_details.jsp:82
82: <td><div align="center"><%=hello.getBuilding_number()%></div></td>
and similarly for other lines.
How is this caused and how can I solve this?
In your servlet, replace
dispatcher.include(request, response);
by
dispatcher.forward(request, response);
In your JSP, remove
<% buildingservice hello = (buildingservice) request.getAttribute("abcd"); %>
and replace
<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>
by
<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>
See also:
- Our Servlets wiki page
- Our JSP wiki page
- Our EL wiki page
There are by the way many other serious problems in your code, but they are not related to the current concrete problem. I'll however try to sum the most important ones up:
- Code holds DB resources as
static
variables. This is a major threadsafety problem! - Code does not handle exceptions in a sensible manner. This is not developer nor user friendly.
- Code does not respect Java naming conventions. This leads to developer confusion and maintainability problems.
- Code (particularly the
buildingservice
andBuildingDAO
) uses very odd approaches/patterns/flows. It look like to be written by a procedural programmer who doesn't understand Object Oriented Programming concepts. - JSP uses scriptlets which is discouraged since 2003. Keep yourself up to date. Java code belongs in Java classes and JSP should only contain HTML, JSP tags and EL.
- HTML uses deprecated attributes. Keep yourself up to date. Learn CSS.
Work on that as well.
精彩评论