4
I have a system made for my company and together I have the new website. I can log into the system normally with the accounts registered in the database, but when I enter a filter into Servlet, I get the message:
java.lang.Classcastexception: java.lang.String cannot be cast to UTIL.Usuario
If you delete the filter, the system works normally. The filter I’m using is to prevent the system pages from being accessed via url, so it is always redirected to the login.jsp
.
Follows the code.
FiltroSeguranca.java
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class FiltroSeguranca implements Filter {
public void init(FilterConfig config) throws ServletException {
}
public void doFilter(ServletRequest req, ServletResponse res,
FilterChain chain) throws IOException, ServletException {
HttpSession session = ((HttpServletRequest) req).getSession();
Usuario usuario = (Usuario) session.getAttribute("name");
if (usuario == null) {
session.setAttribute("msg", "Você não está logado no sistema!");
((HttpServletResponse) res).sendRedirect("login.jsp");
} else {
chain.doFilter(req, res);
}
}
public void destroy() {
}
}
web xml.:
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>UTIL.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/LoginServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>Filtro Seguranca</filter-name>
<filter-class>UTIL.FiltroSeguranca</filter-class>
</filter>
<filter-mapping>
<filter-name>Filtro Seguranca</filter-name>
<url-pattern>/GrantedAccess.jsp</url-pattern>
<url-pattern>/Cadastro.jsp</url-pattern>
</filter-mapping>
UPDATED
Login Servlet
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String n = request.getParameter("username");
String p = request.getParameter("userpass");
HttpSession session = request.getSession(false);
if (session != null) {
session.setAttribute("name", n);
}
if (LoginDao.validate(n, p)) {
RequestDispatcher rd = request.getRequestDispatcher("GrantedAccess.jsp");
rd.forward(request, response);
} else {
out.print("<p style=\"color:red\">Sorry username or password error</p>");
RequestDispatcher rd = request.getRequestDispatcher("index.jsp");
rd.include(request, response);
}
out.close();
}
}
LOGIN FORM
<div id="conteudo" class="animated fadeIn">
<div id="raside">
<div class="animated flash">
<h1>
<form action="LoginServlet" method="post">
LOGIN <input autofocus type="text" name="username" required>
SENHA <input type="password" name="userpass" required>
<input type="submit" value="AUTENTICAR">
</form>
</h1>
</div>
</div>
</div>
How are you setting the attribute
name
in the session? Probably the value is not an instance ofUsuario
, rather a string.– Bruno César
It’s coming as String from Loginservlet
– Joel Bezerra
Can include the
LoginServlet
who said?– Bruno César
I just inserted
– Joel Bezerra
Really, you’re setting like string and trying to recover as
Usuario
, I already include an answer for you– Bruno César
Thank you!!!! the/
– Joel Bezerra