How to use session in Struts
Hello, there:
It's a simple question.
I have a web app which has a login page, in its action class I create an object, user, and save it into session by using request.getSession(true).setAttribute("user", user).
In another action class, I want to use the info of user then I use request.getSession(true).getAttribute("user"); however, the returned object is NULL. Did I use session in a wrong way?
Thanks,
Sway
Did I use session in a wrong way?No, you are using the session correctly. The code looks fine.
Check
- your spelling of the attribute names - obviously they must match
- the ids of the sessions you get both times: session.getId(). If they have different ids, then most probably the session is being lost somewhere.
There are a number of reasons to lose a session. If you close the browser, invalidate the session in code, or lose the cookie recording the id. This happens when you change from https to http, so a session can be lost that way.
The session is normally maintained by session cookies. If you close your browser you lose the cookie. If you have disabled cookies on your machine then it also might not work.
In cases such as that you should be using the method response.encodeURL() to maintain the session for you in any hyperlinks you produce. Struts normally handles that for you though if necessary.
Hope this helps,
evnafets
Similar Messages
-
How to use session in webservice?
In C#, can use session variable in a webservice object.
How to use session in Java webservice?
Who can give me a example?
Thanks a lotDid I use session in a wrong way?No, you are using the session correctly. The code looks fine.
Check
- your spelling of the attribute names - obviously they must match
- the ids of the sessions you get both times: session.getId(). If they have different ids, then most probably the session is being lost somewhere.
There are a number of reasons to lose a session. If you close the browser, invalidate the session in code, or lose the cookie recording the id. This happens when you change from https to http, so a session can be lost that way.
The session is normally maintained by session cookies. If you close your browser you lose the cookie. If you have disabled cookies on your machine then it also might not work.
In cases such as that you should be using the method response.encodeURL() to maintain the session for you in any hyperlinks you produce. Struts normally handles that for you though if necessary.
Hope this helps,
evnafets -
How to use session object in jsp
hi all
marry christmas
can anyone plz tell me how to use session obect in jsp
rachna
Message was edited by:
rachna_arora82hi rachna,
JSP has a default(implicit) session object...... use the getSession(true) method on the session object and then going u can either get or set attributes depending on the requirement
That was in general and now with the issue u have got..... what u can do is that the u can create session for every user who logs in and when he/she tries to login again then u can probably check for the existing session object in the JSP and perform the logic as required..... any clarifications plzzzzzzz let me know
Thanks n Regards
Naveen M
Message was edited by:
Novice_inJAVA
Message was edited by:
Novice_inJAVA -
How to use Session scope in jsp page
Hello, I have login form, where user provides username and password. Then click on submit, it will forward to validation.jsp. Where it will check in database make sure username and password exit. Now i can also retrive accountid of perticular user. I want to put that username and accountid in session scope. so all other pages can use that username and accountid. How can i do that. I'm new at this, so please provide me example too. Here is my code for login.jsp and validation.jsp..
Validation.jsp
<%@ page import="java.util.*" %>
<%@ page import="java.sql.*" %>
//String name = request.getParameter( "username" );//
//session.setAttribute( "accountID",accountID );//
<%
String connURL = "jdbc:oracle:thin:@orca.csc.ncsu.edu:1521:ORCL";
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
conn = DriverManager.getConnection(connURL, "vapatel","pjdas");
stmt = conn.createStatement();
String user=request.getParameter("userName");
String password=request.getParameter("password");
boolean entrance=false;
stmt.excuteQuery("SELECT AccountID From Password WHERE USERNAME='"+user+"'");
rs = stmt.executeQuery("SELECT * FROM Password WHERE USERNAME='"+user+"' AND PASSWORD='"+password+"'");
while(rs.next()){
String dbUser = rs.getString("USERNAME");
String dbPassword= rs.getString("PASSWORD");
if ((user.equals(dbUser)) && (password.equals(dbPassword))){
entrance=true;
if (entrance==true){%>
<jsp:forward page="form.jsp"/>
<%}
else{%>
<jsp:forward page="login.jsp"/>
<%}
%>hi,
to put something into session scope in a jsp page use:
session.setAttribute("counter", Integer.toString(5));to retrieve is from a jsp page use:
String counterAttribute = (String)session.getAttribute("counter"); -
How to use session variable in JSP function & How to use both JSP Servlet
Hi,
I am new to JSP and servlets
Still I am devloping a website in JSP. I am not mixing JSP with servlets, but I do create Java files for bean, logic and database works.
I try to keep the hard coding part out of JSP.
I dont how to use both JSP and Servlets in combination.
Hence If needed I write some functions in JSP.
but it gives me error
+<%! public void abc()+
+{+
int intUserId = Integer.valueOf((Integer) session.getAttribute("MySession_UserID"));
+}+
+%>+
Saying cannot find symbol session
1) So can u please tell how can I access session variables within JSP function
2) And also give me some links/tutorials about useing both JSP and Servlets in combination.
Thanks
VenkatThe application architecture when you use Servlets and JSP in a standard MVC pattern is explained here (under the heading "Integrating Servlets and JSP Pages") and here ...
-
How to use displaytags with struts
How to use display tags with struts to generate report from database
I think it would only consist in including the tag library of displaytags in the header and use the prefix of desplay tags.
Exporting the report from a database would only need to retrieve this information from the database as a List, Map or Collection of objects (beans) and display tem with the corresponding tag (table)
You have some more information here:
http://displaytag.sourceforge.net/11/tut_basic.html
Regards and good luck,
Fran Serrano. -
i am making shopping mall project .
ist page conatins list of product avaiale
2nd page contains list of manufacturuer avaible
problem:-
i want to display on 3rd page the product seleted by user in 1st page
i used session tracking concept.but problem is the value is coming null in third page
please tell me how to solve my problemIf it is like a shopping cart, I suggest you to look for a good shopping cart examples available plenty online.
But if its just about keeping session variables and using them the following works.
Test with a simple example. Have three jsp files like a.jsp, b.jsp and c.jsp.
put the following in a.jsp
<% session.setAttribute("Mobile","Nokia");%>
<%=session.getAttribute("Mobile")%>
<a href="b.jsp">Go to B.jsp</a>Print the value of session variable - <%=session.getAttribute("Mobile")%> in b.jsp and c.jsp
And in b.jsp have a link to c.jsp and so on. Once you set a session variable, it lives as long as your session doesnt expire.
Try it. and also look for more session tracking examples online.
Message was edited by:
passion_for_java -
How to use session cookie property of System object?
Hi all,
I have searched all over the SDN but didnt get anything relevent so here i am posting my query...
My scenario is as follows:
I have created a KM document iview that launches an HTML page, on click of button of HTML page a VC iview is launched. On this iview i have a button that hits BI query.
PS: A system object is created for the connectivity bet portal and backend BI server.
PS: i have configured SSO between portal and backend.
Now when i click on button on iview that fetches the data from backend, i am asked for authentication pop-up, although i have configured SSO why i am asked to enter UID and PWD again??
In system object there is a property named
<b>"session cookie = MYSAPSSO2"</b>
So should i use this property so that cookie will get transfered from one session to other session when i click button on iview??
If yes then HOW??
Is there any other setting remained in Visual Admin?? or Backend or portal?
What could be the missing??
PS: User id are same on portal & backend.
Any help will be highly appreciated...
Regards,
Ameya
Thanks in advance
Message was edited by:
Ameya Pimpalgaonkar
null
Message was edited by:
Ameya PimpalgaonkarHi Ameya,
I do not know the exact answer.However you should look for something called JSESSION ID.
Have a look at the thread:
Re: Problems Using Application Integrator for BSP Application
Reg SSO Logon Tickets and Browser sessions
How to use jsessionid while making HTTP calls??
Hope you find something which can help you.
Regards
Atul Shrivastava -
How to use Session Broker ? (Where to add Session Broker Code)
Hi all,
My requirement is, I have to use two different database session in one Top Link.
I know it can be done using Session Broker, but where actually I need to add that code ? I am using JDeveloper 10.1.2.0.0(Build 1811) as i need to implement this in a BPEL process. Is it possible use Session Broker with this version , if so how to do that ?
Thanks in advance,
Anant.Hello Anant,
Not sure what code you are refering to. Using a SessionBroker is similar to how you would use a regular session, and can be configured using sessions.xml or through java code. If you are using java code, you would do this where ever you first acquire the sessionbroker and login.
Please refer to the docs on what it is and how it is used:
http://download-west.oracle.com/docs/cd/B25221_04/web.1013/b13593/sesun007.htm#CACGDDHE
The docs are for TopLink 10.1.3, and though I think TopLink 10.1.2 (9.0.4.5) is what ships with Jdev 10.1.2, they should still apply.
Best Regards,
Chris -
How to use internationalization in struts
how to use the internationalization in struts with clear example
http://www.google.com/search?hl=en&q=ajax+struts&btnG=Google+Search
-
How to use session state protection
I use Apex 3.2.1
I access my site by a url passing a parameter like this : f?p=101:1:0::::ITEM1:1234567. There is no login and password to access the site.
The value of the parameter ITEM1 is the authorization of the first page, with a database function for the verification.
To secure my site I want to use session state protection so, I enabled it , then I defined "Arguments Must have checksum" for the page 1.
Now , when i try to acces my site with the same url it does not work.
it is the first time a try to use session state protection, could someone tell me what's wrong?Hi user5719906,
I would suggest that as you need to pass an item and are unable to generate a checksum as you are not yet logged in, that you will need to allow arguments without checksum for this page.
This could be a bit of a hole in your security, but as long as you know it is there and clear the cache for all pages that you branch to, you should be able work around it.
The issue is that a malicious user can set page and application items via the url to an unsecured page.
Regards
Michael -
hai
i got answer for this in O'Reilly Jakarta Struts Cook Book....
thanks...I don't think you should use coolies in struts at all. Or for anything at all. I abhor slavery of any kind
-
hai
iI am new to struts. I want to get value in text field from one form to another using cookis in struts, can any one help me?, please...To write a cookie in Struts, you create one and add it to the response object. (response.addCookie(cookie)).
To read it, you use the bean:cookie tag.
As to use cookies to get a given value from one form to another, there might be better ways, like using request attributes.
You might be better off keeping those cookies in the jar, after all. -
Struts-JSP : how to use session variable?
Hi,
i'm new to both struts and JSP and have a doubt here -
I'm creating a web application in which a user logs in.
I need to access this user's userid (id name with which he logs in) in my action classes. how can i do this ?
please help !
Thanks in advance,
susen// set session variable
session.setAttribute("value","hello world");
// retrieve session variable
HttpSession session = request.getSession(false);
String variable = (String) session.getAttribute("value");
variable will have "hello world"
Edited by: abel on May 12, 2008 4:52 AM -
How to use session and cookies in struts
I have login page. If user enters his username and password go to other page. then i copy this url and put the address bar automatically go to this page without enter username and password. So this time i want to send one message "Your session is expired". How can i tell this message page in struts. Please tell me.
I don't want to sound like a jerk (but I often do), but wouldn't it be easier to look for a good struts manual to look this stuff up in?
If each time you don't know something you ask people in this forum to explain it to you then you'll be developing for a LONG time. If you do find a good manual (hint: a book might help a lot), then your lack of struts knowledge isn't really a problem anymore.
Maybe you are looking for
-
Hi, my software just recently started giving me the folowing error and it does not work (start): Sorry, a serious error has occured that requires Adone Premiere Elements to shut down. We will attempt to save your current project. I had tried everythi
-
Reg:table declaration in a top include
Hi gurus, I have declared few tables in the top include of a function module,but while activating the FM it's throwing error that "the table has already been declared".Where as I have not declared those tables anywhere else. Please give me a solution
-
How do i download a pdf file from an e-mail? save to computer did not work.
How do i download a pdf file from an email? Save to computer did not work.
-
My phone was steal.Can you block it with i-mai code?
My iphone was steal. Can you block it with i-mai code? (012752000706947)
-
New user. how to make the screen lighter so I can see it easily
I am new to firefox. An update was done to the computer and now the screen on my laptop is not lighted well enough to see. How can I fix this. I am sure it is simple if you will tell me what to do.