Need some help (Urgent)
Hi all,
I am writing the following filter code, in this the login page is displayed well but before displaying the home page the filter is invoked and
in the filter the user is still null and also the session is not new, hence its not
forwarding the request to the home page and hence doing nothing. Can some one suggest me with
the solution, so that the request is forwarded to the home page.
publicvoid doFilter(javax.servlet.ServletRequest servletRequest,
javax.servlet.ServletResponse servletResponse, javax.servlet.FilterChain
filterChain)throws java.io.IOException, javax.servlet.ServletException{
User user;
HttpServletResponse response = (HttpServletResponse)servletResponse;
HttpServletRequest request = ((HttpServletRequest)servletRequest);
HttpSession session =request.getSession();
System.out.println(session+"*************"+session.isNew());
if (session.isNew())
{
filterChain.doFilter(servletRequest, servletResponse);
}
user = (User)session.getAttribute("user");
System.out.println(user);
if(user ==null){
response.sendRedirect("logon.do");
}else{
filterChain.doFilter(servletRequest, servletResponse);
}
[1761 byte] By [
Sowja] at [2007-11-27 6:38:28]

# 1
So you are saying you get to the login screen ok, but submitting the username/password from the login screen doesn't get processed because the filter interferes and vetoes it?
So you need to make your filter smarter and check if the person is trying to log in, and allow that transaction to go ahead. Maybe check for a request parameter? request.getParameter("loginName") != null?
Cheers,
evnafets
# 2
No I am not checking for the login, and just checking for the session in the filter code, the user is an instance of the formbean which contains the user info.
when trying to login to the home page through the filter, the filter is not forwarding the request to the home page throwing the following exception
java.lang.ClassCastException: java.lang.String
at com.grems.struts.BasicFilter.doFilter(BasicFilter.java:29)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:42)
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3200)
at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:121)
at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:1983)
at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:1890)
at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1344)
at weblogic.work.ExecuteThread.execute(ExecuteThread.java:209)
at weblogic.work.ExecuteThread.run(ExecuteThread.java:181)
Sowja at 2007-7-12 18:07:08 >

# 3
You're getting a ClassCastException at line 29?
Which is line 29 in the above code?
You are trying to cast something that is a String to a class not compatible with String.
At a guess I would say it would be:
user = (User)session.getAttribute("user");
Where and how do you create the session attribute "user"?
What value is in it?
# 4
I created the session attribute in the LoginAction, here User is the formbean for the login form
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws IOException{
User userForm = (User)form;
String user = userForm.getUserId();
String password = userForm.getPassword();
HttpSession session = request.getSession(false);
boolean check;
try{
/*System.out.println(session.getAttribute("user"));
if (session.getAttribute("user") == null) {
response.setHeader("Cache-Control","no-cache");
response.setHeader("Pragma","no-cache");
response.setHeader("Expires","0");
response.sendRedirect("logon.do");
}*/
BusinessClass bclass = BusinessClass.getInstance();
check = bclass.checkUser(user,password);
session.setAttribute("user",user);
if(check==false)
{
return mapping.findForward("failure");
}
}
catch(SQLException sqle)
{
Sowja at 2007-7-12 18:07:08 >

# 5
when I give the username and password and submit the form the following exception is thrown
java.lang.ClassCastException: java.lang.String
at com.grems.struts.BasicFilter.doFilter(BasicFilter.java:29)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:42)
at weblogic.servlet.internal.RequestDispatcherImpl.invokeServlet(RequestDispatcherImpl.java:497)
at weblogic.servlet.internal.RequestDispatcherImpl.forward(RequestDispatcherImpl.java:245)
at org.apache.struts.action.RequestProcessor.doForward(RequestProcessor.java:1063)
at org.apache.struts.tiles.TilesRequestProcessor.doForward(TilesRequestProcessor.java:263)
at org.apache.struts.action.RequestProcessor.processForwardConfig(RequestProcessor.java:386)
at org.apache.struts.tiles.TilesRequestProcessor.processForwardConfig(TilesRequestProcessor.java:318)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:229)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1194)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:763)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:225)
at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:127)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:283)
at weblogic.servlet.internal.TailFilter.doFilter(TailFilter.java:26)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:42)
at com.grems.struts.BasicFilter.doFilter(BasicFilter.java:41)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:42)
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3200)
at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:121)
at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:1983)
at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:1890)
at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1344)
at weblogic.work.ExecuteThread.execute(ExecuteThread.java:209)
at weblogic.work.ExecuteThread.run(ExecuteThread.java:181)
And when I refresh the page the exception is changed to
java.lang.ClassCastException: java.lang.String
at com.grems.struts.BasicFilter.doFilter(BasicFilter.java:29)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:42)
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3200)
at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:121)
at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:1983)
at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:1890)
at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1344)
at weblogic.work.ExecuteThread.execute(ExecuteThread.java:209)
at weblogic.work.ExecuteThread.run(ExecuteThread.java:181)
over here it is checking for the user name and password in in the LogonAction but failing to check the for the session in the filter.
Sowja at 2007-7-12 18:07:08 >

# 6
Take a closer look at your code
// setting session attribute "user" to be of type java.lang.String
String user = userForm.getUserId();
session.setAttribute("user",user);
...
...
// retrieving session attribute "user" and cast to User
user = (User)session.getAttribute("user");
The session attribute is a String.
You are attempting to cast it to a User object
Java can't do that, so it throws a ClassCastException to let you know this.
Solution - either
- put a User object into the session
- retrieve the session attribute as a String rather than a User.
# 7
Thanks a lot for the solution, but i rectified it just before and now I am facing with another problem with the session even if I log off and click the back button on
the browser its still showing the home page for the corresponding user. And also when I log off I need it to be redirected to log in and its not happening, What
can I do to solve that. Is there any problem with my filter?
Sowja at 2007-7-12 18:07:08 >
