Link to home
Start Free TrialLog in
Avatar of sangeetha
sangeetha

asked on

Struts - Validate, forward and re-direct.

W.r.to:

https://www.experts-exchange.com/questions/21097832/Struts-Forward-page-Problem.html

1. How can i make sure that if i set redirect=true,
     how can i write an interim stage to save the errors into the session instead of the request, and then write a listener to extract them back out again.

2. Can i use include="blah..blah" in my <action ..> of my struts-config.xml ?

Thanks.
Avatar of girionis
girionis
Flag of Greece image

1. What kind of errors? Validation errors?

2. No AFAIK.
ASKER CERTIFIED SOLUTION
Avatar of TimYates
TimYates
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sangeetha
sangeetha

ASKER

Yes..Validation errors.
:-)
I am getting "sess" cannot be resolved.

Do i need to create a local variable like:

ServletRequest sess = null;
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
whoops sorry...
I am getting:

The method getSession() is undefined for the type ServletRequest

:-(
You have to return an HttpSession object. Look at my comment above.
Yes...I included that and got that error. This is what i have now:

public class MyFilter implements Filter {
   
    /* (non-Javadoc)
     * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
     */
    public void doFilter(ServletRequest request, ServletResponse response,
                               FilterChain chain) throws IOException, ServletException {
              
        HttpSession sess =  request.getSession();
        Object err = sess.getAttribute( org.apache.struts.Globals.ERROR_KEY );
        sess.removeAttribute( org.apache.struts.Globals.ERROR_KEY );
        if( err != null ) {
            request.setAttribute( org.apache.struts.Globals.ERROR_KEY, err ) ;
          chain.doFilter( request, response ) ;
        }
    }

    /* (non-Javadoc)
     * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
     */
    public void init(FilterConfig arg0) throws ServletException {
        // TODO Auto-generated method stub
       
    }

    /* (non-Javadoc)
     * @see javax.servlet.Filter#destroy()
     */
    public void destroy() {
        // TODO Auto-generated method stub
       
    }
}

but i get the above error.
public void doFilter(ServletRequest request, ServletResponse response,
                            FilterChain chain) throws IOException, ServletException {
             
        HttpSession sess =  ((HttpServletRequest)request).getSession();
        Object err = sess.getAttribute( org.apache.struts.Globals.ERROR_KEY );
        sess.removeAttribute( org.apache.struts.Globals.ERROR_KEY );
        if( err != null ) {
            request.setAttribute( org.apache.struts.Globals.ERROR_KEY, err ) ;
          chain.doFilter( request, response ) ;
        }
    }
Can you try this:

HttpSession sess =  ((HttpServletRequest) request).getSession();
Or just change the parameter to be of type HttpServletRequest/Response
Ok..thanks....

now, where should i put:

<filter>
    <filter-name>requestfilter</filter-name>
    <filter-class>my.package.MyFilter</filter-class>
  </filter>

in my web.xml file. I am getting errors if i put anywhere in the file. This is my web.xml :

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
      <servlet>
            <servlet-name>action</servlet-name>
            <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
            <init-param>
                  <param-name>config</param-name>
                  <param-value>/WEB-INF/struts-config.xml</param-value>
            </init-param>
            <init-param>
                  <param-name>debug</param-name>
                  <param-value>3</param-value>
            </init-param>
            <init-param>
                  <param-name>detail</param-name>
                  <param-value>3</param-value>
            </init-param>
            <load-on-startup>0</load-on-startup>
      </servlet>
      <servlet-mapping>
            <servlet-name>action</servlet-name>
            <url-pattern>*.do</url-pattern>
      </servlet-mapping>

</web-app>
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ok...didnt show any error in the file. but i am getting the following error in my 'Console':

15:05:38,660 INFO  [TomcatDeployer] undeploy, ctxPath=/KalendarTool, warUrl=file:/C:/Programme/jboss/server/default/deploy/KalendarTool.war/
15:05:38,785 INFO  [TomcatDeployer] deploy, ctxPath=/KalendarTool, warUrl=file:/C:/Programme/jboss/server/default/deploy/KalendarTool.war/
15:05:39,129 INFO  [PropertyMessageResources] Initializing, config='org.apache.struts.action.ActionResources', returnNull=true
15:05:39,175 ERROR [Digester] Begin event threw exception
java.lang.reflect.InvocationTargetException

// blah..blah..

Caused by: java.lang.LinkageError: loader constraints violated when linking org/apache/struts/action/ActionMapping class
      at java.lang.Class.forName0(Native Method)
      at java.lang.Class.forName(Class.java:141)
      at org.apache.struts.config.FormBeanConfig.class$(FormBeanConfig.java:190)
      at org.apache.struts.config.FormBeanConfig.setType(FormBeanConfig.java:190)
      ... 97 more
15:05:39,191 ERROR [ActionServlet] Parsing error processing resource path
java.lang.reflect.InvocationTargetException

// blah..blah..

Thanks for your responses. I will award points. Meanwhile, Please tell me how can i resolve this.
My web.xml is as follows:

<web-app>
  <filter>
      <filter-name> requestfilter </filter-name>
      <filter-class>de.grassgmbh.kalendartool.filters.MyFilter</filter-class>
  </filter>
<servlet>
// blah...blah..
There is a parsing error somewhere. Does it happen when you try to forward or call a JSP page? Can you post your forward code?
The error is shown when i save the web.xml file !

Is it problem in detecting the MyFilter Package?

I have:

KalendarTool
|
|____ src
|           |
|           |___ de.grassgmbh.kalendartool
|           |                   |
|           |                   |____ resources.properties
|           |
|           |___ de.grassgmbh.kalendartool.form
|           |                   |
|           |                   |___ FilterForm.java
|           |                   |___ RegisterMaskForm.java
|           |                  
|           |___ de.grassgmbh.kalendartool.action                  
|           |                   |
|           |                   |___ FilterAction.java
|           |                   |___ RegisterMaskAction.java
|           |
|           |___ de.grassgmbh.kalendartool.filters
|                               |
|                               |___ MyFilter.java
|
|_____ We-Root
               |
               |____ WEB-INF
               |           |
               |           |____ struts-config.xml, web.xml, etc..
               |
               |____ jsp
                           |
                           |____ MynageCapacity
               |                           |
                                            |____ FilterList.jsp
                                            |____ RegisterMaskList.jsp
                                            |____ ShowList.jsp


BTW,

  <filter-class>de.grassgmbh.kalendartool.filters.MyFilter</filter-class>  refers to the Filter Class.

but why and where we use: <filter-name> requestfilter </filter-name>

?

Thanks.

Can you post the xml file?

> <filter-name> requestfilter </filter-name>

The <filter-name> tag defines the logical name of the filter that you use to access it. It can be different than the actual filter class.