I have a servlet returning a boolean value that I want rendered to my JSP, not sure what code I need to use.
Things I've looked into but haven't worked yet is using (JSTL) and what ever this, ${serviceRequestData['FN_Contact']}, is.
Servlet name is CustomerExist and method within that servlet is 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>
The above code gives me this error when in my jsp "An exception occurred processing [/CustomerExist.jsp]".
I just want true or false to appear to the user.
Change your doPost method, setAttribute exist to request.
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);
}
In JSP get attribute by
<c:out value="${customerExist}" default="default value of c:out"/>