| | |
Session in jsp problem
Please support our JSP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
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
•
•
Join Date: Aug 2009
Posts: 18
Reputation:
Solved Threads: 0
hello,
the code which i m using to display username on each page is:
this is first page code: content.jsp
this code i am using on the next page: view.jsp
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.
the code which i m using to display username on each page is:
this is first page code: content.jsp
jsp Syntax (Toggle Plain Text)
<% String username = request.getParameter("username"); session.setAttribute("username", request.getParameter("username")); %> <strong> Welcome : <%=session.getAttribute("username") %></strong>
this code i am using on the next page: view.jsp
jsp Syntax (Toggle Plain Text)
<% String username = request.getParameter("username"); session.setAttribute("username", session.getAttribute("username")); session.getAttribute("username"); %> Welcome : <%=session.getAttribute("username")%><br/> <a href="content.jsp">Home</a>
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.
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:
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
Of course it is null every time you do this:
JSP Syntax (Toggle Plain Text)
String username = request.getParameter("username"); 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
•
•
Join Date: Aug 2009
Posts: 18
Reputation:
Solved Threads: 0
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
"content.jsp"
"view.jsp"
please help me.....
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
jsp Syntax (Toggle Plain Text)
<%@page import="java.util.Calendar, java.io.*, java.sql.Statement, java.sql.ResultSet, java.sql.Connection, com.userSet.userSet, com.dataEntry.*" session="true"%> <%@ taglib uri="JavaclassTaglib.tld" prefix="mytaglib" %> <html> <head> <title>LoginPage</title> <script language="javascript" src="username.js"></script> <script language="javascript" src="password.js"></script> </head> <body bgcolor="#C3D9FF" onload="func1(); func2();"> <h1><font color="blue"><marquee style="margin: 0pt 0px; display: -moz-box;"> </marquee></font></h1> <hr> <hr> <% Calendar cale = Calendar.getInstance(); %> <table align="right" border="0"> <%= "Date:"+cale.get(Calendar.DATE)+"/"+cale.get(Calendar.MONTH)+"/"+cale.get(Calendar.YEAR)%> </table> <a href="http://prashantp:8080/inventoryManagment/">Home</a> <% request.getSession(true); session.setAttribute("username", request.getAttribute("username")); %> <center> <h1>Inventory <strong>Management System</strong></h1> </center> <strong> <% connectionFile conef = new connectionFile(); try { String query = "Select * from login" ; conef.coonnectionfile(query); while(conef.rs.next()) { String txt = request.getParameter("username"); String txt1 = request.getParameter("password"); String chk = conef.rs.getString("username"); String chk1 = conef.rs.getString("password"); if (chk.equals(txt)&& txt!="" && txt!= null && chk1.equals(txt1) && txt1!="" && txt1!= null) { System.out.println("Hello..."); %></strong> <strong><jsp:forward page="content.jsp"></jsp:forward></strong> <strong><% } else { System.out.println("Sorry..."); } } %></strong> <table align="right" > <tr> <td> <font size="3"color="red" > The username or password you entered is incorrect. [?] </font> </td> </tr> </table> <strong><% }catch(Exception ex) { System.out.println("EXCeption\t"+ex); } finally { if(conef.con!=null) { conef.rs.close(); conef.stmt.close(); conef.con.close(); } else { System.out.println("connection not closed in login.jsp"); } } %><br/></strong><br/><table border="2" bordercolor="#5B77CC" bgcolor="white" width="338" height="100" align="right" cellspacing="5" cellpadding="1"> <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 </strong></td> </tr> <tr align="center" height="200" > <td > <form method="post" name="form1" onsubmit="displayusername(); displaypassword()"><strong> Username: <input type="text" name="username" ><br> (e.g. [email]fr2rhy@yahoo.com[/email])<br> Password: <input type="password" name="password" maxlength="32"> <br> <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"> <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> Use 6 to 32 characters, no spaces, . </span></span><br> <input type="submit" value="submit"> <input type="reset" name="reset" value=" reset "><br></strong> </form> </td> </tr> <tr > <td><form action="registration.jsp" method = "Post"><strong> 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>>"></strong> </form> </td> </tr> </table> </body> </html>
"content.jsp"
jsp Syntax (Toggle Plain Text)
<%@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" %> <html> <head> <title>Content Page</title> <script language="JavaScript" type="text/JavaScript"> </script> </head> <body bgcolor="#C3D9FF" link="#0000FF"> <marquee> <font color="blue" size="30"</font> </marquee><hr> <% Calendar cale = Calendar.getInstance(); %> <table align="right" border="0"> <tr> <td align = "right"> <%= "Date:"+cale.get(Calendar.DATE)+"/"+cale.get(Calendar.MONTH)+"/"+cale.get(Calendar.YEAR)%><br/> </td> </tr> </table> <% session.setAttribute("username", request.getParameter("username")); %> <strong> Welcome : <%=session.getAttribute("username") %></strong> <% try { connectionFile conef=new connectionFile(); String query = "Select username from login" ; conef.coonnectionfile(query); int count=0; conef.stmt.close(); conef.con.close(); } catch(Exception e) { System.out.println("EXCEPTION"+e); } %> <center> <h1><strong>Inventory Management System</strong></h1><strong><br></strong> </center> <div id="Layer1" style="position:absolute; width:794px; height:115px; z-index:1; left: 217px; top: 178px;" > <table width="748" border="0"> <tr> <td width="641"> <strong><font size="+1">Inventory is a list for goods and materials held in stock by a business. </font></strong></td> <td width="97" ><strong><a href="http://prashantp:8080/inventoryManagment/view.jsp"> view</a></strong> </td> </tr> <tr> <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> <td><strong><a href="http://prashantp:8080/inventoryManagment/add.jsp">add</a> </strong></td> </tr> <tr> <td> <strong><font size="+1">Inventory is a necessary part of doing business </font></strong></td> <td><strong><a href="http://prashantp:8080/inventoryManagment/update.jsp">update</a> </strong></td> </tr> <tr> <td> <strong><font size="+1">The functional factors explain the existence of inventory. </font></strong></td> <td><strong><a href="http://prashantp:8080/inventoryManagment/delete.jsp">delete</a> </strong></td> </tr> <tr> <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> <td><strong><a href="http://prashantp:8080/inventoryManagment/content.jsp">cancel</a> </strong></td> </tr> </table> </div> </body> </html>
"view.jsp"
jsp Syntax (Toggle Plain Text)
<%@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" %> <html> <head> <title>Content Page</title> <script language="JavaScript" type="text/JavaScript" ></script> </head> <body bgcolor="#C3D9FF"> <marquee> <font color="blue" size="30"></font> </marquee><hr><hr> <%connectionFile cone = new connectionFile(); %> <% Calendar cale = Calendar.getInstance(); %> <table align="right" border="0"> <tr> <td align="right"> <%= "Date:"+cale.get(Calendar.DATE)+"/"+cale.get(Calendar.MONTH)+"/"+cale.get(Calendar.YEAR)%><br/> </td> </tr> </table> Welcome : <%=session.getAttribute("username")%><br/> <a href="content.jsp">Home</a> <center> <h1>Inventory Management System</h1> <table border="1" cellspacing="15" bgcolor="white"> <tr> <th>productId</th> <th> itemid <br></th> <th> name <br></th> <th>Type</th> <th> companyname <br></th> <th> dateOfComingStock</th> <th> quantity <br></th> <th>weight</th> <th> costPrice <br></th> <th> sellingPrice <br></th> <th>manufacturingDate</th> <th>expiryDate</th> <th>color</th> </tr> </table> </center> <% String que="Select * from itemdetail"; cone.coonnectionfile(que); while(cone.rs.next()) { String productId = cone.rs.getString("productId"); String itemid = cone.rs.getString("itemid"); String name = cone.rs.getString("name"); String Type = cone.rs.getString("Type"); String companyname = cone.rs.getString("companyname"); String dateOfComingStock = cone.rs.getString("dateOfComingStock"); String quantity = cone.rs.getString("quantity"); String weight = cone.rs.getString("weight"); String costPrice = cone.rs.getString("costPrice"); String sellingPrice = cone.rs.getString("sellingPrice"); String manufacturingDate = cone.rs.getString("manufacturingDate"); String expiryDate = cone.rs.getString("expiryDate"); String color = cone.rs.getString("color"); %> <center> <table border="1" width="25" height="20" cellspacing="10" cellpadding="5" bgcolor="white"> <tr> <th> <%=productId%> </th> <th> <%=itemid%> </th> <th> <%=name%> </th> <th> <%=Type%> </th> <th> <%=companyname%> </th> <th> <%=dateOfComingStock %> </th> <th> <%=quantity %> </th> <th> <%=weight%> </th> <th> <%=costPrice %> </th> <th> <%=sellingPrice %><br/> </th> <th> <%=manufacturingDate%> </th> <th> <%=expiryDate%> </th> <th> <%=color%> </th> </tr> </table> </center> <% } %> </body> </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]
When the user logs in take the username and put it the session:
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:
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.
JSP Syntax (Toggle Plain Text)
String username = request.getParameter("username"); String password = request.getParameter("password"); boolean b = checkLogin(username, password); if (b) { // user valid request.getSession.setAttribute("username", username); } else { // Error: invalid username, password }
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:
JSP Syntax (Toggle Plain Text)
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
•
•
Join Date: Aug 2009
Posts: 18
Reputation:
Solved Threads: 0
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.
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.
When you go back to the DemoSession.jsp this code is executed:
But this:
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:
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:
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.
JSP Syntax (Toggle Plain Text)
String name = request.getParameter( "username" ); 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: JSP Syntax (Toggle Plain Text)
<a href= "DemoSession.jsp?username=<%= session.getAttribute( "theName" ) %>">home</a>
Usually I don't get such a problem because I don't put these things together:
JSP Syntax (Toggle Plain Text)
String name = request.getParameter( "username" ); 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:
JSP Syntax (Toggle Plain Text)
String name = request.getParameter( "username" ); String sessName = session.getAttribute( "theName" ); //check whether one of the above is null and act accordingly
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
•
•
Join Date: Nov 2009
Posts: 1
Reputation:
Solved Threads: 0
0
#10 19 Days Ago
I have similar prob. The get attribute is null while the id is not null.
for my java:
Do i need to also use session.setAttribute(abc) in the jsp?
for my java:
java Syntax (Toggle Plain Text)
if(session.getAttribute(abc)==null) System.out.println(abc is null else System.out.println(abc is not null) if(session.getId()==null) System.out.println(id is null) else 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)
![]() |
Similar Threads
- oracle session in jsp pages (JSP)
- Session Lost on JSP Page (JSP)
- ASP.NET - session objects Problem (ASP.NET)
- session handling problem (JSP)
- timeout session problem (JSP)
- JSP Problem Student database search (Java)
- tomcat jsp problem (JSP)
- [urgent] JSP problem (JSP)
- Losing session variable problem...help! (ASP)
Other Threads in the JSP Forum
- Previous Thread: Problem in running JSP
- Next Thread: URL security in jsp how?
| Thread Tools | Search this Thread |
apache backbutton combobox connection database development directorystructure dynamicpagetitles eclipse frames glassfish ie8 imagetodatabse imageupload integer internet java javaee javascript jsf jsp jsppagetitles levels mvc2 mvcmodel2 network parameters passing ping printinserverinsteadofclient redirect request.getparameter response servlet servletdopost()readxml sessions software ssl state_saving_method stocks sun tomcat tutorial update video web






