Hi , below is the code I have created login page through adaptive forms and uploaded json file through datasource and created data integrations.Now , i created and api and when i m trying to login the page both if and else is getting printed. First time its printing value and second time its printing value as null for both email and password. Can anyone tell why this is happening?
@Component(
service=Servlet.class,
property={
Constants.SERVICE_DESCRIPTION + "=LoginUserPassword Servlet",
"sling.servlet.methods={GET,POST}",
"sling.servlet.paths=" + "/bin/userPasswordLogin",
"sling.servlet.extensions=" + "json"
}
)
public class JdbcExternalServiceImpl extends SlingAllMethodsServlet {
private static final Logger log = LoggerFactory.getLogger(JdbcExternalServiceImpl.class);
@Reference
private DataSourcePool dataSourcePool;
// The datasource.name value of the OSGi configuration containing the connection this OSGi component will use.
private static final String DATA_SOURCE_NAME = "emailPassword-mysql";
private final Logger logger = LoggerFactory.getLogger(getClass());
@Override
protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
HttpSession session = request.getSession();
String email = request.getParameter("email");
session.setAttribute("email", email);
String password = request.getParameter("password");
session.setAttribute("password", password);
logger.info("Login successful for email: {} {}", email, password);
try {
DataSource dataSource = (DataSource) dataSourcePool.getDataSource(DATA_SOURCE_NAME);
try (Connection connection = dataSource.getConnection();
PreparedStatement statement = connection.prepareStatement("SELECT * FROM signUpForm WHERE email=? AND password=?")) {
statement.setString(1, email);
statement.setString(2, password);
try (ResultSet rs = statement.executeQuery()) {
if (rs.next()) {
// Successful login
logger.info("Login successful for email: {}", email);
} else {
// Failed login
logger.info("Login failed for email: {}", password);
}
}
} catch (SQLException e) {
log.error("Unable to validate SQL connection for [ {} ]", DATA_SOURCE_NAME, e);
// You can send an error message to the client.
}
} catch (DataSourceNotFoundException e) {
log.error("Unable to establish a connection with the JDBC data source [ {} ]", DATA_SOURCE_NAME, e);
// You can send an error message to the client.
}
}
