Session in jsp problem

Thread Solved

Join Date: Aug 2009
Posts: 18
Reputation: pandeyprashant is an unknown quantity at this point 
Solved Threads: 0
pandeyprashant pandeyprashant is offline Offline
Newbie Poster

Session in jsp problem

 
0
  #1
Sep 3rd, 2009
Hello every one,
i m trying to use session in jsp application for remembering username from one page to another. it is working fine but when i use hyperlink to come back to home page the username becomes null.
please help me how to remember username when user is using hyperlink?
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,633
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 222
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: Session in jsp problem

 
0
  #2
Sep 3rd, 2009
If you put it in the session it shouldn't got away. You probably put it in the request, so better show some code.
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 18
Reputation: pandeyprashant is an unknown quantity at this point 
Solved Threads: 0
pandeyprashant pandeyprashant is offline Offline
Newbie Poster

Re: Session in jsp problem

 
0
  #3
Sep 3rd, 2009
hello,

the code which i m using to display username on each page is:
this is first page code: content.jsp
  1. <%
  2. String username = request.getParameter("username");
  3. session.setAttribute("username", request.getParameter("username"));
  4. %>
  5. <strong>
  6. Welcome : <%=session.getAttribute("username") %></strong>

this code i am using on the next page: view.jsp
  1. <%
  2. String username = request.getParameter("username");
  3.  
  4. session.setAttribute("username", session.getAttribute("username"));
  5. session.getAttribute("username");
  6. %>
  7. Welcome : <%=session.getAttribute("username")%><br/>
  8. <a href="content.jsp">Home</a>
with the help of hyper link i am trying to go back. but when i click on "Home" it is showing null as username on the content page.
Last edited by peter_budo; Sep 4th, 2009 at 4:38 am. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,633
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 222
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: Session in jsp problem

 
0
  #4
Sep 3rd, 2009
You are not suppose to change the username in the session. You put it once when you log in. You don't set it every time you load a page.
Of course it is null every time you do this:

  1. String username = request.getParameter("username");
  2. session.setAttribute("username", request.getParameter("username"));

If you don't pass the "username" as parameter in the request the first call will return null and then you will set null in the attribute.
The first call is completely useless if you have already put the username in the session after login.

So once you login put the username in the session and that's it. You don't need anything else. All you need to do next is get it from the session whenever you need it.
And after logout set it to null
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 18
Reputation: pandeyprashant is an unknown quantity at this point 
Solved Threads: 0
pandeyprashant pandeyprashant is offline Offline
Newbie Poster

Re: Session in jsp problem

 
0
  #5
Sep 4th, 2009
Hello JavaAddict,
I am passing you my loginpage, content page and view page full code.
please help me how to make session true everywhere, while using hyperlink also.

