Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Requestdispatcher in servlets: forward method not preserving request attribute

This is my prog1:

public class DispatcherSample extends HttpServlet
{
    public void service(HttpServletRequest request,HttpServletResponse response)throws IOException,ServletException
    {
        PrintWriter out=response.getWriter();
        out.println("we are in first servlet.");
        request.setAttribute("name","sanjay");
        RequestDispatcher rd=request.getRequestDispatcher("/ss");
        rd.forward(request,response);
        }
}

This is my prog2:

public class SecondServlet extends HttpServlet
{
    public void service(HttpServletRequest request,HttpServletResponse response)throws IOException,ServletException
    {
        PrintWriter out=response.getWriter();
        out.println("we are in second servlet.");
        request.setAttribute("surname","korlepara");
        //RequestDispatcher rd=request.getRequestDispatcher("/ts");
        //rd.forward(request,response);
        out.println(request.getAttribute("name"));
        out.println(request.getAttribute("surname"));
    }
}   

OUTPUT is: we are in second servlet. null korlepara

Problem is: unable to retrieve attribute name

like image 841
sanjay Avatar asked Nov 11 '22 18:11

sanjay


1 Answers

I prepared analogous sample web app (using maven and tomcat7 plugin).

My sevlet 1:

public class Servlet1 extends HttpServlet {
    private static final long serialVersionUID = 1L;

    @Override
    public void service(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        PrintWriter out = response.getWriter();
        out.println("we are in first servlet.");
        request.setAttribute("name", "sanjay");
        RequestDispatcher rd = request.getRequestDispatcher("/Servlet2");
        rd.forward(request, response);
    }

}

My sevlet 2:

public class Servlet2 extends HttpServlet {
    private static final long serialVersionUID = 1L;

    @Override
    public void service(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        PrintWriter out = response.getWriter();
        out.println("we are in second servlet.");
        request.setAttribute("surname", "korlepara");
        out.println(request.getAttribute("name"));
        out.println(request.getAttribute("surname"));
    }

}

My web.xml

<web-app>
<display-name>Archetype Created Web Application</display-name>

<servlet>
    <servlet-name>Servlet1</servlet-name>
    <display-name>Servlet1</display-name>
    <description></description>
    <servlet-class>com.example.Servlet1</servlet-class>
</servlet>
<servlet>
    <servlet-name>Servlet2</servlet-name>
    <display-name>Servlet2</display-name>
    <description></description>
    <servlet-class>com.example.Servlet2</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>Servlet1</servlet-name>
    <url-pattern>/Servlet1</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>Servlet2</servlet-name>
    <url-pattern>/Servlet2</url-pattern>
</servlet-mapping>

</web-app>

My pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.sample.webapp</groupId>
    <artifactId>SampleJspWebApp</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>SampleJspWebApp Maven Webapp</name>
    <url>http://maven.apache.org</url>

    <dependencies>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.0.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <finalName>SampleJspWebApp</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.2</version>
                <configuration>
                    <port>9090</port>
                    <path>/</path>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

Entering URL:

http://localhost:9090/Servlet1

I get:

we are in second servlet. sanjay korlepara 

So i am able to retrieve attribute name.

Post more details and your web.xml file because your problem is somewhere else.

like image 183
bary Avatar answered Nov 15 '22 04:11

bary