Reputation: 275
I am pulling my hair out trying to get a custom function working in JSP. I have read other questions on the topic, but I have not made those mistakes and am following a JSP book (Head First Servlets & JSP) in creating my tag.
Here's my login.tld file located in /WEB-INF/:
<tlib-version>1.2</tlib-version>
<uri>/WEB-INF/login.tld</uri>
<function>
<name>validate</name>
<function-class>org.patrickslagle.model.LoginManager</function-class>
<function-signature>
boolean validate(String username, String password)
</function-signature>
</function>
The method in LoginManager class:
public static boolean validate(String username, String password) {
System.out.println("Servlet");
DatabaseManager dbm = new DatabaseManager();
boolean valid = false;
ResultSet rs = null;
Statement stmt = null;
dbm.setUrl("jdbc:mysql://localhost:3306/pattycakes");
dbm.connect();
try {
stmt = dbm.getConn().createStatement();
String sql = "SELECT * FROM users WHERE email = '" + username + "' AND password = '" + password + "'";
rs = stmt.executeQuery(sql);
if (rs.next()) {
valid = true;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
dbm.disconnect(stmt, dbm.getConn());
}
return valid;
}
}
And the tag in index.jsp:
<%@ taglib prefix="lm" uri="/WEB-INF/login.tld"%>
It is called like so:
<c:if test="${ lm.validate(username, password) }">
with these two inputs on the page in a form:
<input type="text" name="username" id="user_login" class="input"
placeholder="Email Address" value="" size="20" />
<input type="password" name="password" id="user_pass" class="input"
placeholder="Password" value="" size="20" />
If I print to the console, I find that anything inside the test isn't printed and the System.out.println in LoginManager is never printed. It simply skips the test.
Having trouble figuring out the problem in debug mode as well. I am assuming that the expression just isn't being evaluated for some reason and is returning nothing.
Upvotes: 2
Views: 823
Reputation: 4474
In your login.tld, instead of using
boolean validate(String username, String password)
try using
boolean validate(java.lang.String username, java.lang.String password)
also, instead of using
<c:if test="${ lm.validate(username, password) }">
try using
<c:if test="${lm:validate(username, password)}">
if you getting data from request, then you might need
<c:if test="${lm:validate(param.username, param.password)}">
Upvotes: 3