1
Considering:
public abstract class Usuario {
public Usuario(String login, String senha) {
this.login = login;
this.senha = senha;
}
private String login;
private String senha;
}
public class Professor extends Usuario {
public Professor(String login, String senha, String cpf) {
super(login, senha);
this.cpf = cpf;
}
private String cpf;
}
public class Aluno extends Usuario {
public Aluno(String login, String senha, String matricula) {
super(login, senha);
this.matricula = matricula;
}
private String matricula;
}
How could I turn the above subclasses into ManagedBeans
and how would I pass the values from the view to the attributes of the superclass? I know if I just put the annotation @ManagedBean
in the subclasses will not work.
The main reason for this is that both the teacher and the student will log into the same form and filter (javax.servlet.Filter
) needs to receive a Usuario
to avoid boilerplates in the filter itself and in the subclasses, with repetition of attributes.
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
Usuario u = (Usuario)((HttpServletRequest)request).getSession().getAttribute("usuario");
if (usuario != null && usuario.isAutorizado()) {
chain.doFilter(request, response);
}
else {
String contextPath = ((HttpServletRequest)request).getContextPath();
((HttpServletResponse)response).sendRedirect(contextPath + "login.jsf");
}
}