• Post Reply Bookmark Topic Watch Topic
  • New Topic

session management

 
zalak thakkar
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone,

I found difficulty in session management in jsp for login and logout page...Can any one please help me..? And i am trying to retrieve attribute using getAttribute() method but it returns me null in welcome page..Please help me..

Thank you in advance.. :)
 
sankar manoharan
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please post your code.
 
zalak thakkar
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


login.jsp




<%--
Document : login
Created on : 19 Oct, 2013, 3:32:58 AM
Author : Smart
--%>

<%@page contentType="text/html" pageEncoding="UTF-8" session="true"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Login Page</title>

<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Expires" content="-1">
<meta http-equiv="CACHE-CONTROL" content="NO-CACHE">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">

<link rel="stylesheet" href="login_css/style.css">
<script language="javascript">



function Validation()
{
var email =document.getElementById("login").value;
var pass = document.getElementById("password").value;
var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
if(email=="")
{
alert("Please Enter Email ID");
document.frm.login.focus();
return false;
}

else if(reg.test(email)== false)
{
alert("Please Enter Valid Email Address");
document.frm.password.focus();

}
if(email=="zalak@yahoo.com" || email=="p@gmail.com" && pass=="zkt")
{
//alert("logged in");
alert(window.location = "welcome.jsp");

}
else
{
alert("please enter proper id");
}

}

</script>




</head>
<body>



<form method="post" name="form" action="" class="login" onsubmit="return Validation();">
<center>
<h1 class="color">  CHAT UNLIMITED..!! </h1>
 
 
 
 
 
</center>
<p>
<label for="login">Email:</label>
<input type="text" name="login" id="login" placeholder="name@example.com">

</p>

<p>
<label for="password">Password:</label>
<input type="password" name="password" id="password" placeholder="">
</p>

<p class="login-submit">

<button type="submit" class="login-button" style="background-image: url(login_images/Lan.jpg)">LOGIN </button>



</p>


<label class="NewUser"></label><a href="register.jsp">     if your not a user</a>
       
<label class="Forgot password"></label><a href="forgetpasswrd.html">forgot password.?</a>



</form>

<marquee behaviour="slide" scrollamount="25" direction="left">   <img src="login_images/la_1.png"/><img src="login_images/la1_1.png"/><img src="login_images/Lan1.png"/></marquee>
</body>
</html>
 
zalak thakkar
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome.jsp




<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<%@page contentType="text/html" pageEncoding="UTF-8" session="true"%>
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Expires" content="-1">
<meta http-equiv="cache-control" content="no-cache">
<%--<%

response.addHeader("Cache-Control", "no-cache,no-store,private,must-revalidate,max-stale=0,post-check=0,pre-check=0");
response.addHeader("Pragma", "no-cache");
response.addDateHeader ("Expires", 0);
%>--%>
</head>
<body style="background:url(login_images/bg-headliner.gif) ">
<form method="post" action="">
<center>
<h1>



<%
String str = request.getParameter("login");

session.setAttribute("email2", str);


%>
Welcome, <%= session.getAttribute("email2") %>




</h1>
Click here to Visit <a href="home.jsp">HOME</a><br>Click here to Logout<a href="logout.jsp"><b>Logout</b></a>
</center>
</form>
</body>
</html>
 
zalak thakkar
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
logout.jsp



<%--
Document : logout
Created on : 1 Nov, 2013, 6:43:53 PM
Author : ASUS
--%>

<%@page contentType="text/html" pageEncoding="UTF-8" session="false"%>
<!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">
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Expires" content="-1">
<meta http-equiv="cache-control" content="no-cache">
<title>JSP Page</title>
</head>
<body>
<h1>
<%-- <%

response.addHeader("Cache-Control", "no-cache,no-store,private,must-revalidate,max-stale=0,post-check=0,pre-check=0");
response.addHeader("Pragma", "no-cache");
response.addDateHeader ("Expires", 0);
%>--%>
<%
session.removeAttribute("email2");
request.getSession().invalidate();
out.println("<h1 align='center'>"+"Logout Sucessfully..."+"</h1>");
out.println("<div style='float:left;font-size:25px;'>"+"click here to "
+"<a href='login.jsp'>login</a></div>");
//response.sendRedirect("login.jsp");


%>
</h1>
</body>
</html>
 
zalak thakkar
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And when i clicked on logout, it will successfully logged out me..but when i click on back button in browser it shows me previous pages..However i have added filter but i don't know why it shows me like that..


my Filter is as follow..


/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/


import java.io.IOException;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;

/**
*
* @author ASUS
*/
public class filter1 implements Filter {

private static final boolean debug = true;

// The filter configuration object we are associated with. If
// this value is null, this filter instance is not currently
// configured.
private FilterConfig filterConfig = null;

public filter1() {
}

private void doBeforeProcessing(ServletRequest request, ServletResponse response)
throws IOException, ServletException {
if (debug) log("filter1:DoBeforeProcessing");

// Write code here to process the request and/or response before
// the rest of the filter chain is invoked.

// For example, a logging filter might log items on the request object,
// such as the parameters.
/*
for (Enumeration en = request.getParameterNames(); en.hasMoreElements(); ) {
String name = (String)en.nextElement();
String values[] = request.getParameterValues(name);
int n = values.length;
StringBuffer buf = new StringBuffer();
buf.append(name);
buf.append("=");
for(int i=0; i < n; i++) {
buf.append(values[i]);
if (i < n-1)
buf.append(",");
}
log(buf.toString());
}
*/
}

private void doAfterProcessing(ServletRequest request, ServletResponse response)
throws IOException, ServletException {
if (debug) log("filter1:DoAfterProcessing");

// Write code here to process the request and/or response after
// the rest of the filter chain is invoked.

// For example, a logging filter might log the attributes on the
// request object after the request has been processed.
/*
for (Enumeration en = request.getAttributeNames(); en.hasMoreElements(); ) {
String name = (String)en.nextElement();
Object value = request.getAttribute(name);
log("attribute: " + name + "=" + value.toString());

}
*/

// For example, a filter might append something to the response.
/*
PrintWriter respOut = new PrintWriter(response.getWriter());
respOut.println("<P><B>This has been appended by an intrusive filter.</B>");
*/
}

/**
*
* @param request The servlet request we are processing
* @param response The servlet response we are creating
* @param chain The filter chain we are processing
*
* @exception IOException if an input/output error occurs
* @exception ServletException if a servlet error occurs
*/
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain)
throws IOException, ServletException {



HttpServletResponse hsr = (HttpServletResponse) response;
hsr.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1.
hsr.setHeader("Pragma", "no-cache"); // HTTP 1.0.
hsr.setDateHeader("Expires", 0); // Proxies.
chain.doFilter(request, response);

}

/**
* Return the filter configuration object for this filter.
*/
public FilterConfig getFilterConfig() {
return (this.filterConfig);
}

/**
* Set the filter configuration object for this filter.
*
* @param filterConfig The filter configuration object
*/
public void setFilterConfig(FilterConfig filterConfig) {
this.filterConfig = filterConfig;
}

/**
* Destroy method for this filter
*/
public void destroy() {
}

/**
* Init method for this filter
*/
public void init(FilterConfig filterConfig) {
this.filterConfig = filterConfig;
if (filterConfig != null) {
if (debug) {
log("filter1:Initializing filter");
}
}
}

/**
* Return a String representation of this object.
*/
@Override
public String toString() {
if (filterConfig == null) return ("filter1()");
StringBuffer sb = new StringBuffer("filter1(");
sb.append(filterConfig);
sb.append(")");
return (sb.toString());
}

private void sendProcessingError(Throwable t, ServletResponse response) {
String stackTrace = getStackTrace(t);

if(stackTrace != null && !stackTrace.equals("")) {
try {
response.setContentType("text/html");
PrintStream ps = new PrintStream(response.getOutputStream());
PrintWriter pw = new PrintWriter(ps);
pw.print("<html>\n<head>\n<title>Error</title>\n</head>\n<body>\n"); //NOI18N

// PENDING! Localize this for next official release
pw.print("<h1>The resource did not process correctly</h1>\n<pre>\n");
pw.print(stackTrace);
pw.print("</pre></body>\n</html>"); //NOI18N
pw.close();
ps.close();
response.getOutputStream().close();
}
catch(Exception ex) {}
}
else {
try {
PrintStream ps = new PrintStream(response.getOutputStream());
t.printStackTrace(ps);
ps.close();
response.getOutputStream().close();
}
catch(Exception ex) {}
}
}

public static String getStackTrace(Throwable t) {
String stackTrace = null;
try {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
t.printStackTrace(pw);
pw.close();
sw.close();
stackTrace = sw.getBuffer().toString();
}
catch(Exception ex) {}
return stackTrace;
}

public void log(String msg) {
filterConfig.getServletContext().log(msg);
}

}
 
zalak thakkar
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And when i clicked on logout, it will successfully logged out me..but when i click on back button in browser it shows me previous pages..However i have added filter but i don't know why it shows me like that..


my Filter is as follow..


/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/


import java.io.IOException;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;

/**
*
* @author ASUS
*/
public class filter1 implements Filter {

private static final boolean debug = true;

// The filter configuration object we are associated with. If
// this value is null, this filter instance is not currently
// configured.
private FilterConfig filterConfig = null;

public filter1() {
}

private void doBeforeProcessing(ServletRequest request, ServletResponse response)
throws IOException, ServletException {
if (debug) log("filter1:DoBeforeProcessing");

// Write code here to process the request and/or response before
// the rest of the filter chain is invoked.

// For example, a logging filter might log items on the request object,
// such as the parameters.
/*
for (Enumeration en = request.getParameterNames(); en.hasMoreElements(); ) {
String name = (String)en.nextElement();
String values[] = request.getParameterValues(name);
int n = values.length;
StringBuffer buf = new StringBuffer();
buf.append(name);
buf.append("=");
for(int i=0; i < n; i++) {
buf.append(values[i]);
if (i < n-1)
buf.append(",");
}
log(buf.toString());
}
*/
}

private void doAfterProcessing(ServletRequest request, ServletResponse response)
throws IOException, ServletException {
if (debug) log("filter1:DoAfterProcessing");

// Write code here to process the request and/or response after
// the rest of the filter chain is invoked.

// For example, a logging filter might log the attributes on the
// request object after the request has been processed.
/*
for (Enumeration en = request.getAttributeNames(); en.hasMoreElements(); ) {
String name = (String)en.nextElement();
Object value = request.getAttribute(name);
log("attribute: " + name + "=" + value.toString());

}
*/

// For example, a filter might append something to the response.
/*
PrintWriter respOut = new PrintWriter(response.getWriter());
respOut.println("<P><B>This has been appended by an intrusive filter.</B>");
*/
}

/**
*
* @param request The servlet request we are processing
* @param response The servlet response we are creating
* @param chain The filter chain we are processing
*
* @exception IOException if an input/output error occurs
* @exception ServletException if a servlet error occurs
*/
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain)
throws IOException, ServletException {



HttpServletResponse hsr = (HttpServletResponse) response;
hsr.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1.
hsr.setHeader("Pragma", "no-cache"); // HTTP 1.0.
hsr.setDateHeader("Expires", 0); // Proxies.
chain.doFilter(request, response);

}

/**
* Return the filter configuration object for this filter.
*/
public FilterConfig getFilterConfig() {
return (this.filterConfig);
}

/**
* Set the filter configuration object for this filter.
*
* @param filterConfig The filter configuration object
*/
public void setFilterConfig(FilterConfig filterConfig) {
this.filterConfig = filterConfig;
}

/**
* Destroy method for this filter
*/
public void destroy() {
}

/**
* Init method for this filter
*/
public void init(FilterConfig filterConfig) {
this.filterConfig = filterConfig;
if (filterConfig != null) {
if (debug) {
log("filter1:Initializing filter");
}
}
}

/**
* Return a String representation of this object.
*/
@Override
public String toString() {
if (filterConfig == null) return ("filter1()");
StringBuffer sb = new StringBuffer("filter1(");
sb.append(filterConfig);
sb.append(")");
return (sb.toString());
}

private void sendProcessingError(Throwable t, ServletResponse response) {
String stackTrace = getStackTrace(t);

if(stackTrace != null && !stackTrace.equals("")) {
try {
response.setContentType("text/html");
PrintStream ps = new PrintStream(response.getOutputStream());
PrintWriter pw = new PrintWriter(ps);
pw.print("<html>\n<head>\n<title>Error</title>\n</head>\n<body>\n"); //NOI18N

// PENDING! Localize this for next official release
pw.print("<h1>The resource did not process correctly</h1>\n<pre>\n");
pw.print(stackTrace);
pw.print("</pre></body>\n</html>"); //NOI18N
pw.close();
ps.close();
response.getOutputStream().close();
}
catch(Exception ex) {}
}
else {
try {
PrintStream ps = new PrintStream(response.getOutputStream());
t.printStackTrace(ps);
ps.close();
response.getOutputStream().close();
}
catch(Exception ex) {}
}
}

public static String getStackTrace(Throwable t) {
String stackTrace = null;
try {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
t.printStackTrace(pw);
pw.close();
sw.close();
stackTrace = sw.getBuffer().toString();
}
catch(Exception ex) {}
return stackTrace;
}

public void log(String msg) {
filterConfig.getServletContext().log(msg);
}

}
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!