servlet中的Requestdispatcher:forward方法不保留请求属性

时间:2013-11-16 05:55:43

标签: java servlets requestdispatcher

这是我的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);
        }
}

这是我的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是: 我们在第二个servlet。 空值 korlepara

问题是:无法检索属性名称

2 个答案:

答案 0 :(得分:2)

我准备了类似的示例Web应用程序(使用maven和tomcat7插件)。

我的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);
    }

}

我的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"));
    }

}

我的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>

我的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>

输入网址:

http://localhost:9090/Servlet1

我明白了:

we are in second servlet. sanjay korlepara 

所以我能够检索属性名称。

发布更多详细信息和您的web.xml文件,因为您的问题出在其他地方。

答案 1 :(得分:-1)

您在web-xml中为第二个servlet做了哪些映射?