Win a copy of Spring Boot in Practice this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

getAttribute() ...?

 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

In this following servlet and jsp program the output is displaying NULL.Is there any mistake to display my name attribute in jsp?Can anyone explain why this program is not working?Advance Thanks.

Servlet program :

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;

public class ChkAttrib extends HttpServlet
{

public void doPost(HttpServletRequest req,HttpServletResponse res) throws IOException,ServletException
{
res.setContentType("text/html");
PrintWriter out=res.getWriter();
out.println("<html><body><h1>Atrribute Checking</h1><br>");


String name="Kathir";

req.setAttribute("myname",name);
RequestDispatcher view=req.getRequestDispatcher("chk1.jsp");
view.forward(req,res);
}
}


JSP code :

<html>
<head>
<title>My Attibute</title>
</head>
<body>
My name is :<%= (String)request.getAttribute("name") %>

</body>
</html>
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the servlet, you are setting an attribute called "myname". But in the JSP, you are getting an attribute called "name". Different name, different attribute Give it the same name, and it should work.
[ October 02, 2007: Message edited by: Christophe Verre ]
 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Original post:
Christophe Verre
In the servlet, you are setting an attribute called "myname". But in the JSP, you are getting an attribute called "name". Different name, different attribute Give it the same name, and it should work.



It is not working because attributes are set by request.
It have to set through ServletContext or HttpSession object
Then it would work fine.
Otherwise it will provide same null.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

It have to set through ServletContext or HttpSession object


?? I think you're missing something here. Please show us what did not work for you.
 
Vilas Lawande
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for question again?
I have implemented it now .
It is working properly.
Sorry for wrong reply before.

But I don't understand, how attributs set by req object are goes to next page.
These attribute are only valid for same req.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The request is being forwarded to the JSP file. The JSP gets the request, and does its stuff.
 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
To change the name in the jsp file in which you setted in the servlet file we will get the excat output

regards
naresh
reply
    Bookmark Topic Watch Topic
  • New Topic