A session is a concept used by servers and clients to maintain a connection state. Usually, if the login is successful, it is connected to the client as a session on the server side. At this time, member information is shared through the client's web browser connecting the session. Unlike cookies, the session does not store member information on the client's computer. Using a session, you don't have to take your membership information with you as you move the page. It is easier and safer to use than cookies.

 

Here are some basics about sessions. 

To set session

session.setAttribute ( java.lang.String name, java.lang.Object value);

To use session

session.getAttribute ( java.lang.String  name)

To set the expired time

session.setMaxInactiveInterval();

To delete session

session.removeAttribute ( java.lang.String  name );

To delete all current sessions

session.invalidate();

 

Let's see the examples of session objects and compare them to the cookies.

The examples of cookies are here: 2022.08.28 - [JSP] - JSP) Cookies

 

JSP) Cookies

Cookies are small blocks of data created by a web server while a user is browsing a website and placed on the user's computer or other devices by the user's web browser. Cookies are placed on the de..

www.agilemeadow.com

To set session

<%@ page contentType="text/html; charset=euc-kr" %>

<html>
	<head><title>Session Example</title>
	</head>
	<body>

<%
	String id = "guardian23";
	String passwd = "1234";

	session.setAttribute("id", id);
	session.setAttribute("passwd", passwd);
%>
   The id and passwd property in the session are set.<br><br>

	<input type="button" value="Check properties in the session" onclick="javascript:window.location='viewSession.jsp'">
	</body>
</html>

To view session

You will see the id and the password with the enumeration type. This example is for when you don't know about the session name.

<%@ page contentType="text/html; charset=euc-kr" %>
<%@ page import="java.util.*" %>

<html>
	<head><title>Session Example</title></head>
	<body>

<%
	Enumeration attr = session.getAttributeNames();

	while(	attr.hasMoreElements()	){
		String attrName = (String)attr.nextElement();
		String attrValue = (String)session.getAttribute(attrName);
		out.println("The name of the session is " + attrName + ", and  ");
		out.println("the value of the session is " + attrValue + ".<br><br>");
	}
%>

	</body>
</html>

If you know the session name, you will use it this way.

<%@ page contentType="text/html; charset=euc-kr" %>
<%@ page import="java.util.*" %>

<html>
	<head><title>Session Example</title></head>
	<body>

<%
String id = (String)session.getAttribute("id"); //Downcast to String class
String passwd = (String)session.getAttribute("passwd");
%>

ID : <%=id %> <br>
Password : <%=passwd %> <br>


	</body>
</html>

The URL of the session is shared with the same browser. 

 

When you log out or delete your account, you need to delete the session on the browser. 

To close session

<%@ page contentType="text/html; charset=euc-kr"%>

<%
	session.invalidate();
%>

<html>
<head>
<title>Closing Session</title>
</head>
<body>
	<script>
		alert("Log out");
		location.href = "../session/viewSession.jsp";
	</script>
</body>
</html>

After deleting the session

Now, let's make a log in program with session. 

sessionLoginForm.jsp

<%@ page contentType = "text/html; charset=euc-kr" %>

<html>
	<head><title>Log in</title></head>
	<body>

	<form action="<%= request.getContextPath() %>/sessionLogin.jsp" method="post">
		ID <input type="text" name="id" size="10">
		PASSWORD <input type="password" name="password" size="10">
			<input type="submit" value="Log in">
	</form>

	</body>
</html>

sessionLoginCheck.jsp

<%@ page contentType = "text/html; charset=euc-kr" %>
<%
    String memberId = (String)session.getAttribute("MEMBERID");

	//Ternary Operator 
	// (Statement) ? a : b
	// if the statement is true -> a 
    // if the statement is false -> b

    boolean login = (memberId == null) ? false : true;
%>

<html>
	<head><title>Log in Check</title></head>
	<body>

<%
    if (login) {
%>
		Hello, "<%= memberId %>"!
		
		<a href="sessionLogout.jsp">Log out</a><br>
<%
    } else {
%>
		<a href="sessionLoginForm.jsp">Log in</a><br>
		
		<a href="../../request/member/memberform.html">Sign up</a> <br>
<%
    }
%>

	</body>
</html>

sessionLogin.jsp

<%@ page contentType = "text/html; charset=euc-kr" %>

<%
    String id = request.getParameter("id");
    String password = request.getParameter("password");
    
    if (id.equals(password)) {
        session.setAttribute("MEMBERID", id);
%>

		<html>
			<head><title>Success</title></head>
			<body>
			<script>
				alert("Log in success.")
				location.href="sessionLoginCheck.jsp"
			</script>

			</body>
		</html>

<%
    } else { // log in failed
%>

		<script>
			alert("Failed to log in.");
	//		history.go(-1);
			history.back();
		</script>
<%
    }
%>

Once you link the log in to the log in form and the signup to the sign up form, it will load to the page.

sessionLogout.jsp

<%@ page contentType = "text/html; charset=euc-kr" %>

<%
    session.invalidate();
%>

<html>
	<head><title>Log out</title></head>
	<body>
	<script>
		alert("Log out");
		location.hret="sessionLoginForm.jsp"
	</script>
	</body>
</html>

If you want to know more about the sign up program, please refer to this post of mine :

2022.08.24 - [Codes & Projects] - jQuery/ JSP) Sign up Form

 

jQuery/ JSP) Sign up Form

Sign up Form member_join.jsp <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!DOCTYPE html PU..

www.agilemeadow.com

 

'Java > JSP' 카테고리의 다른 글

JSP) Action tags - forward  (0) 2022.08.30
JSP) Scopes - Page / Request Scopes  (0) 2022.08.29
JSP) Cookies  (0) 2022.08.28
JSP) Encoding Korean 한글 인코딩  (0) 2022.08.27
JSP) Implicit Objects 3 - Out Object  (0) 2022.08.27

+ Recent posts