ZK - Open Source Ajax Java FrameworkZK - Open Source Ajax Java Framework

How to get Jsp session value in zk java page

aks
18 Jan 2012 06:18:42 GMT
18 Jan 2012 06:18:42 GMT

Hello,
can anybody tell me that how to access jsp session value in zk java page...
Is it possible or not.. I have set value to the session in jsp page using session.setAttribute("str",propStr);
n i m trying to access it in zk java page by creating session object as
HttpSession session= request.getSession(true);
bt here i m getting NULLPOINTEREXCEPTION...
Please help me out..
Is it right way to do the same or provide me with right solution for the same...

Thank you in advance..

Kailas
18 Jan 2012 06:27:04 GMT
18 Jan 2012 06:27:04 GMT

Try this code

Executions.getCurrent().getSession().getNativeSession();
               //or   Sessions.getSession.getNativeSession();


Read getNativeSession()

aks
20 Jan 2012 11:43:41 GMT
20 Jan 2012 11:43:41 GMT

Hi,
the given solution in not working..

I want to access jsp session (HttpSession) object in zk java page..
I have written code in java file as follows

HttpSession s =(HttpSession)Sessions.getCurrent().getNativeSession();

but I m getting NULLPOINTEREXCEPTION at this line....
Is it the right way to get jsp session in zk java page..
Please reply me with proper solution if any..

Thanks in advance

aks
23 Jan 2012 06:15:56 GMT
23 Jan 2012 06:15:56 GMT

is there any solution???????????

ashishd
1 Feb 2012 09:09:37 GMT
1 Feb 2012 09:09:37 GMT

Hi aks,
what is "zk java page"? is it a composer or an extended component? Please post your complete source code to help us understand where in your code you are trying to access the session from ZK.

aks
1 Feb 2012 09:46:53 GMT
1 Feb 2012 09:46:53 GMT

it is a composer...

This is MS.java (using GenericForwardComposer) file...

public void onClick$btn1(){
				Iframe iframe = new Iframe();
				mb.setImageName(rs.getString("image"));
				String dwfImage = mb.getImageName();
				
				System.out.println("file name............."+dwfImage);
				session.setAttribute("imgDWFPath", dwfImage);
				System.out.println("file name......GET......."+session.getAttribute("imgDWFPath"));
				
				//acceptFile.detach();
				iframe.setWidth("100%");
				iframe.setHeight("100%");
				iframe.setSrc("/jsp/dwfJSP.jsp");
				iframe.setParent(divIframe);

}



Now here is jsp file to set dwf image--
dwfJSP.jsp
	<script language="javascript"  type="text/javascript" for="ADViewer" event="OnUpdateUiItem(type, state, data)">
				
             if (type == "OBJECTPROPERTIES")
             {
            	var xmlHttpAjax;
            	objProps = data;
                if ( objProps )
                {
                    //propStr = ObjectProperties.innerHTML;
                    propStr = "";
                    nObjs = objProps.Count;
                    for (i=1; i <= nObjs; i++ )
                    {
                        prop = objProps.Item(i);
                        if ( prop )
                        {
                            propStr += prop.Name + ": " + prop.Value + "     ";
                        }
                    }
                    //ObjectProperties.innerHTML = propStr + "<p>";
                    
					//alert("PropStr : "+propStr);
					
					xmlHttpAjax = new ActiveXObject("Microsoft.XMLHTTP");  
			    	var url="/cat/jsp/ajaxDWFJsp.jsp"+"?propStr="+propStr;
					xmlHttpAjax.open("GET",url,true);	
					xmlHttpAjax.setRequestHeader('content-type','application/x-www-form-urlencoded');
					xmlHttpAjax.send(null);    				
                }
             }
        </script>
<div id="test">
					<object id="ADViewer" classid="clsid:A662DA7E-CCB7-4743-B71A-D817F6D575DF"
							codebase="http://www.autodesk.com/global/dwfviewer/installer/DwfViewerSetup.cab#version=7,0,0,928"
							width="100%" height="700" > 
							<param name="Src" value="/cat/img/small.dwf"/>
						</object>
						<%String str = (String) session.getAttribute("imgDWFPath");
						System.out.println("attr : "+str);
						%>			
						<script type="text/javascript" language="javascript">
							url="/cat/DwfFile/"+'<%= str %>';
							ADViewer.SourcePath=url;							
				        </script>
            		</div>




from above jsp file my contrl goes to next ajaxDwfjsp.jsp page
here in this jsp file i m setting propStr to session which is jsp session..
code of this file is as follows-

ajaxDwfJsp.jsp
<%
	String para = request.getParameter("propStr");	
	session.setAttribute("propString", para);%>



now again from MS.java file which is composer
I am trying to acces that jsp session on Thread i have created there in MS.java file.

MS.java code is as follows----
public void run()
	{
HttpSession s=(HttpSession)Sessions.getCurrent().getNativeSession();
}


but I m getting not getting session here...
I m getting Null pointerexception at this point..

Please guide me for the same..
Thanks in advance

ashishd
2 Feb 2012 08:37:33 GMT
2 Feb 2012 08:37:33 GMT

From Sessions.getCurrent() javadoc
"Returns the session for the current thread, or null if not available. It is the same as getSession(true).
Note: it returns null if it is called in a working thread."

maybe you could retrieve Session data before starting the thread?

Kailas
6 Feb 2012 12:45:46 GMT
6 Feb 2012 12:45:46 GMT

Hi Aks

This is sample code for you.

public class ZulJspViewCtrl extends GenericForwardComposer {
	
	private Include jspinclude;
	private Button btnFirst;
	
	private Desktop _desktop;
	
	/**
	 *
	 */
	@Override
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		_desktop = Executions.getCurrent().getDesktop();
	}
	
	public void onClick$btnFirst() {
		// TODO: please check if you have use "self" or zscript functions here.
		jspinclude.setSrc("/jsp/jspzul.jsp");
		try {
			CometServerPush.start(btnFirst);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

class CometServerPush {
	
	public static void start(Component btnFirst) throws InterruptedException {
		final Desktop desktop = Executions.getCurrent().getDesktop();
		
		if (desktop.isServerPushEnabled()) {
			Messagebox.show("Already started");
		} else {
			desktop.enableServerPush(true);
			new WorkingThread(btnFirst).start();
		}
	}
	
	public static void stop() throws InterruptedException {
		final Desktop desktop = Executions.getCurrent().getDesktop();
		if (desktop.isServerPushEnabled()) {
			desktop.enableServerPush(false);
		} else {
			Messagebox.show("Already stopped");
		}
	}
	
	private static class WorkingThread extends Thread {
		
		private final Desktop _desktop;
		// private final Component _info;
		private final Button _btnFirst;
		
		private WorkingThread(Component btnFirst) {
			_desktop = btnFirst.getDesktop();
			_btnFirst = (Button) btnFirst;
		}
		
		public void run() {
			try {
				while (true) {
					if (_btnFirst.getDesktop() == null
							|| !_desktop.isServerPushEnabled()) {
						
						_desktop.enableServerPush(false);
						return;
					}
					Executions.activate(_desktop);
					try {
						process();
					} finally {
						Executions.deactivate(_desktop);
					}
					Threads.sleep(1000);
				}
			} catch (DesktopUnavailableException ex) {
				System.out.println("The server push thread interrupted");
			} catch (InterruptedException e) {
				System.out.println("The server push thread interrupted");
			}
		}
		
		public void process() {
			
			try {
				HttpSession hses = (HttpSession) _desktop.getSession().getNativeSession();
				System.out.println("HttpSession.........."+hses.getAttribute("propString"));
				
			} catch(NullPointerException e) {
				System.out.println("Null error.........."+e);
			}
		}
		
	}
}

And jsp file

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
	<script type="text/javascript">
		function btnFirstCall() {
                       <%
				session.setAttribute("propString", "First Click");
			%>
		}

		function btnSecondCall() {
                       <%
				session.setAttribute("propString", "Second Click");
			%>
		}

	</script>

</head>
<body>
	<h1> JSP to ZUL </h1>
	<form>
		<input type="button" value="First" name="btnFirst" onClick="btnFirstCall();">
		<input type="button" value="Second" name="btnSecond" onClick="btnSecondCall();">
	</form>
</body>
</html>

This sample code is helpfull for you. If you use Ajax call then modify JavaScript function like this and write one more jsp ajax page

JavaScript function

<script type="text/javascript">
		function btnFirstCall() {

			var xmlHttpAjax;
			var propStr = "First Button Click";
			
			if (window.XMLHttpRequest)
			{
				// code for IE7+, Firefox, Chrome, Opera, Safari
				xmlHttpAjax = new XMLHttpRequest();
			}
			else if (window.ActiveXObject)
			{
				// code for IE6, IE5
				xmlHttpAjax = new ActiveXObject("Microsoft.XMLHTTP");
			}
			else
			{
				alert("Your browser does not support XMLHTTP!");
			}			
  
			var url="/zkjsp/jsp/ajaxDWFJsp.jsp"+"?propStr="+propStr;
			xmlHttpAjax.open("GET",url,true);	
			xmlHttpAjax.setRequestHeader('content-type','application/x-www-form-urlencoded');
			xmlHttpAjax.send(null);
		}

		function btnSecondCall() {

			var xmlHttpAjax;
			var propStr = "Second Button Click";


			if (window.XMLHttpRequest)
			{
				// code for IE7+, Firefox, Chrome, Opera, Safari
				xmlHttpAjax = new XMLHttpRequest();
			}
			else if (window.ActiveXObject)
			{
				// code for IE6, IE5
				xmlHttpAjax = new ActiveXObject("Microsoft.XMLHTTP");
			}
			else
			{
				alert("Your browser does not support XMLHTTP!");
			}
			  
			var url="/zkjsp/jsp/ajaxDWFJsp.jsp"+"?propStr="+propStr;
			xmlHttpAjax.open("GET",url,true);	
			xmlHttpAjax.setRequestHeader('content-type','application/x-www-form-urlencoded');
			xmlHttpAjax.send(null);
		}

	</script>

and ajax page : - ajaxDWFJsp.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
	String para = request.getParameter("propStr");	
	session.setAttribute("propString", para);
%>

This is help full for you.

Kailas
8 Feb 2012 12:04:48 GMT
8 Feb 2012 12:04:48 GMT

Hi Aks,

The Second JavaScript code work for Separate Ajax Page for setting Session value.