Re: Session Handling In JSP
Session management can be achieved by using the following things:
- Cookies
- URL rewriting
- Hidden form fields
Re: Session Handling In JSP
Most of the time if you consider then you will come to know that the session is work with the user base application. If user login and login gets successful then the session object is used to set the session. It is set for the maximum time of the session. It is provided by the web server or it is defined by the application. The session object gets destroyed when the session is closed by the user or it expires.
Re: Session Handling In JSP
You can simply make use of the code of the Session Handling in JSP from below:
Code:
<html>
<head>
<title>Session Jsp</title>
</head>
<body>
<form method = "post" action = "session.jsp">
<font size = 6>Name<input type = "text" name = "str"></font><br><br>
<font size = 6>Password<input type="password" name = "pwd" >
</font><br><br>
<input type = "submit" name = "submit" value = "submit" >
</form>
</body>
</html>
<%
String str = request.getParameter("str");
String pwd = request.getParameter("pwd");
if(str.equals("Williams") && pwd.equals("abcde"))
{
session.setAttribute("userstr",str);
response.sendRedirect("NextPageAfterFirst.jsp");
}
else
{
response.sendRedirect("SessionManagement.html");
}
%>
<html>
<head>
<title>Welcome in In the program of URL rewriting</title>
</head>
<body>
<font size = 6>Hello</font> <%= session.getAttribute("userstr") %>
</body>
</html>
Re: Session Handling In JSP
You can able to Remove JSP Session Variables with the help of below step:
When the session in not needed then you can simply remove that forcefully. For that you need to make use of the method below:
Code:
session.invalidate();
You can simply check for the new session by the help of following method:
Re: Session Handling In JSP
Use the code below for session in jsp:
Code:
<%@ page language="java" import="java.util.*"%>
<%
String str=request.getParameter("sessionVariable");
session.setAttribute("MySession",str);
%>
<html>
<head>
<title>Cookie IN JSP</title>
</head>
<body>
Session : <%=(String)session.getAttribute("MySession")%>
</body>
</html>