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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With