Share the Session value in JSP and Asp.net

Source: Internet
Author: User

Specifically, the Session in the Web refers to the time that the user spends browsing a website from entering the website to closing the browser. Therefore, we can see from the above definition that Session is actually a specific concept of time.

What is the Session value between JSP and Asp.net?

The idea at the beginning of this topic is to serialize the Session in Asp.net to save binary data to the database, and then read the binary data from the database by JSP to deserialize the Session object, forced conversion into a Java Session object. An error occurs during JAVA-side conversion, and the online data cannot be retrieved. Therefore, a replacement method is used.

Ideas for the replacement method:

Log on to the ASPX file. After Successful Logon, save the variable values in the Session to a table in the database. Use the Session ID of the Session object of Asp.net, create An ASPX file, and obtain

Get the SessionID of the currently logged-on user and use the Asp.net redirect statement to go to the JSP file. The URL request path format is test. jsp? Aspnetsessionid = ffj12d455p0ujr45vdqwhh45, if

Asp.net is not logged on or fails to log on. Although there is a SessionID value, there is no data associated with this SessionID in the database.

Some readers may find that test. jsp is not required? Aspnetsessionid = ffj12d455p0ujr45vdqwhh45 and other request paths can also be completed. Yes, you can use test. jsp? Userid = 1111

The value is also passed. Of course, userid is the value obtained after the Asp.net login is successful, but some users can know the sensitive data of USERID (User ID.

Create a table

◆ Table name:

◆ Iis_session

◆ Field name:

◆ Id varchar (26) -- store the SessionID of Asp.net

◆ Userid int (4) -- stores the user ID after Successful Logon

◆ Power int (4) -- store the user's permission number

Source code snippet of the Session value in the Asp.net program:

 
 
  1. /* After successful logon, you can place the following CODEING In the logon authentication ASPX page */
  2.  
  3. // Record the Session value to the database
  4. Private void WriteSession2DB (string sessionID, string sUID, string sPWR)
  5. {
  6. // Connect to the database code, which is automatically added by the reader
  7. StringSessID=SessionID;
  8. StringStrSQL="Insert into iis_session (id, userid, power) values (@ seionID, @ UID, @ PWR )";
  9.  
  10. // Webmod. sqlConn is the connection object of the database. you can replace it with your own database connection.
  11. SqlCommandSqlCmd=NewSqlCommand (strSQL, webmod. sqlConn );
  12. SqlCmd. Parameters. Add ("@ seionID", SqlDbType. VarChar). Value=SessID;
  13. SqlCmd. Parameters. Add ("@ UID", SqlDbType. Int). Value=Convert. ToInt32 (sUID. Trim ());
  14. SqlCmd. Parameters. Add ("@ PWR", SqlDbType. Int). Value=Convert. ToInt32 (sPWR. Trim ());
  15. SqlCmd. ExecuteNonQuery ();
  16. // Closes the database connection, and the reader adds
  17.  
  18. }
  19.  
  20. /* When you exit the system, you can delete a row of data from the database's SessionID on the exit page or the Session_END process of Global. asax */
  21.  
  22. // Delete the Session value in the database
  23. Private void RemoveSession4DB ()
  24. {
  25. // Connect to the database code, which is automatically added by the reader
  26. StringSessID=Session. SessionID;
  27. StringStrSQL="Delete from iis_session where id = '"+ SessID + "'";
  28.  
  29. // Webmod. sqlConn is the connection object of the database. you can replace it with your own database connection.
  30. SqlCommandSqlCmd=NewSqlCommand (strSQL, webmod. sqlConn );
  31. SqlCmd. ExecuteNonQuery ();
  32. // Closes the database connection, and the reader adds
  33. }
  34.  
  35. /* Redirect to the ASPX page of JSP, and add the following code in PAGE_LOAD of this ASPX page */
  36. Private void Page_Load (object sender, System. EventArgs e)
  37. {
  38. StringStrSessionID= Session. SessionID. Trim ();
  39. StringStrRoot="Http: // localhost/test. jsp? Aspnetsessionid ="+ StrSessionID;
  40. Response. Redirect (strRoot, true );
  41. }
  42.  

The source code snippet of the Session value in the JSP program:

 
 
  1. <% @ PageContentType="Text/html; charset = gb2312"%> 
  2. <%
  3. /*
  4. You can replace your own database connection classes.
  5. */
  6. %> 
  7. <Jsp: useBean Id="Db" Scope="Page" Class="Com. itbaby. bean. dbx. database"/> 
  8.  
  9. <%
  10.  
  11. StringSASPNetSessionID=Request. GetParameter ("aspnetsessionid ");
  12.  
  13. // The connection pool is used to connect to the database. you can replace it with your own
  14. StringSDBSourceName="Itbaby_dbpool";
  15. Db. dbConnOpen (sDBSourceName );
  16.  
  17. StringSSql="Select userid, power from iis_session where id = '"+ SASPNetSessionID + "'";
  18.  
  19. // The Reader replaces the code used to read the result set.
  20. Java. SQL. ResultSetRs=Db. GetRs (sSql );
  21.  
  22. If (rs. next ())
  23. {
  24.  
  25. StringSUID=Rs. GetString (1 );
  26. StringSPower=Rs. GetString (2 );
  27.  
  28. /* Read the corresponding SESSIONID value from the database and display that if the SESSION of Asp.net times out, there will be no value */
  29. Out. print ("<H1>Asp.net Session ValueUserID="+ SUID +" H1><Br><Br>");
  30. Out. print ("<H1>Asp.net Session ValuePower="+ SPower +" H1><Br><Br>");
  31.  
  32. }
  33. Rs. close ();
  34.  
  35. Db. dbConnClose ();
  36.  
  37. %> 
  38.  

So far, the sharing of Session values in JSP and Asp.net has come to an end. Although it is not a good method, it can also be used to protect users' sensitive data.

I will continue to consider using serialization and deserialization to share Session objects between different Web languages, instead of sharing Session values.

  1. JSP2.0 features of basic JSP tutorial knowledge
  2. JSP tutorial-traffic count JSP source code
  3. JSP entry-level website environment setup steps
  4. Servlet import event-driven technology in JSP development
  5. How to export Oracle Data Tables Using JSP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.