0% found this document useful (0 votes)
273 views

Baseaction:: Package Import Import Import Import Import Import Import Import Import Import Public Class Extends Public

The document describes code for a Struts-based web application that allows adding employees. It includes: 1) A BaseAction class that handles exceptions and forwards requests. 2) An Exception class for custom exceptions. 3) A DBConnection class for database connectivity. 4) A TestConnection class for testing connections. 5) An EmpAddAction class that retrieves department lists and handles form submission. 6) An EmpAddForm class for the form. 7) An EmpAddBean class to represent form data. 8) A JSP page to display the form. The application retrieves department lists and allows selecting a department when adding a new employee

Uploaded by

api-3745771
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
273 views

Baseaction:: Package Import Import Import Import Import Import Import Import Import Import Public Class Extends Public

The document describes code for a Struts-based web application that allows adding employees. It includes: 1) A BaseAction class that handles exceptions and forwards requests. 2) An Exception class for custom exceptions. 3) A DBConnection class for database connectivity. 4) A TestConnection class for testing connections. 5) An EmpAddAction class that retrieves department lists and handles form submission. 6) An EmpAddForm class for the form. 7) An EmpAddBean class to represent form data. 8) A JSP page to display the form. The application retrieves department lists and allows selecting a department when adding a new employee

Uploaded by

api-3745771
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 18

BaseAction :

package upsl.Action;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import upsl.Exception.UPSLException;

public abstract class UPSLBaseAction extends Action {

public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException,ServletException{

ActionErrors errors = new ActionErrors();
ActionForward forward = null;
String forwardString = null;
request.getSession().setAttribute("error","");
try {
forwardString =  performAction(mapping, form, request, 
response);

forward = mapping.findForward(forwardString);

} catch (Exception e) {
// Report the error using the appropriate name and ID.

request.getSession().setAttribute("error",e.getMessage());

forward = mapping.findForward("errorPage");;

e.printStackTrace();
}
// Finish with

return (forward);

/**
* Method performAction
 * @param mapping Mapping
 * @param form ActionForm
 * @param request HttpServletRequest
 * @param response HttpServletResponse
 * @return String
 * @throws SCAException
 * @throws SCAMultiException
 */
protected abstract String performAction(ActionMapping mapping,
ActionForm form, HttpServletRequest request, 
HttpServletResponse response) throws UPSLException;

Exception :

package upsl.Exception;

public class UPSLException extends Exception {

public UPSLException() {
super();
}

public UPSLException(String s) {
super(s);
}

DBConnection:

/*
* Created on May 12, 2004
*
* To change the template for this generated file go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
package upsl.Util;

//import java.sql.*;
import java.sql.Connection;
import java.sql.DriverManager;

import javax.naming.InitialContext;
import javax.sql.DataSource;

import org.apache.struts.util.MessageResources;
import org.apache.struts.util.MessageResourcesFactory;
import com.mysql.jdbc.Driver;
import upsl.Exception.UPSLException;

public class DBConnection{

private static DataSource ds =  null; 
    public DBConnection(String DSN){
    } 
public static Connection getConnection() throws UPSLException {
Connection connection = null;

MessageResourcesFactory fact = 
MessageResourcesFactory.createFactory();
MessageResources resource = 
fact.createResources("upsl.resources.ApplicationResources");
// System.out.println("resource message
"+resource.getMessage("UPSL.DSN"));
// System.out.println("connection String"+
resource.getMessage("UPSL.connectionString"));
// end of resource fetching
// Properties props = System.getProperties();
// System.out.println("in system
env"+props.getProperty("UPSL.DSN"));
        
        

     try {
// Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//connection =
DriverManager.getConnection("jdbc:odbc:UPSL","UPSL","UPSL123");
Class.forName("com.mysql.jdbc.Driver").newInstance();
connection = 
DriverManager.getConnection("jdbc:mysql://helios2/UPSL","UPSL","UPSL123"
);
// connection =
DriverManager.getConnection(resource.getMessage("UPSL.DSN"),resource.get
Message("UPSL.UName"),resource.getMessage("UPSL.Passwd"));
//
Class.forName(props.getProperty("UPSL.connectionString"));
// connection =
DriverManager.getConnection(props.getProperty("UPSL.DSN"),props.getPrope
rty("UPSL.UName"),props.getProperty("UPSL.Passwd"));
connection.setAutoCommit(false);
} catch (Exception e) {
e.printStackTrace();
throw new UPSLException (e.getMessage());
}
return connection;
}

public static DataSource getDataSource() throws UPSLException {
try {
InitialContext context = new InitialContext();
ds = (DataSource) context.lookup("jdbc/ds1");
} catch (javax.naming.NameNotFoundException e) {
throw new UPSLException ("Datasource lookup failed
with NameNotFoundException: " + e.getMessage());
} catch (Exception e) {
throw new UPSLException (e.getMessage());
}
return ds;
}

public static String formatDate(String dateFormat) {
return 
dateFormat.substring(8,10)+"/"+dateFormat.substring(5,7)+"/"+dateFormat.
substring(0,4);
}

public static String dateFormat(String dateFormat) {
return 
dateFormat.substring(6,10)+"-"+dateFormat.substring(3,5)+"-"+dateFormat.
substring(0,2);
}
}

TestConnection :

package upsl.Util;

import java.sql.*;
import com.mysql.jdbc.Driver;
public class TestConnection {
public static void main(String args[]) {
Connection connection = null;
try {
System.out.println("Loading class");
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Loaded class");
                        connection = 
DriverManager.getConnection("jdbc:mysql://helios2/UPSL?user=UPSL&passwor
d=UPSL123");
System.out.println("Connection established");
Statement st = connection.createStatement();
ResultSet rs = st.executeQuery("show tables");
while(rs.next()) {
System.out.println(rs.getString(1));
}
rs.close();
st.close();
connection.close();
System.out.println("Connection Established " + 
connection);

} catch (Exception e) {
System.out.println(e);
}
}
}

***********************************************************************
*

Select

Jsp:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">


<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB­INF/struts­bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB­INF/struts­logic.tld" prefix="logic" %>
<%@ page language="java"contentType="text/html; charset=ISO­8859­1"%>

<html:base/>
<html:html>

<HEAD>
<META http-equiv="Content­Type" content="text/html; charset=ISO­8859­">
<META name="GENERATOR" content="IBM WebSphere Studio">
<TITLE>UPSLEmpAddJsp.jsp</TITLE>

<script language="javascript">
function doSubmit(sv)
{
document.forms[0].sendvalue.value = sv;
document.forms[0].submit();
}
</script>

</HEAD>
<BODY>
<html:form action="empAddAction">
<input type="hidden" name="sendvalue"/>
<html:select name="empAddForm" property="deptId">
<html:option value=""> [SELECT DEPARTMENT ID]</html:option>
<html:options property="deptIdList" labelProperty="deptNameList"/>
</html:select>
<input type="button" value="Save" 
onClick="javascript:doSubmit('save');"/>

</html:form>
</BODY></html:html>

Action :

package upsl.Action;

import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import upsl.Exception.UPSLException;
import upsl.Form.UPSLEmpAddForm;
import upsl.JavaBean.UPSLEmpAddBean;
import upsl.Manager.UPSLEmpAddMg;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

public class UPSLEmpAddAction extends UPSLBaseAction{
public String performAction(ActionMapping mapping,ActionForm 
form,HttpServletRequest request,HttpServletResponse response) 
throws UPSLException
{
UPSLEmpAddForm empform=(UPSLEmpAddForm)form;

if("save".equals(request.getParameter("sendvalue")))
{
System.out.println("Testing Value :" +empform.getDeptId()); 
}

UPSLEmpAddMg empmg = new UPSLEmpAddMg();
ArrayList dept =empmg.getdeptList();
ArrayList deptId=(ArrayList) dept.get(0);
ArrayList deptName=(ArrayList) dept.get(1);

empform.setDeptIdList(deptId);
empform.setDeptNameList(deptName);

return "Arul";
}

Form :

package upsl.Form;

import java.util.ArrayList;
import org.apache.struts.action.ActionForm;

public class UPSLEmpAddForm extends ActionForm{
private String deptId;
private ArrayList deptIdList= new ArrayList();
private ArrayList deptNameList = new ArrayList();

public String getDeptId() {
return deptId;
}

public ArrayList getDeptIdList() {
return deptIdList;
}

public ArrayList getDeptNameList() {
return deptNameList;
}

public void setDeptId(String deptId) {
this.deptId = deptId;
}

public void setDeptIdList(ArrayList deptIdList) {
this.deptIdList = deptIdList;
}

public void setDeptNameList(ArrayList deptNameList) {
this.deptNameList = deptNameList;
}

Bean :

package upsl.JavaBean;

import java.util.ArrayList;

public class UPSLEmpAddBean {
private String deptId;
private ArrayList deptIdList = new ArrayList();
private ArrayList deptNameList = new ArrayList();

/**
 * Returns the deptId.
 * @return String
 */
public String getDeptId() {
return deptId;
}

/**
 * Returns the deptIdList.
 * @return ArrayList
 */
public ArrayList getDeptIdList() {
return deptIdList;
}

/**
 * Returns the deptNameList.
 * @return ArrayList
*/
public ArrayList getDeptNameList() {
return deptNameList;
}

/**
 * Sets the deptId.
 * @param deptId The deptId to set
 */
public void setDeptId(String deptId) {
this.deptId = deptId;
}

/**
 * Sets the deptIdList.
 * @param deptIdList The deptIdList to set
 */
public void setDeptIdList(ArrayList deptIdList) {
this.deptIdList = deptIdList;
}

/**
 * Sets the deptNameList.
 * @param deptNameList The deptNameList to set
 */
public void setDeptNameList(ArrayList deptNameList) {
this.deptNameList = deptNameList;
}

Manager :

package upsl.Manager;

import upsl.Db.UPSLEmpAddDb;
import upsl.Util.DBConnection;

import java.util.*;
import java.sql.*;
import upsl.Db.*;
public class UPSLEmpAddMg {
Connection con;

public UPSLEmpAddMg()
{
try{
con = DBConnection.getConnection(); 
}catch(Exception e){}
}
public ArrayList getdeptList() {
UPSLEmpAddDb empdb = new UPSLEmpAddDb();
empdb.setConnection(con);
ArrayList dept = empdb.getDept();
return dept;
}

Db :

package upsl.Db;

import java.util.ArrayList;
import java.sql.*;

public class UPSLEmpAddDb {

Connection con=null;
Statement st=null;
ResultSet rs=null;

public void setConnection(Connection con)
{
this.con = con;
}

public ArrayList getDept() 
{
ArrayList deptIdList = new  ArrayList();
ArrayList deptNameList = new  ArrayList();
ArrayList deptList = new  ArrayList();
try{
String query = "select dm_dept_id,dm_dept_name from
dm_department_master";
        st  =  con.createStatement();
    rs= st.executeQuery(query);
    while(rs.next())
       { 
       deptIdList.add(rs.getString(1));
       deptNameList.add( rs.getString(2));
         }
deptList.add(deptIdList);
deptList.add(deptNameList);      
}catch(Exception e){}     
return deptList;
}
}

***********************************************************************

Logic-Iterate:
JSP :

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

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


<%@ taglib uri="/WEB­INF/struts­bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB­INF/struts­logic.tld" prefix="logic" %>
<%@ page language="java"contentType="text/html; charset=ISO­8859­1"%>

<html:base/>
<html:html>
<HEAD>
<META http-equiv="Content­Type" content="text/html; charset=ISO­8859­1">
<META name="GENERATOR" content="IBM WebSphere Studio">
<TITLE>balaJsp.jsp</TITLE>
</HEAD>
<BODY>
<input type="button" value="yes" >
<html:form action="balaAction">

<logic:iterate id="dept" name="balaForm" property="deptList" 
type="upsl.JavaBean.BalaBean" indexId="index">
<table>
<tr>
<td>
<div>
<bean:write name="dept" property="deptId"/>
</div>
</td>
<td>
<div>
<bean:write name="dept" property="deptName"/>
</div>
</td>
<td>
<div>
<html:text name="balaForm" property='<%= "deptList["+ index+ 
"].deptName"%>' />
</div>
</td>
</tr>
</table>
</logic:iterate>
</html:form>
</BODY>
</html:html>

Action :

package upsl.Action;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
Import java.util.ArrayList;
import javax.servlet.http.*;
Import upsl.Exception.*;
import upsl.Form.BalaForm;
import upsl.JavaBean.BalaBean;
import upsl.Manager.BalaMg;

public class BalaAction extends UPSLBaseAction {

public String performAction(ActionMapping mapping,ActionForm 
form,HttpServletRequest request, HttpServletResponse response) throws 
UPSLException
{
try{
BalaForm bform = (BalaForm)form;
BalaMg bmg=new BalaMg();
ArrayList deptList= new ArrayList();
deptList=bmg.getdeptList();
bform.setDeptList(deptList);
}catch(Exception e){
System.out.println(e);}
return "Call";
}

Form:
package upsl.Form;

import java.util.ArrayList;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import upsl.JavaBean.BalaBean;

public class BalaForm extends ActionForm {
private String deptId;
private String deptName;
private ArrayList deptList = new ArrayList();

public String getDeptId() {
return deptId;
}

public ArrayList getDeptList() {
return deptList;
}

public String getDeptName() {
return deptName;
}

public void setDeptId(String deptId) {
this.deptId = deptId;
}

public void setDeptList(ArrayList deptList) {
this.deptList = deptList;
}

public void setDeptName(String deptName) {
this.deptName = deptName;
}

Bean :

package upsl.JavaBean;

import java.util.ArrayList;

public class BalaBean {
private String deptId;
private String deptName;
private ArrayList deptList = new ArrayList();

public String getDeptId() {
return deptId;
}

public ArrayList getDeptList() {
return deptList;
}

public String getDeptName() {
return deptName;
}

public void setDeptId(String deptId) {
this.deptId = deptId;
}

public void setDeptList(ArrayList deptList) {
this.deptList = deptList;
}

public void setDeptName(String deptName) {
this.deptName = deptName;
}

Manager :

package upsl.Manager;

import java.sql.Connection;
import java.util.ArrayList;
import upsl.Db.BalaDb;
import upsl.Util.DBConnection;

public class BalaMg {
Connection con;

public BalaMg() throws Exception
{

con=DBConnection.getConnection();

}
public ArrayList getdeptList() throws Exception {

ArrayList deptList = new ArrayList();
BalaDb bdb=new BalaDb();
bdb.SetConnection(con);
deptList=bdb.getDeptList();
return deptList;
}

Db:

package upsl.Db;

import java.util.ArrayList;
import java.sql.*;
import upsl.JavaBean.BalaBean;

public class BalaDb {

Connection con;
Statement st;
ResultSet rs;
ArrayList deptList = new ArrayList();

public void SetConnection(Connection con)
{
this.con=con;
}

public ArrayList getDeptList() {
try{
String query="select dm_dept_id,dm_dept_name from
dm_department_master";
st = con.createStatement();
rs=st.executeQuery(query);
while(rs.next())
{
BalaBean bbean = new BalaBean();
bbean.setDeptId(rs.getString(1));
bbean.setDeptName(rs.getString(2));
deptList.add(bbean);
}
}catch(Exception e)
{
System.out.println(e);
}
return deptList;
}

}
CheckBox :

JSP:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

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


<%@ taglib uri="/WEB­INF/struts­bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB­INF/struts­logic.tld" prefix="logic" %>
<%@ page
language="java"
contentType="text/html; charset=ISO­8859­1"
%>
<html:base/>
<html:html>
<HEAD>
<META http-equiv="Content­Type" content="text/html; charset=ISO­8859­1">
<META name="GENERATOR" content="IBM WebSphere Studio">
<TITLE>balaJsp.jsp</TITLE>

</HEAD>
<BODY>

<html:form action="balaAction">

<html:checkbox name="balaForm" property="checked">
</html:checkbox>

<input type="submit" value="Submit">

</html:form>
</BODY>
</html:html>
Action:

package upsl.Action;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import java.util.ArrayList;
import javax.servlet.http.*;
import upsl.Exception.*;
import upsl.Form.BalaForm;

public class BalaAction extends UPSLBaseAction {

public String performAction(ActionMapping mapping,ActionForm 
form,HttpServletRequest request, HttpServletResponse response) throws 
UPSLException
{

BalaForm bform = (BalaForm)form;

 if(bform.isChecked())
   {
     bform.setChecked(false);
   }
   else
   {
    bform.setChecked(true);
   }
 

return "Call";
}

Form:

package upsl.Form;

import java.util.ArrayList;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
public class BalaForm extends ActionForm {
private boolean checked;

public boolean isChecked() {
return checked;
}

public void setChecked(boolean checked) {
this.checked = checked;
}

You might also like