How To Assign Value To Session Using Javascript - javascript

Work on Asp.Net C# VS08
I would like to assign a value to Session using javascript.
Is it possible?
example: Session["Id"] = document.getElementById("id").value;

The session is a server-side concept; Javascript has no conception of it.
You can make an AJAX service that sets a session value.
However, you should probably use a cookie instead.
You can set cookies in Javascript using this library, among others.

No it is not possible to do that directly with JavaScript on the client-side.
Sessions are handled on the server-side. In general your browser would simply store a cookie which uniquely identifies a session stored on your server. When you change the value of a session variable, you are storing this on the server, and the browser cookie would be unaffected.
You could however use JavaScript to send an XMLHttpRequest to your ASP.NET application, which in turn changes the session variable on the server-side. Since this involves a round-trip to the server, you would have to wait for the response, to confirm that the operation succeeded.

Assigning to a session variable like this isn't possible in JavaScript. Best you can do is write data to a cookie.
http://www.w3schools.com/js/js_cookies.asp
Otherwise if you want to communicate to the server use Ajax.

No it is not possible. Session is server side object. A couple of ways to do that would be to send cookies from JavaScript and use them on the server side to assign a value to a session variable, or to make an AJAX call and set the values on the backend.

Related

Integer variable common for all sessions in node.js

How to have an integer variable common for all client sessions in node.js and access them in client html? (that is, I need a integer variable which can be displayed in the html page and can be updated in html page. The new updated value should be displayed for other clients.) I knew this is a silly question. But I am sorry. I am totally new to node.js and I do not have much time. I need a solution at the earliest. Thank you.
There are multiple options for getting the common variable into a web page:
You can embed an actual <script> tag with the variable in every page that is served from the server. This technique would be easiest if using a template engine so that a common sub-template can be included in every page.
You can have each page send an ajax call to the server when it first loads to fetch the value of the variable.
You can have each page create a webSocket connection to the server when it first loads and then when the server receives the connection, it can send back the value of the variable.
When the client wants to update the variable, it has a couple options for informing the server:
If using the webSocket connection above, it can just send a message to the server that the variable has been uddated and send the new value. The server can then broadcast to the other webSocket-connected clients what the new value is.
If using the either of the other two options above, then whenever the client wants to change the value, it can issue an ajax call to the server to update the value. Other than the webSocket option above, the only way for the other clients to know when the value of the variable changes is for each client to regularly poll the server asking to fetch the current value of the variable (say every 30 seconds or so). This is not particularly efficient.
Because part of your problem is for all connected clients to know when the variable has change, you need the ability for the server to "push" an updated value to all clients. This is what webSockets were invented for and would likely be the recommended option.

with sessionStore and webSockets, are cookies still needed?

if the following conditions are met:
all pages are static (eg, templates to be filled in via websocket data)
all pages are public
session id and status communicated through websocket
client session state stored via sessionStorage and/or localStorage
is there still a need for cookies?
The localStorage/sessionStore can indeed replace cookie Storage. Both are on the client.
The neat thing about cookies is that they are auto appended to any HTTP request. There is absolutely nothing to do from a coding standpoint. But since you want to use websockets, it doesn't apply - you will still need to do wiring with the sessionid stored in the localStorage.
So the answer to your question is "No" you don't need cookies in your scenario
If the pages are 100% static then there is no state, so the question becomes moot, since no mechanism at all is required for preserving state across requests.
However, if any part of the pages are dynamic then cookies may still be necessary for preserving state across multiple sessions. Since cookies are stored client side but passed to the server with every request they are a mechanism for synchronizing client and server state. Of course, you could implement this via an AJAX request and localStorage yourself if you wanted to.

Javascript: Read out session id from cookie

for websockets I have to expose my sessionid from the cookie.
I have searched a bit and found that I should be able to access cookies by:
console.log(document.cookie);
unfortunatly this doesn't work or better document.cookie contains an empty string even chrome itself shows me the cookie also authentication works.
Can it be that chrome hides the cookie for javascript?
That can happen if the server is configured to send the session cookie with the HttpOnly flag. This way the cookie becomes invisible/inaccessible to client side scripting languages like JS.
To achieve your concrete functional requirement, either reconfigure the server to not do so, or look for alternate means, e.g. setting a custom cookie (without the HttpOnly flag, of course), or letting the server side view technology dynamically print the current session ID as a JS variable or as an attribute of some HTML element so that JS can access it by traversing the HTML DOM.

Insert the value of a JavaScript function to an SQL Server database

Is it possible to add a JavaScript variable in an SQL Server database database (run an SQL query or stored procedure)? If so, how?
With JavaScript only, no you cannot (unless you're using Node.js of course).
The point is, you'll need some server-side code to interact with your database. You can use JavaScript in the browser to make an Ajax call to a server-side script (PHP, Ruby, Python, ASP.NET, Node.js, etc.) that performs the interaction with the database.
You can not access the database from JavaScript. You need to send your data to PHP, Ruby on Rails or ASP.NET or whatever you are using to implement the back end.
You can use Ajax to post to a web service of some sort (or any other server side script) to pass the JavaScript value. The web service would access the database and save it...
Set value of variable in the hidden field and get value of that field at server side when submit form and save it into the database....
You can easily access a database from JavaScript in Node.js.
If you're doing it from the browser, you'll most definitely need some kind of mid-tier service which translates HTTP queries to the relevant database driver (you can write that in any language you want - PHP, JavaScript, ASP.NET, etc.).

Access of Cookies on server side

I am designing a web application front end I am making use of HTML and at back-end I am using a servlet.At front end side I am storing some values in cookies that are to be used on the server side (i.e in servlet)
Now my question is : How do I get the values of cookies in servlets.
Please help me with this.
HttpServletRequest has method getCookies(), it will return you array of cookies, so just look for your cookie. I assume you already know how to set cookie using servelt response.
Cookies are sent with every request to the web app, and should thus be used very rarely. You should probably use request (get/post) parameters, and store values that must persist for a whole user session in the HttpSession object.
Now, to answer your specific question: everything coming from the client browser is stored in the HttpServletRequest object. Browse the javadoc for this class (part of the Java Enterprise Edition - Java EE javadoc), and you'll find a method named getCookies(). Follow the links, and you'll find how to extract the cookies you're interested in, and get their value.

Categories

Resources