如何在jsp中呈现来自servlet的对象变量?

时间:2019-06-13 04:13:13

标签: java jsp servlets jstl

我有一个servlet向我的JSP返回要渲染的布尔值,不确定我需要使用什么代码。

我研究过但尚未使用的东西正在使用(JSTL),而这就是$ {serviceRequestData ['FN_Contact']}}。

Servlet名称为CustomerExist,该Servlet中的方法为customerExist。

package MainPackage;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/CustomerExist")
public class CustomerExist extends HttpServlet  implements DBAccessVariables {
    private static final long serialVersionUID = 1L;


    public CustomerExist() {
        super();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String firstName = request.getParameter("firstName");
        String lastName = request.getParameter("lastName");
        customerExist(firstName, lastName);
    }

    private boolean customerExist(String firstName, String lastName) {
        boolean answer = false;
        try {
             Class.forName("com.mysql.cj.jdbc.Driver");

             Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);

             PreparedStatement ps = conn
                        .prepareStatement("select * from Customers WHERE FirstName = ? and LastName = ?");

             ps.setString(1, firstName);
             ps.setString(2, lastName);

             final ResultSet resultSet = ps.executeQuery();

             if (resultSet.next() == false) { 
                 answer = false;
                 } else 
                { 
                do { 
                    answer = true;
                    } while (resultSet.next()); 
                }            

             ps.close();
             conn.close();
          } catch(SQLException se) {
             se.printStackTrace();
          } catch(Exception e) {
             e.printStackTrace();
          }
        System.out.println(answer);
        return answer;
    }
}
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"
    import="MainPackage.*"
    %>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Customer Exist?</title>
</head>
<body>

<c:out value="${CustomerExist.customerExist}" default="default value of c:out"/>
<%-- <c:out value="${'Test'}" default="default value of c:out"/> --%>

</body>
</html>

在我的jsp中,“上面的代码在处理[/CustomerExist.jsp]发生异常时”给了我这个错误。

我只希望对用户显示对或错。

1 个答案:

答案 0 :(得分:0)

更改您的doPost方法,setAttribute存在以进行请求。

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String firstName = request.getParameter("firstName");
        String lastName = request.getParameter("lastName");
        boolean exist = customerExist(firstName, lastName);
        request.setAttribute("customerExist", exist);
    }

在JSP中,获取属性为

<c:out value="${customerExist}" default="default value of c:out"/>