gpt4 book ai didi

java - servlet 中的请求调度程序 : forward method not preserving request attribute

转载 作者:搜寻专家 更新时间:2023-10-31 20:34:42 25 4
gpt4 key购买 nike

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

输出是:我们在第二个 servlet 中。无效的科勒帕拉

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

最佳答案

我准备了类似的示例网络应用程序(使用 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 文件,因为您的问题出在其他地方。

关于java - servlet 中的请求调度程序 : forward method not preserving request attribute,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20015294/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com