我想通过调用servlet的doGet()方法来预先填充一些复选框,该方法从SQL数据库中检索数据。我已经看到,添加一个链接到servlet的按钮或href应该调用doGet()方法,但是它不起作用(并且看起来是一种调用方法的混乱方式)。
由于某种原因,下面的代码不会调用doGet方法并因NPE而失败,因为LenderLoanTypes对象为null(doGet方法填充它)。
为什么JSP没有调用doGet()的任何想法?是否有更优雅的方式从JSP调用doGet方法?谢谢。
这是lenderLoanTypes.jsp
的JSP代码<%@page import="com.jexel.util.LenderLoanTypes"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Lender Loan Types</title>
</head>
<body>
<a href="${pageContext.request.contextPath}/LenderLoanType" class="button">Projeler</a>
<form action="/LenderLoanType"><button type="submit">Projeler</button></form><br>
<div>Indicate the types of loans that you make by checking the boxes below: </div>
<div>Then press submit </div>
<% LenderLoanTypes data=(LenderLoanTypes)request.getAttribute("loanTypes"); %>
<form method="get">
<input type="checkbox" name="loanTypes" value="equipmentFinance" <%= (data.isEquipmentFinance() ? "checked=checked" : "") %> >equipment Finance<br>
<input type="checkbox" name="loanTypes" value="inventoryFinance" <%= (data.isInventoryFinance() ? "checked=checked" : "") %> >inventory Finance<br>
<input type="checkbox" name="loanTypes" value="supplyChainFinance" <%= (data.isSupplyChainFinance() ? "checked=checked" : "") %> >supply Chain Finance<br>
<input type="submit" value="Update">
</form>
</body>
</html>
这里是servlet的servlet代码doGet方法LenderLoanType.java
@WebServlet(name = "LenderLoanType", urlPatterns = {"/LenderLoanType"})
public class LenderLoanType extends HttpServlet {
/**
* Handles the HTTP <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println(" in do get ");
HttpSession session = request.getSession();
User user = (User) session.getAttribute("User");
String company = user.getcompany();
System.out.println("company = " + company);
Connection con = (Connection) getServletContext().getAttribute("DBConnection");
PreparedStatement ps = null;
ResultSet rs = null;
LoanTypes loanTypes = null;
try {
ps = con.prepareStatement("select equipmentFinance, inventoryFinance, supplyChainFinance from LenderLoanTypes where company=? limit 1");
ps.setString(1, company);
rs = ps.executeQuery();
if (rs != null && rs.next()) {
loanTypes = new LoanTypes(company, rs.getBoolean("equipmentFinance"), rs.getBoolean("inventoryFinance"), rs.getBoolean("supplyChainFinance"));
} else {
loanTypes = new LoanTypes(company, false, false, false);
}
} catch (SQLException e) {
e.printStackTrace();
// logger.error("Database connection problem");
throw new ServletException("DB Connection problem.");
} finally {
try {
rs.close();
ps.close();
} catch (SQLException e) {
// logger.error("SQLException in closing PreparedStatement or ResultSet");;
}
}
System.out.println("loanTypes = " + loanTypes.toString());
request.setAttribute("loanTypes", loanTypes);
request.getRequestDispatcher("/lenderLoanTypes.jsp").forward(request, response);
}
答案 0 :(得分:1)
添加动作=&#34; / LenderLoanType&#34;在第二个&#34;形式&#34;标签