| | |
getAttribute returns NULL
Please support our JSP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Oct 2008
Posts: 1
Reputation:
Solved Threads: 0
Iam not able to pass a value from servlet to jsp using setAttribute and getAttribute.
My test code :
Test1.jsp
=======
TestServlet1.java
============
Test2.jsp
=======
All the time iam getting null from getAttribute.
When i printed the sessionId in the servlet and both jsp (Test1,Test2), it showed thw same session id
the session id is same but the attributes which i set is not there...
code for printing session id
===================
can anyone help me in this ?
Thanks in advance
My test code :
Test1.jsp
=======
JSP Syntax (Toggle Plain Text)
<%@ page language="java" contentType="text/html" %> <html> <head><title>Login Example</title></head> <body> Enter Your Text <hr><p> <form name = login action="../servlet/TestServlet1" method=POST> Your Text : <input type=text name=yourText><br> <input type=submit value=LogIn> </form> </body> </html>
TestServlet1.java
============
JSP Syntax (Toggle Plain Text)
import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class TestServlet1 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { // initalize some variables HttpSession session = request.getSession(true); String userText = request.getParameter("yourText"); session.setAttribute("myText", userText); RequestDispatcher rd = request.getRequestDispatcher("/../TEST/Test2.jsp"); rd.forward(request, response); } } }
Test2.jsp
=======
JSP Syntax (Toggle Plain Text)
<%@ page language="java" contentType="text/html" %> <% String up = (String)session.getAttribute("myText"); %> <html> <head><title>Login Example</title></head> <body> <% if (up == null) { %> its null again <% } else { %> <%= up %> is my text!<br> <% } %> </body> </html>
All the time iam getting null from getAttribute.
JSP Syntax (Toggle Plain Text)
<% String up = (String)session.getAttribute("myText"); %>
When i printed the sessionId in the servlet and both jsp (Test1,Test2), it showed thw same session id
the session id is same but the attributes which i set is not there...
code for printing session id
===================
JSP Syntax (Toggle Plain Text)
String id = session.getId(); System.out.println(id);
can anyone help me in this ?
Thanks in advance
> try this
...which would anyways throw a
> The session attribute is of object datatype.so we have to convert it to string .
What? The `
<% String up = session.getAttribute("myText").toString(); %> ....which would anyways throw a
NullPointerException in this case.> The session attribute is of object datatype.so we have to convert it to string .
What? The `
session ' variable is of the type ` HttpSession '. Last edited by ~s.o.s~; Oct 26th, 2008 at 10:11 am.
I don't accept change; I don't deserve to live.
![]() |
Similar Threads
- How to pass a server side value to an attribute of a custom jsp tag (JSP)
- javascript works in IE but not working in firefox (JavaScript / DHTML / AJAX)
- "cannot resolve symbol"problem (Java)
Other Threads in the JSP Forum
- Previous Thread: Problem in using java beans
- Next Thread: JSF Q' - Dynamically update a displayed web page
| Thread Tools | Search this Thread |
apache array backbutton combobox comma connection csv database development directorystructure dropdownlist dynamicpagetitles eclipse frames glassfish ie8 imagetodatabse imageupload integer internet java javaee javascript jsf jsp jsppagetitles levels mvc2 mvcmodel2 mysql netbeans network parameters passing ping printinserverinsteadofclient read redirect request.getparameter response seperated servlet servletdopost()readxml sessions software sql ssl state_saving_method stocks sun tomcat tutorial update values video web write






