answersLogoWhite

0


Best Answer

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
This answer is:
User Avatar
More answers
User Avatar

AnswerBot

βˆ™ 6mo ago

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.

This answer is:
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.

Related questions

Syntax when using image tag to an HTML document?

The basic syntax is:Additional optional attributes include height, width, and border.


Where does the name syntax error come from?

syntax means synthesis that attributes comma,dash,slash and every keyword..however the deepest answer for evaluating this question is synthesis error.


Php Source code for validation?

If you wish to check the syntax of your source code, you need to run the code through the PHP CLI with the -l option. There are also some online resources to validate the syntax. This will check only the syntax of your file. This will not however detect runtime errors or logic errors. You must absolutely run the script for the best possible code validation.


What does HTML contain?

By contain I assume you mean the Syntax (Words), it is made up of tags such as and closing tags such as , also attributes such as to learn more about this visit http://w3schools.com/


Software for opening DXL ext files?

.dxl files are script files used for customising Telelogic DOORS. they can be edited through notepad or any other text editor. It is suggested though that using a syntax editor such as Crimson Edit or Xemacs with a DXL syntax file applied is better, then you can get syntax highlighting for key words, etc which makes debugging much simpler


What should I do when scope not digest in angular java script?

Apply $scope.$apply(); It's better to have apply in timeout syntax as below : SetTimeout(function() {$scope.$apply();},10);


How do you prevent syntax in programming?

Not possible; syntax cannot be avoided. (Syntax errors can be though.)


Are HTML tags and syntax the same?

No. Syntax is/are the rules of the language, tags are part of the syntax.


Is database design language a programming language?

No, it doesn't comprise any binary or computer language syntax. It simply uses schematic to graphic representation of tables structures and attributes as a mechanism to ease to the design process.


How syntax affect setting?

Syntax is essential to any form of language. Without syntax, there is no setting, there is no way to explain it and no language. Language and setting are both comprised with syntax.


How does syntax affect setting?

Syntax is essential to any form of language. Without syntax, there is no setting, there is no way to explain it and no language. Language and setting are both comprised with syntax.


What are some antonyms of syntax?

There are no antonyms of syntax. It's a noun.