datasource is not working in struts
I am try to get some values from table using datasource in struts but it gives exception :
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
javax.servlet.ServletException: java.lang.NullPointerException
org.apache.struts.action.RequestProcessor.processException(RequestProcessor.java:520)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:427)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:228)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:462)
javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
root cause
java.lang.NullPointerException
login.LoginAction.execute(LoginAction.java:31)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:425)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:228)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:462)
javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
note The full 开发者_如何学运维stack trace of the root cause is available in the Apache Tomcat/6.0.26 logs.
and my struts-config.xml is contains datasources connection property :
<form-beans>
<form-bean name="LoginForm" type="login.LoginForm"/>
<form-bean name="dynaActionForm" type="org.apache.struts.action.DynaActionForm">
<form-property name="fname" type="java.lang.String"/>
<form-property name="lname" type="java.lang.String"/>
</form-bean>
</form-beans>
<data-sources>
<data-source key="dataSource" type="org.apache.tomcat.dbcp.dbcp.BasicDataSource">
<set-property property="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
<set-property property="url" value="jdbc:sqlserver://192.168.1.169:1433;databaseName=localDB"/>
<set-property property="username" value="sa"/>
<set-property property="password" value="pass"/>
</data-source>
</data-sources>
<global-exceptions></global-exceptions>
<global-forwards>
<forward name="welcome" path="/Welcome.do"/>
</global-forwards>
<action-mappings>
<action name="LoginForm" path="/login" scope="request" type="login.LoginAction" validate="false">
<forward name="failure" path="/login.jsp"/>
<forward name="success" path="/WEB-INF/success.jsp"/>
</action>
<action name="dynaActionForm" path="/dyna" scope="request" type="DaynaAction.dynaActionClass" validate="false">
<forward name="filure" path="/index.jsp"/>
<forward name="success" path="/WEB-INF/JSP/SuccessDyna.jsp"/>
</action>
<action path="/Welcome" forward="/welcomeStruts.jsp"/>
</action-mappings>
<controller processorClass="org.apache.struts.tiles.TilesRequestProcessor"/>
<message-resources parameter="com/myapp/struts/ApplicationResource"/>
<!-- ========================= Tiles plugin ===============================-->
<!--
This plugin initialize Tiles definition factory. This later can takes some
parameters explained here after. The plugin first read parameters from
web.xml, thenoverload them with parameters defined here. All parameters
are optional.
The plugin should be declared in each struts-config file.
- definitions-config: (optional)
Specify configuration file names. There can be several comma
separated file names (default: ?? )
- moduleAware: (optional - struts1.1)
Specify if the Tiles definition factory is module aware. If true
(default), there will be one factory for each Struts module.
If false, there will be one common factory for all module. In this
later case, it is still needed to declare one plugin per module.
The factory will be initialized with parameters found in the first
initialized plugin (generally the one associated with the default
module).
true : One factory per module. (default)
false : one single shared factory for all modules
- definitions-parser-validate: (optional)
Specify if xml parser should validate the Tiles configuration file.
true : validate. DTD should be specified in file header (default)
false : no validation
Paths found in Tiles definitions are relative to the main context.
-->
<plug-in className="org.apache.struts.tiles.TilesPlugin" >
<set-property property="definitions-config" value="/WEB-INF/tiles-defs.xml" />
<set-property property="moduleAware" value="true" />
</plug-in>
<!-- ========================= Validator plugin ================================= -->
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property
property="pathnames"
value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/>
</plug-in>
adn my Action class is
package login;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import java.util.*;
import javax.sql.DataSource;
public class LoginAction extends org.apache.struts.action.Action {
/* forward name="success" path="" */
private static final String SUCCESS = "success";
private final static String FAILURE = "failure";
private ResultSet rs;
private PreparedStatement ps;
private Connection conn;
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
throws Exception {
LoginForm formBean = (LoginForm) form;
String fname = formBean.getName();
String lname = formBean.getEmail();
if ((fname == null) || lname == null) {
ArrayList al = new ArrayList();
DataSource ds = (DataSource) servlet.getServletContext().getAttribute("");
conn = ds.getConnection();
String query = "select * from login";
ps = conn.prepareStatement(query);
ResultSet rs = ps.executeQuery();
int i = 1;
while (rs.next()) {
al.add(rs.getString(i));
al.add(rs.getString(i + 1));
i++;
}
request.setAttribute("value", al);
formBean.setError();
return mapping.findForward(FAILURE);
}
return mapping.findForward(SUCCESS);
}
}
please any one to help to run the above program.....
Regards Mouli V
Your DataSource
is null
, which causes the NullPointerException
. And it is null
because you are trying to get a non-existent attribute:
DataSource ds = (DataSource) servlet.getServletContext().getAttribute("");
It appears that in Struts you obtain the DataSource
by calling getDataSource(request)
Another thing - you have tagged struts2, but are using struts1. Are you sure you are using the correct library?
When you are using Struts-1.X in-built 'datasource' feature, struts-config.xml contains all DB related details. In that you can use "validationQuery" property to validate everything about db-connection.
<data-sources>
<data-source key="datasource" type="org.apache.tomcat.dbcp.dbcp.BasicDataSource">
<set-property property="password" value="pass" />
<set-property property="username" value="uname" />
<set-property property="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<set-property property="url" value="jdbc:oracle:thin:@localhost:1521:abc" />
<set-property property="validationQuery" value="select sysdate from dual" />
</data-source>
</data-sources>
Strtus will initialize and provide Datasource and we can use it in any Action class in following way:
public class LoginAction extends Action
{
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
{
LoginForm loginForm = (LoginForm) form;
if (loginForm.getUsername() != null && loginForm.getPassword() != null)
{
DataSource dataSource = getDataSource(request, "datasource");
Connection myConnection = dataSource.getConnection();
PreparedStatement stmt = null;
String select = "SELECT USERNAME,PASSWORD from USER where USERNAME=?";
stmt = myConnection.prepareStatement(select);
stmt.setString(1, username);
rst = stmt.executeQuery();
................
}
}
精彩评论