27.Struts to enter the credentials of the Staff and store it in the database

<%@page contentType=”text/html”%>
<%@page pageEncoding=”UTF-8″%>
<%– <jsp:forward page=”Welcome.do”/> –%>
<form action=”performAction.do” method=”post” >
<h3>Example of insert data using Struts</h3>
<table border=”1″>
<th colspan=”2″ >Staff Details</th>
<td>ID </td>
<td><input type=”text” name=”userId”/></td>
<td>Name </td>
<td><input type=”text” name=”userName”/></td>
<tr align=”center”>
<td colspan=”2″><input type=”submit” value=”Insert”/></td>
<h3><%out.println(request.getAttribute(“msg”)); %></h3>

error.jsp :-
<%@page contentType=”text/html” pageEncoding=”UTF-8″%>
<!DOCTYPE html>
<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″>
<h1>Hello World!Error</h1>
<h2><%out.println(request.getAttribute(“errormsg”)); %></h2>

NewStrutsAction.java :-
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
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;
public class NewStrutsAction extends org.apache.struts.action.Action
private static final String SUCCESS = “success”;
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception
NewStrutsActionForm obj = (NewStrutsActionForm) form;
//define two variable to get value from jsp through NewStrutsActionForm
String id, name;
//Initialize value using object
id = obj.getUserId();
name = obj.getUserName();
Connection con = DriverManager.getConnection(“jdbc:odbc:dsn”);
Statement st = con.createStatement();
if (id.length() != 0 && name.length() != 0)
int m = st.executeUpdate(“insert into StaffData values(‘” + id + “‘,'” + name + “‘)”);
if (m > 0)
// This is send forward message to struts-config.xml for further process
// You can pass any message instead of “okay”
request.setAttribute(“msg”, “Successfully Inserted”);
return mapping.findForward(“okay”);
request.setAttribute(“errormsg”, “Null Value!”);
return mapping.findForward(“wrong”);
catch (Exception e)
request.setAttribute(“errormsg”, “” + e.getMessage());
//this message (wrong) will pass to struts-config.xml if any exception will ocuur
return mapping.findForward(“wrong”);

return mapping.findForward(SUCCESS);

NewStrutsActionForm.java :-
public class NewStrutsActionForm extends org.apache.struts.action.ActionForm {

private String userId;
private String userName;

public String getUserId() {
return userId;

public void setUserId(String userId) {
this.userId = userId;

public String getUserName() {
return userName;

public void setUserName(String userName) {
this.userName = userName;

struts-config.xml :-
<?xml version=”1.0″ encoding=”UTF-8″ ?>
<!DOCTYPE struts-config PUBLIC
“-//Apache Software Foundation//DTD Struts Configuration 1.3//EN”
<!– Form Bean important to write–>
<form-bean name=”NewStrutsActionForm” type=”NewStrutsActionForm”/>
<!–Action Mapping tag start here –>
<action input=”/index.jsp” name=”NewStrutsActionForm” path=”/performAction” scope=”session” type=”NewStrutsAction”>
<!– forward index.jsp page if “okay” message send from NewStrutsAction.class –>
<forward name=”okay” path=”/index.jsp”/>
<!– forward error.jsp page if “wrong” message send from NewStrutsAction.class –>
<forward name=”wrong” path=”/error.jsp”/>
<!– –>
<controller processorClass=”org.apache.struts.tiles.TilesRequestProcessor”/>
<message-resources parameter=”com/myapp/struts/ApplicationResource”/>
<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” />
<!– ========================= Validator plugin ================================= –>
<plug-in className=”org.apache.struts.validator.ValidatorPlugIn”>