login.jsp
  1. <%@page import="java.util.Calendar, java.io.*, java.sql.Statement, java.sql.ResultSet, java.sql.Connection, com.userSet.userSet, com.dataEntry.*" session="true"%>
  2. <%@ taglib uri="JavaclassTaglib.tld" prefix="mytaglib" %>
  3. <html>
  4. <head>
  5. <title>LoginPage</title>
  6. <script language="javascript" src="username.js"></script>
  7. <script language="javascript" src="password.js"></script>
  8. </head>
  9. <body bgcolor="#C3D9FF" onload="func1(); func2();">
  10. <h1><font color="blue"><marquee style="margin: 0pt 0px; display: -moz-box;">
  11.  
  12. </marquee></font></h1>
  13. <hr>
  14. <hr>
  15. <%
  16. Calendar cale = Calendar.getInstance();
  17. %>
  18. <table align="right" border="0">
  19. <%= "Date:"+cale.get(Calendar.DATE)+"/"+cale.get(Calendar.MONTH)+"/"+cale.get(Calendar.YEAR)%>
  20. </table>
  21. <a href="http://prashantp:8080/inventoryManagment/">Home</a>
  22. <%
  23. request.getSession(true);
  24. session.setAttribute("username", request.getAttribute("username"));
  25.  
  26. %>
  27. <center>
  28. <h1>Inventory <strong>Management System</strong></h1>
  29. </center>
  30. <strong>
  31. <%
  32. connectionFile conef = new connectionFile();
  33. try
  34. {
  35. String query = "Select * from login" ;
  36. conef.coonnectionfile(query);
  37. while(conef.rs.next())
  38. {
  39. String txt = request.getParameter("username");
  40. String txt1 = request.getParameter("password");
  41.  
  42. String chk = conef.rs.getString("username");
  43. String chk1 = conef.rs.getString("password");
  44. if (chk.equals(txt)&& txt!="" && txt!= null && chk1.equals(txt1) && txt1!="" && txt1!= null)
  45. {
  46. System.out.println("Hello...");
  47. %></strong>
  48. <strong><jsp:forward page="content.jsp"></jsp:forward></strong>
  49. <strong><%
  50. }
  51. else
  52. {
  53. System.out.println("Sorry..."); }
  54. }
  55. %></strong>
  56.  
  57. <table align="right" >
  58. <tr>
  59. <td>
  60. <font size="3"color="red" >
  61. The username or password you entered is incorrect. [?]
  62. </font>
  63. </td>
  64. </tr>
  65. </table>
  66.  
  67. <strong><%
  68. }catch(Exception ex)
  69. {
  70. System.out.println("EXCeption\t"+ex);
  71. }
  72. finally
  73. {
  74. if(conef.con!=null)
  75. {
  76. conef.rs.close();
  77. conef.stmt.close();
  78. conef.con.close();
  79. }
  80. else
  81. {
  82. System.out.println("connection not closed in login.jsp");
  83. }
  84. }
  85. %><br/></strong><br/><table border="2" bordercolor="#5B77CC" bgcolor="white" width="338" height="100" align="right" cellspacing="5" cellpadding="1">
  86. <tr ><td><strong>To make your password more secure:<br> - Use letters and numbers <br>-Use special characters(e.g., @)<br> - Mix lower and uppercase
  87. </strong></td>
  88. </tr>
  89. <tr align="center" height="200" >
  90. <td >
  91. <form method="post" name="form1" onsubmit="displayusername(); displaypassword()"><strong>
  92. Username: <input type="text" name="username" ><br>
  93. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(e.g. [email]fr2rhy@yahoo.com[/email])<br>
  94. Password: <input type="password" name="password" maxlength="32"> <br>
  95. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  96. <span style="border-collapse: separate; color: rgb(0, 0, 0); font-family: 'Times New Roman'; font-size: 16px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;" class="Apple-style-span">
  97. <span style="color: rgb(80, 80, 80); font-family: arial; font-size: 10px; line-height: 12px; text-align: left;" class="Apple-style-span">Capitalization matters.<br>
  98. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  99. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Use 6 to 32 characters, no spaces, .
  100. </span></span><br>
  101. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  102. <input type="submit" value="submit">
  103. <input type="reset" name="reset" value=" reset "><br></strong>
  104. </form>
  105. </td>
  106. </tr>
  107. <tr >
  108. <td><form action="registration.jsp" method = "Post"><strong>
  109. I can not access my account!<a href="http://prashantp:8080/inventoryManagment/forgot.jsp">help</a><br><input type="button" name="forgot" value="Create an account&gt;&gt;"></strong>
  110. </form>
  111. </td>
  112. </tr>
  113. </table>
  114. </body>
  115. </html>


