-1

I want to store session value in String object in jsp using struts2.

FILE : header.jsp

<%
name = (String)session.getAttribute("name");    
/* name = (String)request.getSession().getAttribute("name"); */
if(name.equals("cmp")
{  // show something   }
else if(name.equals("emp")
{  // show something   }
else
{  // show something   }
%>

When i try to get session value using this syntax i get error : HTTP Status 500 - java.lang.NullPointerException

FILE : loginAction.java

public class LoginAction extends ActionSupport implements SessionAware
{
    private Map<String, Object> session;
    private String name;
    //setters and getters
    public String execute()
    {
        session.put("name", name);
        return SUCCESS;
    }
}

I set name value based on the user enter name in login page.

Based on the user type i change my header view.

So suggest me how can i get value from the session in jsp and then store into String object.

Parth Patel
  • 799
  • 1
  • 13
  • 20
  • 1
    Do NOT use Scriptlets!!! – Andrea Ligios Mar 29 '17 at 12:21
  • code are in scriplets. – Parth Patel Mar 29 '17 at 12:22
  • 1
    Do NOT use Scriptlets!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! – Andrea Ligios Mar 29 '17 at 13:17
  • This is trivially expressed using standard JSP EL or conditionals; why not try a more modern approach first. – Dave Newton Mar 29 '17 at 16:11
  • See https://struts.apache.org/docs/ognl.html. – Aleksandr M Mar 29 '17 at 16:14
  • @RomanC i don't want to print value of object from the session but i want to get session value in String object. So first of all understand my concern and then make it **duplicate**. – Parth Patel Mar 30 '17 at 05:05
  • @AndreaLigios if i do not use scriplets then how can i get session value in String object. using `` i only print session value but can not get into an object. My need is to store into String object and then check that if value is matched then show something. – Parth Patel Mar 30 '17 at 05:17
  • @AndreaLigios for condition checking but in first how can i retrieve value of session into String object. – Parth Patel Mar 30 '17 at 07:05
  • 1
    This is the [perfect example of the **XY PROBLEM**](https://meta.stackexchange.com/a/66378/214186). **You don't need any `String` in the JSP**. There is no such concept of `String` in a JSP. FORGET STRINGS IN JSPs. You need values, not Strings. Strings in JSPs are like light-years in a calendar. Stop your brain, restart it, then *start thinking **differently***. You need to print ? ``; you need to check ? ``; you need to send ? ``, ``, etc. REMOVE the `<% %>` from your coding dictionary. DO NOT USE SCRIPTLETS. – Andrea Ligios Mar 30 '17 at 07:39
  • @ParthPatel And what are you saying? – Roman C Mar 30 '17 at 08:31
  • @AndreaLigios thank you so much. :) – Parth Patel Mar 30 '17 at 09:10
  • 1
    @ParthPatel you're welcome. Please, stop trying adding me to Skype :D I've two jobs and a family, answering on StackOverflow is enough as free help at the moment :) – Andrea Ligios Mar 30 '17 at 09:35

1 Answers1

2

FILE : header.jsp

<s:if test="#session.role=='cmp'">
    // show something
<s:if>
<s:elseif test="#session.role=='emp'">
    // show something
<s:elseif>
<s:else>
    // show something
<s:else>
Parth Patel
  • 799
  • 1
  • 13
  • 20