Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

getAttribute() ...?

 
kathir vel
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • 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>
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • 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 ]
 
Vilas Lawande
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • 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 Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • 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
  • 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 Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The request is being forwarded to the JSP file. The JSP gets the request, and does its stuff.
 
naresh govindaswmay
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic