Issue
I am working through a tutorial from this JSP Murach book. The example I am working through asks the user to enter text in three fields in index.jsp. The fields send their entries to the servlet and the servlet doPost tests whether any of the three fields were left blank. If this occurs, index.jsp is dispatched again, and any fields which were not blank are filled with their previous entries. I believe I have done this as the book shows as well as what this site shows under 3. Passing Objects, but I still get the following error.
javax.el.PropertyNotFoundException: Property [inputA] not readable on type [package01.User]
Perhaps my User class is set up improperly? I don't really understand how the JSP is supposed to understand "message" or "user". Clearly, it doesn't understand the "user" properties of inputA, inputB, inputC.
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Index JSP</title>
</head>
<body>
<p>Index JSP body</p>
<p>${message}</p>
<form action="servlet01" method="post">
<input type="hidden" name="nextPage" value="revisitIndex">
Input A:<input title="A" type="text" name="inputA" value="${user.inputA}"><br>
Input B:<input title="B" type="text" name="inputB" value="${user.inputB}"><br>
Input C:<input title="C" type="text" name="inputC" value="${user.inputC}"><br>
<input type="submit" value="Next Page">
</form>
</body>
</html>
Servlet01 doPost
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String nextPage = request.getParameter("nextPage");
if (nextPage == null){
nextPage = "continueToThanks";
}
String url = "/thanks.jsp";
String message = "";
if(nextPage.equals("revisitIndex")){
String inputA = request.getParameter("inputA");
String inputB = request.getParameter("inputB");
String inputC = request.getParameter("inputC");
User user = new User(inputA, inputB, inputC);
if(inputA == null || inputB == null || inputC == null ||
inputA.isEmpty() || inputB.isEmpty() || inputC.isEmpty()){
url = "/index.jsp";
message = "Please fill out all three text boxes.";
}
request.setAttribute("message", message);
request.setAttribute("user", user);
}
getServletContext()
.getRequestDispatcher(url)
.forward(request, response);
}
User class
package package01;
class User {
private String inputA;
private String inputB;
private String inputC;
User(String a, String b, String c){
inputA = a;
inputB = b;
inputC = c;
}
public void setInputA(String a){ inputA = a; }
public void setInputB(String b){ inputB = b; }
public void setInputC(String c){ inputC = c; }
public String getInputA(){ return inputA;}
public String getInputB(){ return inputB;}
public String getInputC(){ return inputC;}
}
Solution
As JB Nizet commented, my User class needed to be public. Without the public access modifier, nothing outside of the package named "package01" can access the class.
Answered By - Jack J