"content.jsp"
  1. <%@page language="java" import="java.util.Calendar,java.io.*, javax.servlet.http.*, java.sql.*, java.sql.SQLException, java.sql.Statement, java.sql.ResultSet, java.sql.Connection, com.dataEntry.*, com.userSet.userSet" errorPage="exceptionHandler.jsp" contentType="text/html" session="true" %>
  2. <html>
  3. <head>
  4. <title>Content Page</title>
  5. <script language="JavaScript" type="text/JavaScript">
  6.  
  7. </script>
  8. </head>
  9. <body bgcolor="#C3D9FF" link="#0000FF">
  10. <marquee>
  11. <font color="blue" size="30"</font>
  12. </marquee><hr>
  13. <%
  14. Calendar cale = Calendar.getInstance();
  15. %>
  16. <table align="right" border="0">
  17. <tr>
  18. <td align = "right">
  19. <%= "Date:"+cale.get(Calendar.DATE)+"/"+cale.get(Calendar.MONTH)+"/"+cale.get(Calendar.YEAR)%><br/>
  20.  
  21. </td>
  22. </tr>
  23. </table>
  24.  
  25. <%
  26.  
  27. session.setAttribute("username", request.getParameter("username"));
  28. %>
  29. <strong>
  30.  
  31. Welcome : <%=session.getAttribute("username") %></strong>
  32.  
  33. <%
  34. try
  35. {
  36. connectionFile conef=new connectionFile();
  37. String query = "Select username from login" ;
  38. conef.coonnectionfile(query);
  39. int count=0;
  40. conef.stmt.close();
  41. conef.con.close();
  42. }
  43. catch(Exception e)
  44. {
  45. System.out.println("EXCEPTION"+e);
  46. }
  47. %>
  48.  
  49. <center>
  50. <h1><strong>Inventory Management System</strong></h1><strong><br></strong>
  51. </center>
  52. <div id="Layer1" style="position:absolute; width:794px; height:115px; z-index:1; left: 217px; top: 178px;" >
  53. <table width="748" border="0">
  54. <tr>
  55. <td width="641"> <strong><font size="+1">Inventory is a list for goods and materials held in stock by a business. </font></strong></td>
  56. <td width="97" ><strong><a href="http://prashantp:8080/inventoryManagment/view.jsp"> view</a></strong> </td>
  57. </tr>
  58. <tr>
  59. <td> <strong><font size="+1">Inventories not intended for sale to customers or to clients may be held in any premises for organization uses. </font></strong></td>
  60. <td><strong><a href="http://prashantp:8080/inventoryManagment/add.jsp">add</a> &nbsp;</strong></td>
  61. </tr>
  62. <tr>
  63. <td> <strong><font size="+1">Inventory is a necessary part of doing business </font></strong></td>
  64. <td><strong><a href="http://prashantp:8080/inventoryManagment/update.jsp">update</a> &nbsp;</strong></td>
  65. </tr>
  66. <tr>
  67. <td> <strong><font size="+1">The functional factors explain the existence of inventory. </font></strong></td>
  68. <td><strong><a href="http://prashantp:8080/inventoryManagment/delete.jsp">delete</a> &nbsp;</strong></td>
  69. </tr>
  70. <tr>
  71. <td><strong><font size="+1">With the help of following link you can view, add update and delete the data from the database. </font></strong></td>
  72. <td><strong><a href="http://prashantp:8080/inventoryManagment/content.jsp">cancel</a> &nbsp;</strong></td>
  73. </tr>
  74. </table>
  75. </div>
  76. </body>
  77. </html>

