Struts error:Servlet action is not available
My Struts is 1.2.9,and tomcat is 5.5.15,and JDK is 1.5,I want to use Struts Datasource,but it raise follows error:
type Status report
message Servlet action is not available
description The requested resource (Servlet action is not available) is not available.
My code is follows:
/*TestForm.java*/
package test;
import org.apache.struts.action.*;
publicclass TestFormextends ActionForm{
}
/*TestAction.java*/
package test;
import java.io.*;
import java.util.*;
import java.sql.*;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
publicclass TestActionextends Action{
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception{
System.out.println("ok");
javax.sql.DataSource dataSource;
java.sql.Connection myConnection=null;
try{
dataSource = getDataSource(request);
myConnection = dataSource.getConnection();
Statement stmt = myConnection.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM abc");
while(rs.next()){
System.out.print(rs.getString(1));
}
rs.close();
stmt.close();
}catch(Exception e){
e.printStackTrace();
}
finally{
try{
myConnection.close();
}
catch (SQLException e){
e.printStackTrace();
}
}
returnnull;
}
}
/*Struts-config.xml*/
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">
<struts-config>
<data-sources>
<data-source type="org.apache.commons.dbcp.BasicDataSource">
<set-property
property="driverClassName"
value="sun.jdbc.odbc.JdbcOdbcDriver" />
<set-property
property="url"
value="jdbc:odbc:bb" />
<set-property
property="username"
value="administrator" />
<set-property
property="password"
value="123" />
<set-property
property="maxActive"
value="10" />
<set-property
property="maxWait"
value="5000" />
<set-property
property="defaultAutoCommit"
value="false" />
<set-property
property="defaultReadOnly"
value="false" />
<set-property
property="abc"
value="SELECT COUNT(*) FROM abc" />
</data-source>
</data-sources>
<form-beans>
<form-bean name="Test" type="test.TestForm" />
</form-beans>
<action-mappings>
<action
path="/Test"
type="test.TestAction"
name="Test"
scope="request"
validate="true"
input="/test.jsp"/>
</action-mappings>
</struts-config>
/*test.jsp*/
<html>
<body>
OK
</body>
</html>
/*web.xml*/
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC"-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
"http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">
<web-app>
<display-name>Struts Blank Application</display-name>
<!-- Standard Action Servlet Configuration (with debugging) -->
<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>2</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>2</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<!-- Standard Action Servlet Mapping -->
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!-- The Usual Welcome File List -->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- Struts Tag Library Descriptors -->
<taglib>
<taglib-uri>/tags/struts-bean</taglib-uri>
<taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/tags/struts-html</taglib-uri>
<taglib-location>/WEB-INF/struts-html.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/tags/struts-logic</taglib-uri>
<taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/tags/struts-nested</taglib-uri>
<taglib-location>/WEB-INF/struts-nested.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/tags/struts-tiles</taglib-uri>
<taglib-location>/WEB-INF/struts-tiles.tld</taglib-location>
</taglib>
</web-app>
When I run http://localhost:8080/Test.do,it raise following error:
type Status report
message Servlet action is not available
description The requested resource (Servlet action is not available) is not available.
When I removes data-source from Struts-config.xml,it can run well.But when I add data-source into Struts-config.xml,it raise above erorr. I am puzzled with it very much! Anybody can tell me the reason and how to correct it?
Thanks in advance!
Hi,
I m giving here code for struts config.xml and action java file..pls chk it and help me asap..
Struts-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">
<struts-config>
<data-sources>
<data-source type="org.apache.commons.dpcp.BasicDataSource" key="StrutsChk">
<set-property property="driverClassName" value="com.microsoft.jdbc.sqlserver.SQLServerDriver"/>
<set-property property="url" value="jdbc:microsoft:sqlserver://10.20.1.22;DataBaseName=tempdb"/>
<set-property property="username" value="sa" />
<set-property property="password" value="" />
</data-source>
</data-sources>
<form-beans>
<form-bean name="userRegistrationForm" type="com.sahara.action.userRegistrationForm">
</form-bean>
</form-beans>
<global-exceptions>
</global-exceptions>
<global-forwards>
</global-forwards>
<action-mappings>
<!-- <set-property property="cancellable" value="true"/> -->
<action path="/userRegistration"
type="com.sahara.action.userRegistrationAction"
input="/userRegistration.jsp"
name="userRegistrationForm">
<set-property property="cancellable" value="true"/>
<exception key="errors.cancel"
type="org.apache.struts.action.InvalidCancelException"
path="/userRegistration.jsp"/>
<forward name="success" path="/regSuccess.jsp"/>
<forward name="cancel" path="/regSuccess.jsp"/>
</action>
<action forward="/regSuccess.jsp" path="/logout"/>
</action-mappings>
<controller processorClass="org.apache.struts.tiles.TilesRequestProcessor"/>
<message-resources parameter="com/sahara/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>
</struts-config>
UserRegistration.jsp
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@taglib uri="/tags/struts-html" prefix="html"%>
<%@taglib uri="/tags/struts-bean" prefix="bean"%>
<%--
The taglib directive below imports the JSTL library. If you uncomment it,
you must also add the JSTL library to the project. The Add Library... action
on Libraries node in Projects view can be used to add the JSTL 1.1 library.
--%>
<%--
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
--%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>User Resgistration</title>
</head>
<body>
<h3 align="center">User Resgistration</h3>
<html:errors/>
<table border="1" cellpadding="1" align="center">
<html:form action="userRegistration" method="post">
<tr>
<td colspan="2"><html:link action="/logout">Logout</html:link></td>
</tr>
<tr>
<td><bean:message key="userRegistration.FirstName"/></td>
<td><html:text property="fName"></html:text></td>
</tr>
<tr>
<td><bean:message key="userRegistration.LastName"/></td>
<td><html:text property="lName"></html:text></td>
</tr>
<tr>
<td><bean:message key="userRegistration.UserName"/></td>
<td><html:text property="uName"></html:text></td>
</tr>
<tr>
<td><bean:message key="userRegistration.Password"/></td>
<td><html:password property="password"></html:password></td>
</tr>
<tr>
<td><bean:message key="userRegistration.Email"/></td>
<td><html:text property="email"></html:text></td>
</tr>
<tr>
<td><bean:message key="userRegistration.Fax"/></td>
<td><html:text property="fax"></html:text></td>
</tr>
<tr>
<td><bean:message key="userRegistration.Phone"/></td>
<td><html:text property="phone"></html:text></td>
</tr>
<tr>
<td colspan=2"" align="center"><html:submit property="submit"/>
<html:cancel />
<html:reset/>
</td>
</tr>
</html:form>
</table>
</body>
</html>
userRegistrationAction.java
/*
* userRegistrationAction.java
*
* Created on April 13, 2007, 12:09 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package com.sahara.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import java.sql.Connection;
import java.sql.PreparedStatement;
import javax.sql.DataSource;
/**
*
* @author Administrator
*/
public class userRegistrationAction extends Action{
/**
* Creates a new instance of userRegistrationAction
*/
public userRegistrationAction() {
}
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception
{
userRegistrationForm userForm= (userRegistrationForm)form;
DataSource objDataSource=getDataSource(request,"StrutsChk");
Connection conn=objDataSource.getConnection();
try{
PreparedStatement stmt=conn.prepareStatement
(" insert into StrutsTab " +
"( fName , lName, uName, Password ,Phone ,Fax ,Email ) " +
"values (?,?,?,?,?,?,?)");
stmt.setString(1,userForm.getfName());
stmt.setString(2,userForm.getlName());
stmt.setString(2,userForm.getuName());
stmt.setString(3,userForm.getpassword());
stmt.setString(3,userForm.getPhone());
stmt.setString(3,userForm.getFax());
stmt.setString(3,userForm.getEmail());
stmt.executeUpdate();
}
finally{
conn.close();
}
/*if(isCancelled(request)){
System.out.println("Request Cancel");
return mapping.findForward("cancel");
}
else
{*/
return mapping.findForward("success");
//}
}
}
Pls reply me