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


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

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

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”;
@Override
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();
try
{
Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
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”);
}
}
else
{
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”
http://jakarta.apache.org/struts/dtds/struts-config_1_3.dtd”&gt;
<struts-config>
<!– Form Bean important to write–>
<form-beans>
<form-bean name=”NewStrutsActionForm” type=”NewStrutsActionForm”/>
</form-beans>
<!–Action Mapping tag start here –>
<action-mappings>
<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”/>
</action>
</action-mappings>
<!– –>
<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” />
</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>