"view.jsp"
  1. <%@page language="java" import="java.util.Calendar,java.io.*, javax.servlet.http.*, java.sql.*, java.sql.SQLException, java.sql.Statement, java.sql.ResultSet, java.sql.Connection, com.dataEntry.*, com.userSet.userSet" errorPage="exceptionHandler.jsp" contentType="text/html" session="true" %>
  2. <html>
  3. <head>
  4. <title>Content Page</title>
  5. <script language="JavaScript" type="text/JavaScript" ></script>
  6. </head>
  7. <body bgcolor="#C3D9FF">
  8. <marquee>
  9. <font color="blue" size="30"></font>
  10. </marquee><hr><hr>
  11. <%connectionFile cone = new connectionFile(); %>
  12. <%
  13. Calendar cale = Calendar.getInstance();
  14. %>
  15. <table align="right" border="0">
  16. <tr>
  17. <td align="right">
  18. <%= "Date:"+cale.get(Calendar.DATE)+"/"+cale.get(Calendar.MONTH)+"/"+cale.get(Calendar.YEAR)%><br/>
  19. </td>
  20. </tr>
  21. </table>
  22.  
  23.  
  24. Welcome : <%=session.getAttribute("username")%><br/>
  25. <a href="content.jsp">Home</a>
  26.  
  27. <center>
  28. <h1>Inventory Management System</h1>
  29.  
  30. <table border="1" cellspacing="15" bgcolor="white">
  31. <tr>
  32. <th>productId</th>
  33. <th>&nbsp; &nbsp;&nbsp;&nbsp; itemid &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <br></th>
  34. <th>&nbsp;&nbsp; &nbsp;&nbsp; name&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; <br></th>
  35. <th>Type</th>
  36. <th>&nbsp;&nbsp; &nbsp;&nbsp; companyname &nbsp; &nbsp; &nbsp; <br></th>
  37. <th>&nbsp;dateOfComingStock</th>
  38. <th>&nbsp;&nbsp;&nbsp;&nbsp; quantity&nbsp;&nbsp;&nbsp; <br></th>
  39. <th>weight</th>
  40. <th>&nbsp;&nbsp;&nbsp; costPrice&nbsp;&nbsp;&nbsp;&nbsp; <br></th>
  41. <th>&nbsp;&nbsp;&nbsp; sellingPrice&nbsp;&nbsp;&nbsp; <br></th>
  42. <th>manufacturingDate</th>
  43. <th>expiryDate</th>
  44. <th>color</th>
  45. </tr>
  46. </table>
  47. </center>
  48.  
  49. <%
  50. String que="Select * from itemdetail";
  51. cone.coonnectionfile(que);
  52. while(cone.rs.next())
  53. {
  54. String productId = cone.rs.getString("productId");
  55. String itemid = cone.rs.getString("itemid");
  56. String name = cone.rs.getString("name");
  57. String Type = cone.rs.getString("Type");
  58. String companyname = cone.rs.getString("companyname");
  59. String dateOfComingStock = cone.rs.getString("dateOfComingStock");
  60. String quantity = cone.rs.getString("quantity");
  61. String weight = cone.rs.getString("weight");
  62. String costPrice = cone.rs.getString("costPrice");
  63. String sellingPrice = cone.rs.getString("sellingPrice");
  64. String manufacturingDate = cone.rs.getString("manufacturingDate");
  65. String expiryDate = cone.rs.getString("expiryDate");
  66. String color = cone.rs.getString("color");
  67. %>
  68. <center>
  69. <table border="1" width="25" height="20" cellspacing="10" cellpadding="5" bgcolor="white">
  70. <tr>
  71. <th>
  72. <%=productId%>
  73. </th>
  74. <th>
  75. <%=itemid%>
  76. </th>
  77. <th>
  78. <%=name%>
  79. </th>
  80. <th>
  81. <%=Type%>
  82. </th>
  83. <th>
  84. <%=companyname%>
  85. </th>
  86. <th>
  87. <%=dateOfComingStock %>
  88. </th>
  89. <th>
  90. <%=quantity %>
  91. </th>
  92. <th>
  93. <%=weight%>
  94. </th>
  95. <th>
  96. <%=costPrice %>
  97. </th>
  98. <th>
  99. <%=sellingPrice %><br/>
  100. </th>
  101. <th>
  102. <%=manufacturingDate%>
  103. </th>
  104. <th>
  105. <%=expiryDate%>
  106. </th>
  107. <th>
  108. <%=color%>
  109. </th>
  110. </tr>
  111. </table>
  112. </center>
  113. <%
  114. }
  115. %>
  116. </body>
  117. </html>

please help me.....
Last edited by peter_budo; Sep 4th, 2009 at 4:40 am. Reason: Please use code tags as[code]YOUR CODE HERE[/code]
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,633
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 222
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: Session in jsp problem

 
0
  #6
Sep 4th, 2009
When the user logs in take the username and put it the session:
  1. String username = request.getParameter("username");
  2. String password = request.getParameter("password");
  3.  
  4. boolean b = checkLogin(username, password);
  5.  
  6. if (b) {
  7. // user valid
  8. request.getSession.setAttribute("username", username);
  9. } else {
  10. // Error: invalid username, password
  11. }

That's it. It's in the session. You don't need to put it every time you go to a page, because it's IN THE SESSION . Just get it and check if it is not null.
Also learn the difference between session and request, and what it means to put something inside each one of them.

Also when you logout do this:
  1. request.getSession.setAttribute("username", null);

