register.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 | <%@page contentType="text/html"%> <%@page pageEncoding="UTF-8"%> <%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean" %> <%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html" %> <%@ taglib uri="http://jakarta.apache.org/struts/tags-logic" prefix="logic" %> <html:form action="/insert"> <center> <h1><u> Registration Screen </u></h1> <h2> <table border="0" width="100%" align="center"> <tbody> <tr> <td><bean:message key="registration.id"></bean:message></td> <td><html:text property="id"></html:text></td> </tr> <tr> <td><bean:message key="registration.name"></bean:message></td> <td><html:text property="name"></html:text></td> </tr> <tr> <td><bean:message key="registration.address"></bean:message></td> <td><html:text property="address"></html:text></td> </tr> <tr> <td><bean:message key="registration.doj"></bean:message></td> <td><html:text property="doj"></html:text></td> </tr> <tr> <td><bean:message key="registration.age"></bean:message></td> <td><html:text property="age"></html:text></td> </tr> <tr> <td><bean:message key="registration.email"></bean:message></td> <td><html:text property="email"></html:text></td> </tr> </tbody> </table> <center> <html:submit value="Register"> </html:submit></center></h2> </center></html:form> |
Failure.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <%@ taglib uri="/tags/struts-html" prefix="html" %> <center> <h2> The following error(s) were encountered : <html:errors> <a href="Register.jsp">Try Again ? </a> </html:errors></h2> </center> |
Fail.html
1 2 3 4 5 6 7 8 9 10 11 12 | <title>faill.html</title> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="this is my page"> <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1"> <!-- <link rel="stylesheet" type="text/css" href="./styles.css">--> This is my HTML page. |
web.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | <!--?xml version="1.0" encoding="UTF-8"?--> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <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> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <session-config> <session-timeout> 30 </session-timeout> </session-config> <welcome-file-list> <welcome-file>Register.jsp</welcome-file> </welcome-file-list> <jsp-config> <taglib> <taglib-uri>/WEB-INF/struts-bean.tld</taglib-uri> <taglib-location>/WEB-INF/struts-bean.tld</taglib-location> </taglib> <taglib> <taglib-uri>/WEB-INF/struts-html.tld</taglib-uri> <taglib-location>/WEB-INF/struts-html.tld</taglib-location> </taglib> <taglib> <taglib-uri>/WEB-INF/struts-logic.tld</taglib-uri> <taglib-location>/WEB-INF/struts-logic.tld</taglib-location> </taglib> <taglib> <taglib-uri>/WEB-INF/struts-nested.tld</taglib-uri> <taglib-location>/WEB-INF/struts-nested.tld</taglib-location> </taglib> <taglib> <taglib-uri>/WEB-INF/struts-tiles.tld</taglib-uri> <taglib-location>/WEB-INF/struts-tiles.tld</taglib-location> </taglib> </jsp-config> </web-app> |
struts-config.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | <!--?xml version="1.0" encoding="UTF-8" ?--> <struts-config> <form-beans> <form-bean name="bean" type="org.apache.struts.validator.DynaValidatorForm"> <form-property name="id" type="java.lang.String"> <form-property name="name" type="java.lang.String"> <form-property name="address" type="java.lang.String"> <form-property name="email" type="java.lang.String"> <form-property name="doj" type="java.lang.String"> <form-property name="age" type="java.lang.String"> </form-property></form-property></form-property></form-property></form-property></form-property></form-bean> </form-beans> <global-exceptions> </global-exceptions> <global-forwards> <forward name="welcome" path="/Welcome.do"> </forward></global-forwards> <action-mappings> <action input="/Failure.jsp" name="bean" path="/insert" scope="request" type="com.myapp.struts.Register" validate="true"> <forward name="ok" path="/Success.html"> <forward name="fail" path="/Failure.html"> </forward></forward></action> </action-mappings> <controller processorclass="org.apache.struts.tiles.TilesRequestProcessor"> <message-resources parameter="com/myapp/struts/ApplicationResource"> <!-- ========================= Tiles plugin ===============================--> <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"> </set-property></set-property></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"> </set-property></plug-in> </message-resources></controller></struts-config> |
validation.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | <!--?xml version="1.0" encoding="UTF-8" ?--> <form-validation> <formset> <form name="bean"> <field property="id" depends="required,integer"> <arg0 key="my.error.id"> </arg0></field> <field property="name" depends="required,mask"> <msg name="mask" key="my.error.mask.name"> <arg0 key="my.error.name"> <var> <var-name>mask</var-name> <var-value>^[a-z A-Z]*$</var-value> </var> </arg0></msg></field> <field property="address" depends="required,minlength"> <arg0 key="my.error.address"> <arg1 name="minlength" key="${var:minlength}" resource="false"> <var> <var-name>minlength</var-name> <var-value>10</var-value> </var> </arg1></arg0></field> <field property="email" depends="required, email"> <arg0 key="my.error.email"> </arg0></field> <field property="age" depends="required,integer,range"> <arg0 key="my.error.age"> <arg1 name="range" key="${var:min}" resource="false"> <arg2 name="range" key="${var:max}" resource="false"> <var> <var-name>min</var-name> <var-value>20</var-value> </var> <var> <var-name>max</var-name> <var-value>50</var-value> </var> </arg2></arg1></arg0></field> <field property="doj" depends="required,date"> <arg0 key="my.error.doj"> <var> <var-name>datePattern</var-name> <var-value>yyyy-MM-dd</var-value> </var> </arg0></field> </form> </formset> </form-validation> |
Register.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | package com.myapp.struts; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; 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.ActionMapping; import org.apache.struts.action.ActionForward; import org.apache.struts.validator.DynaValidatorForm; public class Register extends org.apache.struts.action.Action { private final static String SUCCESS = "success"; public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { Connection con=null; PreparedStatement ps=null; String status=null; try{ DynaValidatorForm registerForm = (DynaValidatorForm)form; Class.forName("com.mysql.jdbc.Driver"); con=DriverManager.getConnection("jdbc:mysql://localhost:3306/gobind","root","root"); System.out.println("connected"); ps=con.prepareStatement("insert into employee values(?,?,?,?,?,?)"); System.out.println("connected1"); ps.setString(1,(String)registerForm.get("id")); ps.setString(2,(String)registerForm.get("name")); System.out.println("connected3"); ps.setString(3,(String)registerForm.get("address")); ps.setString(6,(String)registerForm.get("email")); ps.setString(4,(String)registerForm.get("doj")); ps.setInt(5,Integer.parseInt((String)registerForm.get("age"))); System.out.println("connected2"); if(ps.executeUpdate()==0) status="fail"; else status="ok"; } catch (ClassNotFoundException ce) { ce.printStackTrace(); } catch(SQLException se) { se.printStackTrace(); } finally { try { if(ps!=null) ps.close(); } catch (SQLException se) { } try { if(con!=null) con.close(); } catch(SQLException se) {} } return mapping.findForward(status); } // execute() } |
ApplicationResources.properties
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | # Struts Validator Error Messages errors.header= <h1>List of error(s)</h1> <h2> <hr> errors.footer=</h2> <hr> errors.required={0} is required. errors.minlength={0} can not be less than {1} characters. errors.maxlength={0} can not be greater than {1} characters. errors.invalid={0} is invalid. errors.byte={0} must be a byte. errors.short={0} must be a short. errors.integer={0} must be an integer. errors.long={0} must be a long. errors.float={0} must be a float. errors.double={0} must be a double. errors.date={0} is not a date. errors.range={0} is not in the range {1} through {2}. errors.creditcard={0} is an invalid credit card number. errors.email={0} is an invalid e-mail address. my.error.id= <u>ID</u> my.error.name= <u>Name</u> my.error.address= <u>Address</u> my.error.email= <u>Email</u> my.error.age= <u>Age</u> my.error.doj= <u>Date of Joining</u> my.error.mask.name= Please provide only alphabets for <u>Name</u> registration.id=Student ID registration.name=First Name registration.email=E-Mail Address registration.address=Resedential Address registration.doj=Date of Joining (YYYY-MM-DD) registration.age=Age |