简体   繁体   中英

doGet() Servlet method not being invoked from JSP

I'm tying to prepopulate some checkboxes by calling a servlet's doGet() method that retrieves data from an SQL database. I've seen on SO that adding a button or an href that links to the servlet should call the doGet() method but it's not working (and seems a messy way of calling the method).

For some reason the code below doesn't invoke the doGet method and fails with an NPE as the LenderLoanTypes object is null (the doGet method populates it).

Any ideas why the doGet() isn't being called by the JSP ? Is there a more elegant way of calling the doGet method from the JSP? Thank you.

Here is the JSP code for lenderLoanTypes.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>

and here is the servlet code doGet method for the servlet 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);
}

在第二个“表单”标签上添加action =“ / LenderLoanType”

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM