javavalidationjspstruts2struts-config

Session in struts2 application not working


I have tried many examples and referred many questions on Stack Overflow but still not succeeded in setting up logout action for user.

I am getting null pointer exception while setting logout action.

I also I want that when back button is pressed it should not redirect to logged in page of user.
Here is my

LoginAction.java:

package org.entity;

import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.dispatcher.SessionMap;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport implements SessionAware
{
    List<UpdateStock> ustocklist;

    Login login;
    private SessionMap<String,Object> sessionMap;  

    public SessionMap<String, Object> getSessionMap() {
        return sessionMap;
    }
    public void setSessionMap(SessionMap<String, Object> sessionMap) {
        this.sessionMap = sessionMap;
    }
    public LoginAction() {
        // TODO Auto-generated constructor stub
    }
    public Login getLogin() {
        return login;
    }
    public void setLogin(Login login) {
        this.login = login;
    }
    
    public List<UpdateStock> getUstocklist() {
        return ustocklist;
    }
    public void setUstocklist(List<UpdateStock> ustocklist) {
        this.ustocklist = ustocklist;
    }
    public String execute() throws Exception {
        
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/foryou", "root", "siddheshkk");
            System.out.println("Driver Loaded");
            Statement st = con.createStatement();
            PreparedStatement ps = con
                    .prepareStatement("select * from bbinfo where code=? and password=?");
            ps.setString(1,login.getCode());
            ps.setString(2,login.getPassword());
            PreparedStatement ps1 = con
                    .prepareStatement("select * from stockinfo where code=?");
            ps1.setString(1, login.getCode());
            ResultSet rs1 = ps1.executeQuery();
            System.out.println("Data loaded 2");
            ustocklist = new ArrayList<UpdateStock>();
            while (rs1.next()) {
                ustocklist.add(new UpdateStock(rs1.getString(7), rs1
                        .getString(8), rs1.getString(9), rs1.getString(10), rs1
                        .getString(11), rs1.getString(12), rs1.getString(13), rs1
                        .getString(14)));
            }
            
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
                sessionMap.put("login","true");  
                sessionMap.put("name",login.getCode());  
                
                
                return "success";
                
            } else {
                return "error";
            }   
            
        } catch (Exception e) {
            System.out.println(e);
        }
        HttpSession session=ServletActionContext.getRequest().getSession(false);  
        if(session==null || session.getAttribute("login")==null){  
            return "login";  
        }  
        else{  
            return "success";  
        }       
        
        //return "success";
    }
    
    public void validate() {  
        if(login.getCode().trim().length()<1 || login.getPassword().trim().length()<1)
        {  
            addFieldError("login.code", "Please enter valid code"); 
            addFieldError("login.password", "Please enter valid Password");
        }  
}
    @Override
    public void setSession(Map<String, Object> map) {
        // TODO Auto-generated method stub
        sessionMap=(SessionMap)map; 
    }

    public String logout(){  
        if(sessionMap!=null){  
            sessionMap.invalidate();  
            sessionMap.remove("login");
            sessionMap.clear();
           return "success";
           
         }
        return "success";  
    }
    
    }   

Below is my action tag in struts.xml page:

<action name="logout" class="org.entity.LoginAction" method="logout">
            <result name="input">UpdateStock.jsp</result>
            <result name="success">BloodBankSection.jsp</result>
        </action>  

And here is my JSP after successfully logged in page

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!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=ISO-8859-1">
<title>Welcome! Update Stock</title>

<style type="text/css">
body {
    background-color: lightyellow;
    font-family: sans-serif;
    font: italic;
}


h3 {
    color: yellow;
    background-color: olive;
}
h2{
    color:red;
    text-align:center;
}
#header
{
background-color:black;
height:80px;
width:100%;

}
#navArea{
height:100px;
float:left;
}
#nav
{
list-style:none;
margin-top:30px;
}
#nav a
{
    color:white;
    text-decoration:none;
}
#nav li
{
    float:left;
    
    background-color:#252525;
    padding:3px;
    
    border-radius:5px;
    margin:8px;
}
#nav li:hover{
    background-color:red;
}

</style>
</head>
<body>
<div id ="header">
            <div id="navArea">
    <ul id="nav">
    <li><a href="welcome.jsp">Home</a></li>
    <li><a href="Admin.jsp">Admin Panel</a></li>
    <li><a href="Feedback.jsp">Feedback</a></li>
    <li><a href="aboutus.jsp">About Us</a></li>
    <li><a href="donorbloodsection.jsp">Donor Section</a></li>
    <li><a href="BloodUpdateProfile.jsp">Edit Profile</a></li>
    <li><a href="logout">Logout</a></li></ul></div></div>
    
    <div align="center">
    
    Welcome, <s:property value="#session.name"/>   
    <h2>Please Enter the Stock Details </h2>
        <h3>Previous Stock</h3>
        <table border="2">
            <th>
            <tr>
                <td>A+</td>
                <td>A-</td>
                <td>B+</td>
                <td>B-</td>
                <td>AB+</td>
                <td>AB-</td>
                <td>O+</td>
                <td>O-</td>
            </tr>
            </th>
            <s:iterator value="ustocklist">
                <s:iterator>
                    <tr>
                        <td><s:property value="opositive" /></td>
                        <td><s:property value="onegative" /></td>
                        <td><s:property value="apositive" /></td>
                        <td><s:property value="anegative" /></td>
                        <td><s:property value="bpositive" /></td>
                        <td><s:property value="bnegative" /></td>
                        <td><s:property value="abpositive" /></td>
                        <td><s:property value="abnegative" /></td>
                    </tr>
                </s:iterator>
            </s:iterator>
        </table>
        <h3>Update Stock here</h3>
        <s:form action="UpdateStockAction">
            <s:textfield label="Enter your registered code " name="us.code" />
            <s:textfield label="O+ " name="us.opositive" />
            <s:textfield label="O- " name="us.onegative" />
            <s:textfield label="A+ " name="us.apositive" />
            <s:textfield label="A- " name="us.anegative" />
            <s:textfield label="B+ " name="us.bpositive" />
            <s:textfield label="B- " name="us.bnegative" />
            <s:textfield label="AB+ " name="us.abpositive" />
            <s:textfield label="AB- " name="us.abnegative" />
            <s:submit value="Update" />
        </s:form>
    </div>
</body>
</html>

Here I am posting error image as well.

error


Solution

  • Try the code below

    @SkipValidation
    public String logout(){  
     ...    
    }
    

    And i also i want that when back button is pressed it should not redirect to logged in page of user.

    <action name="logout" class="org.entity.LoginAction" method="logout">
      <result type="redirect">/</result>
    </action>