These are the only two times where you will need to set the value of the session.

I didn't read your code nor will I ever read such un-formatted code.
Last edited by javaAddict; Sep 4th, 2009 at 4:07 am.
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 18
Reputation: pandeyprashant is an unknown quantity at this point 
Solved Threads: 0
pandeyprashant pandeyprashant is offline Offline
Newbie Poster

Re: Session in jsp problem

 
0
  #7
Sep 4th, 2009
hello javaAddict,
sorry to disturbe you, but as you suggested me the code like
[
request.getSession.getAttribute("username");
]
i tried this, but it is not working.

please check my this sample code.

Demosession.html
[
<<HTML>
<BODY>
<FORM METHOD=POST ACTION="DemoSession.jsp">
What's your name? <INPUT TYPE=TEXT NAME=username SIZE=20>
<P><INPUT TYPE=SUBMIT>
</FORM>
</BODY>
</HTML>

>
]
in this page i have written just input box and sending it to the next page DemoSession.jsp

[
<%
String name = request.getParameter( "username" );
session.setAttribute( "theName", name );
%>
<HTML>
<BODY>
Hello, <%=session.getAttribute("theName")%>
<A HREF="NextPage.jsp">Continue</A>
</BODY>
</HTML>
]
in this, i have given one link for another page and displaying the username.
Now from the NextPage.jsp, i am trying to come back to DemoSession.jsp and trying to display the username on NextPage.jsp.
code of NextPage.jsp is
[
<HTML>
<BODY>
Hello, <%= session.getAttribute( "theName" ) %>
<a href= "DemoSession.jsp">home</a>
</BODY>
</HTML>
]

In the above code everything is working fine, but when i click Home link on NextPage.jsp to come back at DemoSession.jsp the username is becoming null.
Please help me how to make it correct, so if user come back the username should not be null.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,633
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 222
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: Session in jsp problem

 
0
  #8
Sep 4th, 2009
When you go back to the DemoSession.jsp this code is executed:

  1. String name = request.getParameter( "username" );
  2. session.setAttribute( "theName", name );

But this: request.getParameter( "username" ) will return null since you didn't submit anything in the request. So you get null and you put null. There are ways to fix this but we generally don't do things that way. You could do this:
  1. <a href= "DemoSession.jsp?username=<%= session.getAttribute( "theName" ) %>">home</a>
Also whenever you go to that page you need to make sure that you have submitted a field with name "username" or put it in the link as described.

Usually I don't get such a problem because I don't put these things together:
  1. String name = request.getParameter( "username" );
  2. session.setAttribute( "theName", name );

I put the setting of the session at a page before the homepage. So when I go to homepage I already have the session. And I don't return to the first page unless I have logged out.


You can also do this:
  1. String name = request.getParameter( "username" );
  2. String sessName = session.getAttribute( "theName" );
  3.  
  4. //check whether one of the above is null and act accordingly
Example: if name is null and sessName not null you can do this:
name = sessName
Or put the name in the session only when it is not null.
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 18
Reputation: pandeyprashant is an unknown quantity at this point 
Solved Threads: 0
pandeyprashant pandeyprashant is offline Offline
Newbie Poster

Re: Session in jsp problem

 
0
  #9
Sep 5th, 2009
@javaAddict,
Thanks javaAddict. this is working fine.
but as you suggested, i tried storing session first in login page only but the same error was coming so i tried to store it in the content page.
but i want to say thanks again!!!
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 1
Reputation: marysbv is an unknown quantity at this point 
Solved Threads: 0
marysbv marysbv is offline Offline
Newbie Poster
 
0
  #10
19 Days Ago
I have similar prob. The get attribute is null while the id is not null.
for my java:
  1. if(session.getAttribute(abc)==null)
  2. System.out.println(abc is null
  3. else
  4. System.out.println(abc is not null)
  5.  
  6. if(session.getId()==null)
  7. System.out.println(id is null)
  8. else
  9. System.out.println(abc is not null)

Do i need to also use session.setAttribute(abc) in the jsp?
Last edited by peter_budo; 19 Days Ago at 4:18 am. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC