Testing browsers for a session variable

The Auto redirect snippet in the JSP drawer of the Snippets view tests for the presence of a session variable. If the session variable is not present, the customized code forwards control to a different web page.

The snippet must be placed within the <head> tag of a JSP page after the <pageEncoding> tag.

To insert and configure this snippet:

  1. From the EGL drawer of the Snippets view, drag the Auto redirect snippet to a blank line in the JSP file within the <head> tag of the page. This snippet goes into the code of the JSP file, not the code of the JSF Handler part. For more information, see Inserting code snippets into EGL and JSP files. The Insert Template window opens.
  2. In the Insert Template window, set the SessionAttribute variable to the name of the session variable that is being tested. The default value is UserID. See Storing data in the user's session.
  3. Set the ApplicationName variable to the name of your project or application. The default value is EGLWeb.
  4. Set the PageName variable to the name of the page that the browser will be redirected to if the session variable is absent. The default value is Login.jsp.
  5. When you have customized the values in the Insert Template window, click Insert.
  6. Save the file.
The code inserted by this snippet is as follows:
<%
if ((session.getAttribute("userID") == null ))
  {
   String redirectURL = 
   "http://localhost:9080/EGLWeb/faces/Login.jsp";
   response.sendRedirect(redirectURL);
  }
%>

Feedback