Link to home
Start Free TrialLog in
Avatar of i808237
i808237Flag for United States of America

asked on

Unable to set ServletRequest property of Java bean using JSTL and JAAS

Hello Expert,

I am trying to learn how to use a "JAAS in a JSP" (from Java Servlet an JSP Cookbook page 349) to implement security for my login page.  I followed the 'Java Servlet an JSP Cookbook'  by creating a login.html file that performs a POST  to authenLogin.jsp (sends entered userName and password).  A code snippet from login.html follows:

<form method="POST"  action="http://localhost:8084/Database2/authenLogin.jsp">
<td>Enter the user name: </td><td><input type="text " name="userName" size="15"></td>
<td>Enter the password: </td><td><input type="password " name="password" size="15"></td>

The authenLogin.jsp is properly called that contains the following code:
1. Taglib JSTL is brought into scope <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
2. Next a bean is created called jaasBean  <jsp:useBean id="jaasBean" class="com.jspservletcookbook.LoginBean" />
(confirmed in debug that the LoginBean object constructor is executed properly to create the object)
3. The next statement fails for me when an attempt is made to set the beans ServletRequest property (called req) to the current request using c:set.  It follows:

 <c:set target="${jaasBean}" value="${pageContext.request}" property="${req}"/>

I used '<c:out' tag to print the values in the request using pageContext.request (I could see information like the userName and password entered in the login.html) to confirm the the request is not null.  It seems like the setter method in LoginBean class 'setReq(ServletRequest request)' should get invoked to set the objects' req attribute. (full class is at the bottom of this page)

4. When I run the program, I get the following server error: javax.servlet.ServletException: Invalid property in &lt;set&gt;: (full error is at the bottom of this page).  I assume that the error relates to the fact that the setter method is not getting invoked properly if at all???

I've tried for 2 days to get this to work without success.  Any help would be greatly appreciated!!!

Thanks,
I808237

The full LoginBean class follows:

package com.jspservletcookbook;          
import javax.servlet.ServletRequest;
import javax.security.auth.login.LoginContext;
import javax.security.auth.login.LoginException;

public class LoginBean {
    public ServletRequest req;//pass ServletRequest to WebCallbackHandler to exract
                               //username and password
    boolean loginSuccess;//boolean to indicate whether name and password have
                         //have passed the login test.
   

  public LoginBean(){ }

  public boolean getLoginSuccess() throws LoginException {
   
      if (req == null)
          throw new IllegalStateException(
          "The ServletRequest cannot be null in getLogin()");
         
      WebCallbackHandler webcallback = new WebCallbackHandler(req);
     
     try{
   
          LoginContext lcontext = new LoginContext("WebLogin",webcallback );
         
          lcontext.login();
         
          return true;
   
      } catch (LoginException lge){
   
         return false;
   
      }

   } //getLoginSuccess
     
     public void setReq(ServletRequest request) {
       
         if (request == null)
             throw new IllegalArgumentException(
               "ServletRequest argument was null in: "+getClass().getName());
                    
             this.req = request;
             
      }//setReq
     public ServletRequest getReq() {              
             return this.req;

Full server error follows:

javax.servlet.ServletException: Invalid property in &lt;set&gt;:  ""
      org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:846)
      org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:779)
      org.apache.jsp.authenLogin_jsp._jspService(authenLogin_jsp.java:101)
      org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:99)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
      org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:325)
      org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:295)
      org.apache.jasper.servlet.JspServlet.service(JspServlet.java:245)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
      org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:362)
root cause
javax.servlet.jsp.JspTagException: Invalid property in &lt;set&gt;:  ""
      org.apache.taglibs.standard.tag.common.core.SetSupport.doEndTag(Unknown Source)
      org.apache.jsp.authenLogin_jsp._jspx_meth_c_set_0(authenLogin_jsp.java:120)
      org.apache.jsp.authenLogin_jsp._jspService(authenLogin_jsp.java:83)
      org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:99)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
      org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:325)
      org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:295)
      org.apache.jasper.servlet.JspServlet.service(JspServlet.java:245)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
      org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:362)

ASKER CERTIFIED SOLUTION
Avatar of Asora
Asora
Flag of Thailand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of i808237

ASKER

That solved the issue!  Thanks expert!!!