Access to session values in jsp is briefly described

  • 2020-11-26 18:46:47
  • OfStack

We will manipulate some form values in jsp. Or get the user's value operation, then we can use the scope operation of jsp, 1.page, request, session, application four scope, the most commonly used is the request and session domain operation.

Use the session domain operation, because the web container used is the Tomcat server, and as long as session does not close the browser, it will not disappear, or is the default time limit of 30 minutes, then use session in jsp,


<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form method=POST action="Session.jsp">

Please enter user name:


<input type=text name="name"> <input type=submit
value=" Submit information ">
</form>
<!-- session Set the value  -->
<%
String name = request.getParameter("name");


session.setAttribute("name", name);
String names = (String) session.getAttribute("name");
%>

 Your username is :<%=names%>
</body>
</html>

Now you have the value of name, and you can call back the session

JSTL tag get Session:

session.setAttribute("age","123");

${ sessionScope.age} That's 123 on the page

sessionScope refers to the range of session, similar to requestScope, pageScope,contextScope
Then age, which follows, represents the key value for the set attribute

Obtain Session from Jsp:

session is the built-in object of jsp, so you can write directly to jsp


<% 
session.setAttribute("a", b); // the b In the session In, let's call it a .  
String M = session.getAttribute( " a " ).toString(); // from session In the a Take it out and assign it to M 
%> 

conclusion

That is the end of this article's brief introduction to accessing session values in jsp, and I hope you found it helpful. Those who are interested can continue to see this site:

Details Struts2 jsp page not logged in to implement the interception function

jsp- Solves the problem of file deletion when Tomcat is restarted after file upload

If there is any deficiency, please let me know. Thank you for your support!


Related articles: