Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

JavaWorld Daily Brew

javax.servlet.ServletException: Exception creating bean of class



Hi,

 

I got the following error while running
sample struts application. I would appreciate any
help.

struts version= 1.1

 

error:

========

javax.servlet.ServletException: Exception creating bean of class struts1.form.TestActionForm: {1}

                org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:848)

                org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:781)

                org.apache.jsp.test.test_jsp._jspService(org.apache.jsp.test.test_jsp:86)

                org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)

                javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

                org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:322)

                org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:291)

                org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241)

                javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

 

root cause

----------------------------------------------------------------------

test.jsp

===========

<%@ page contentType="text/html; charset=utf-8"%>

<%@ taglib uri="/WEB-INF/struts-bean.tld"
prefix="bean" %>

<%@ taglib uri="/WEB-INF/struts-html.tld"
prefix="html" %>

<%@ taglib uri="/WEB-INF/struts-logic.tld"
prefix="logic" %>

<html:html>

<head>

                <title>test</title>

</head>

<body >

                <table
align="center" >

                                <tr>

                                                <td>

                                                                <html:form action="test"
method="POST">

                                                                                <strong><font size="4">User Name:
</font> </strong>

                                                                                <html:text property="userName" />

                                                                                <br>

                                                                                <strong><font size="4">Password:
</font> </strong>

                                                                                <html:text
property="password" />

                                                                                <br>

                                                                                <html:submit
property="submit" value="Save" />

                                                                                <html:submit
property="submit" value="Delete" />

                                                                                <html:reset value="Reset"
/>

                                                                </html:form>                                                   

                                                </td>

                                </tr>

                </table>

</body>

</html:html>

---------------------------------------------------------------------

struts-confif.xml

===================

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts-config PUBLIC "-//Apache
Software Foundation//DTD Struts Configuration 1.1//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">

<struts-config>

                <data-sources>

                </data-sources>

                <form-beans>

                                <form-bean
name="testActionForm"
type="struts1.form.TestActionForm" />

                </form-beans>

                <global-exceptions
/>

                <global-forwards
/>

                <action-mappings>

                                <action
attribute="TestActionForm"
input="/test/test.jsp"

                                                name="testActionForm"
parameter="do" path="/test" scope="request"

                                                type="struts1.action.TestAction">

                                                <forward
name="success" path="../index.jsp" />

                                                <forward
name="failure" path="/test/test.jsp"
/>

                                </action>           

                </action-mappings>

                <message-resources
parameter="struts1.ApplicationResources" />

</struts-config>

------------------------------------------------------------------------

Action Class:

==============

package struts1.action;

import struts1.form.TestActionForm;

import org.apache.struts.action.*;

import javax.servlet.http.*;

public class TestAction extends Action {

                public ActionForward execute(ActionMapping mapping, ActionForm
form,

                                                HttpServletRequest request, HttpServletResponse
response) {                

                                TestActionForm form1 = (TestActionForm)form;                              

                                if (request.getParameter("submit").equalsIgnoreCase("Save"))

                                                form1.save();

                                if (request.getParameter("submit").equalsIgnoreCase("Delete"))

                                                form1.delete();                               

                   if (request.getAttribute("err") == null){                                                

                                  request.setAttribute("userName",
form1.getUserName());

                                  request.setAttribute("password", form1.getPassword());

                                  return mapping.findForward("success");

                     }              

                      else

                     return mapping.findForward(request.getAttribute("err").toString());

                 }            

}

-----------------------------------------------------------------------

Action Form Class:

====================

package struts1.form;

import java.sql.Connection;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

import java.util.Properties;

import com.mysql.jdbc.ReplicationDriver;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;

import org.apache.struts.action.ActionForm;

import org.apache.struts.action.ActionMapping;

 

public class TestActionForm extends ActionForm {

private static final long serialVersionUID =
-7296021341331004928L;

                private String userName;

                private String password;

                private String result;

                private Connection con;

                private String sql ="";

                private String sql2 ="";

                private Statement st;

                private ResultSet rs;

 

                public TestActionForm () {
super(); }

                public String getPassword() {

                                return password;

                }

                public void setPassword(String
password) {

                                this.password = password;

                }             

                public String getUserName() {

                                return userName;

                }

                public void setUserName(String userName) {

                                this.userName = userName;

                }

               

public ActionErrors validate(ActionMapping
actionMapping, HttpServletRequest
request) {        

                userName = request.getParameter("userName");

                password = request.getParameter("password");

               

                if (userName == null || password
== null )

                                request.setAttribute("err", "roleErr");                  

                if ((result == "You have entered an incorrect username
or password")||

                                (result == "you could not assign user to role that are
not available"))

                                request.setAttribute("err", "userRoleErr");

                return null;

                      }

 

public String save(){       

                sql = "insert into test (userName, password) values ("+

                  "'"+this.getUserName()+

                  "', '"+this.getPassword()+"'
);";

                Properties
props = new Properties();

               

                try {

                    ReplicationDriver
driver = new ReplicationDriver();

                                props.put("autoReconnect", "true");

                                props.put("roundRobinLoadBalance", "true");

                                props.put("failOverReadOnly", "false");

                                props.put("user",
"root");

                                props.put("password",
"");

 

                                Class.forName("com.mysql.jdbc.Driver").newInstance();

                                con =driver.connect("jdbc:mysql://localhost,localhost/project",

       
                props);

                                con.setReadOnly(false);

       
con.createStatement().executeUpdate(sql);                

                    } catch(Exception
e) {

                      System.err.println("Exception: " + e.getMessage());

                    } finally {

                      try {

                        if(con !=
null)

                          con.close();

                      } catch(SQLException e) {}

                    }

                return sql;

}

public void delete(){

                sql = "delete from user where
" +

                  "roleName='"+this.getUserName()+"';";

                Properties
props = new Properties();

                try {

                    ReplicationDriver
driver = new ReplicationDriver();

                                props.put("autoReconnect", "true");

                                props.put("roundRobinLoadBalance", "true");

                                props.put("failOverReadOnly", "false");

                                props.put("user",
"root");

                                props.put("password",
"");

 

                                Class.forName("com.mysql.jdbc.Driver").newInstance();

                                con =driver.connect("jdbc:mysql://localhost,localhost/project",

       
                props);

                                con.setReadOnly(false);

 

       
con.createStatement().executeUpdate(sql);                

                    } catch(Exception
e) {

                      System.err.println("Exception: " + e.getMessage());

                    } finally {

                      try {

                        if(con !=
null)

                          con.close();

                      } catch(SQLException e) {}

                    }

}

}

--------------------------------------------------------------

web.xml

===========

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee  
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

 
<servlet>

   
<servlet-name>action</servlet-name>

   
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>

   
<init-param>

     
<param-name>config</param-name>

     
<param-value>/WEB-INF/struts-config.xml</param-value>

   
</init-param>

   
<init-param>

     
<param-name>debug</param-name>

     
<param-value>3</param-value>

   
</init-param>

   
<init-param>

     
<param-name>detail</param-name>

     
<param-value>3</param-value>

   
</init-param>

   
<load-on-startup>0</load-on-startup>

 
</servlet>

 
<servlet-mapping>

   
<servlet-name>action</servlet-name>

   
<url-pattern>*.do</url-pattern>

 
</servlet-mapping>

 
<welcome-file-list>

   
<welcome-file>index.jsp</welcome-file>

 
</welcome-file-list>

</web-app>