我正在尝试从我的java类的getter中检索和显示数据,但无法显示结果。它在我的jsp页面中显示0和null。我无法弄清楚这个问题。我首先添加了servlet代码,然后添加了DAO代码,然后添加了jsp代码。
PrintWriter out = response.getWriter();
String input = request.getParameter("list");
String txtField = request.getParameter("txtField");
String op = request.getParameter("op");
String bSearch = request.getParameter("bookingSearch");
try{
if(op.equals("1")){
Searching search1 = new Searching();
search1.setInputList(input);
search1.setTxtField(txtField);
SearchDAO searchDAO = new SearchDAO();
boolean searchDriver = searchDAO.searchDriver(search1);
if(searchDriver){
response.sendRedirect("driversearchresult.jsp");
//response.sendRedirect("SearchOutPut.jsp?op=" + search1.getPassingValue());
}
else {
out.println("<html>");
out.println("<head>");
out.println("<script>");
out.println("alert('Record Not Found!')");
out.println("</script>");
out.println("<META HTTP-EQUIV= Refresh CONTENT=0;URL=SearchDriver.jsp>");
}
}
package com.vechile.dao;
import java.sql.ResultSet;
import com.vechile.model.DriverSearchResult;
import com.vechile.model.Searching;
import com.vechile.utils.DBConnection;
public class SearchDAO {
private String sql;
private ResultSet rs, rs1;
public boolean searchDriver(Searching searching) {
try {
sql = String.format("select * from adddriver where (%s) = '%s'",
searching.getInputList(), searching.getTxtField());
rs = DBConnection.executeQuery(sql);
if(rs.next()) {
DriverSearchResult searchResult = new DriverSearchResult();
Integer driverId = new Integer(rs.getInt(1));
String driverIdString = driverId.toString();
searchResult.setId(driverIdString);
searchResult.setName(rs.getString("dname"));
searchResult.setAddress(rs.getString("daddress"));
searchResult.setCity(rs.getString("dcity"));
searchResult.setContact(rs.getString("dcontact"));
searchResult.setCountry(rs.getString("dcountry"));
return true;
}
} catch (Exception e) {
e.getStackTrace();
}
return false;
}
public boolean searchBooking(Searching searching) {
try {
sql = String.format(
"select * from booking where booking_id = '%s'",
searching.getBookingSearch());
rs1 = DBConnection.executeQuery(sql);
if (rs1.next()) {
return true;
}
} catch (Exception e) {
e.getStackTrace();
}
return false;
}
}
<%@page import="com.vechile.dao.UserDAO"%>
<%@page import="com.vechile.model.DriverSearchResult"%>
<%@page import="com.vechile.model.Vehicle"%>
<%@page import="com.vechile.model.Searching"%>
<%@page import="java.util.List"%>
<%@page import="com.vechile.dao.SearchDAO"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Booking Search Result</title>
</head>
<body>
<%
DriverSearchResult dSR = new DriverSearchResult();
%>
<table>
<th>ID</th>
<tr>
<td><%=dSR.getId() %></td>
</tr>
<th>Name</th>
<tr>
<td><%=dSR.getName() %></td>
</tr>
</table>
</body>
</html>
答案 0 :(得分:2)
问题是您在jsp代码中创建了new
DriverSearchResult
:
DriverSearchResult dSR = new DriverSearchResult();
由于它是一个新对象,因此其值未初始化。然后你打印出来。
有问题的代码:
<%
DriverSearchResult dSR = new DriverSearchResult(); // <- you create a new object
%>
<table>
<th>ID</th>
<tr>
<td><%=dSR.getId() %></td> <!-- <-- you print its id which is 0 -->
</tr>
<th>Name</th>
<tr>
<td><%=dSR.getName() %></td> <!-- <-- you print its name which is null -->
</tr>