answersLogoWhite

0

Setting and getting session attributes is fairly easy. It is the same in both Servlets and JSPs with one exception. In a JSP, you already have access to the session object, and you do not have to declare it. In a Servlet, you must get the session like this: javax.servlet.http.HttpSession session = request.getSession(); Once you have done that, you can set a session object like this: session.setAttribute("name","value"); To retrieve the value, do this: String foo = (String) session.getAttribute("name"); A couple of things to keep in mind: * The second parameter in the setAttribute method is an Object, not a String. When you retrieve the value, you have to cast it. In the example above, I am casting it to a String. * If you try to perform a getAttribute on a session attribute that does not exist, or was not set, it will return a null. * Session attributes are not available using JavaScript. You can not set or get an attribute in JavaScript. * You do NOT need to do the 'session = request.getSession() in a JSP. It is only necessary in a Servlet.

User Avatar

Wiki User

15y ago

Still curious? Ask our experts.

Chat with our AI personalities

RossRoss
Every question is just a happy little opportunity.
Chat with Ross
ViviVivi
Your ride-or-die bestie who's seen you through every high and low.
Chat with Vivi
RafaRafa
There's no fun in playing it safe. Why not try something a little unhinged?
Chat with Rafa
More answers

In servlets, to set a session attribute, you can use request.getSession().setAttribute("attributeName", attributeValue);. To get a session attribute, you can use request.getSession().getAttribute("attributeName");.

In JavaScript, you can set a session attribute using sessionStorage.setItem("attributeName", attributeValue);. To get a session attribute, you can use sessionStorage.getItem("attributeName");. Note that sessionStorage is limited to the current tab or window, and the data persists until the tab or window is closed.

User Avatar

AnswerBot

10mo ago
User Avatar

Add your answer:

Earn +20 pts
Q: Set and get session attributes syntax in servlets and java script?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Information Science

What is sql-3?

SQL-3 is short for Structured Query Language Level 3, which is a standard that defines the syntax and semantics of SQL queries. It includes features like advanced outer joins, enhanced data manipulation capabilities, and support for recursive queries. SQL-3 builds upon SQL-92 and provides more advanced functionality for managing databases.


What is semi join in SQL?

A semi join in SQL compares two tables and returns rows from the first table where a match is found in the second table, but only the columns from the first table are included in the result. It is commonly used to filter results based on a condition in another table without duplicating data.


Where can one find more information regarding XSL online?

There are multiple online resources where you can find more information regarding XSL. Some popular options include the World Wide Web Consortium (W3C) website, which provides the official XSL specifications and documentation, as well as tutorial websites like W3Schools or Tutorials Point, which offer beginner-friendly explanations and examples of XSL concepts and syntax. Additionally, online forums and communities such as Stack Overflow can be valuable sources for specific questions and discussions related to XSL.


What are the advantages and disadvantages of SQL?

Advantages of SQL include its flexibility to handle diverse data types, its ability to retrieve and manipulate large amounts of data efficiently, and its widespread industry use. Disadvantages may include its complex syntax, the potential for security vulnerabilities if used incorrectly, and the need for a deep understanding of database structures to optimize queries.


What is the use of query by example?

Query by Example (QBE) is a database query language where users can perform searches by providing an example of the data they are looking for. It simplifies the process of constructing queries for users by allowing them to specify criteria in the form of example data entries. This method is user-friendly and does not require knowledge of complex query syntax.