Monday, July 23, 2012

1.3. Basic Struts Example



WEB-INF\results\

bad-address.jsp

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD><TITLE>Illegal Email Address</TITLE></HEAD>
<BODY BGCOLOR="#FDF5E6">
<CENTER>
<H1>invalid Email Address</H1>
Please <A HREF="index.jsp">try again</A>.
</CENTER>
</BODY></HTML>

bad-password.jsp

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD><TITLE>invalid password</TITLE></HEAD>
<BODY BGCOLOR="#FDF5E6">
<CENTER>
<H1>invalid password</H1>
Please <A HREF="index.jsp">try again</A>.
</CENTER>
</BODY></HTML>

confirm.jsp

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<HTML>
<HEAD><TITLE>Success</TITLE></HEAD>
<BODY BGCOLOR="#FDF5E6">
<CENTER>
<H1>You are login successfully.</H1>
<bean:write name="userFormBean" property="name" />
Congratulations.
</CENTER>
</BODY></HTML>

1.2 Basic Struts Example


RegisterAction1.java

package coreservlets;
import javax.servlet.http.*;


import org.apache.struts.action.*;


public class RegisterAction1 extends Action {
  public ActionForward execute(ActionMapping mapping,
                               ActionForm form,
                               HttpServletRequest request,
                               HttpServletResponse response)
      throws Exception {
UserFormBean userBean = (UserFormBean)form;
    String email = userBean.getEmail();
    String password = userBean.getPassword();
if(email.equals("om@gmail.com"))
{
if(password.equals("om"))
return(mapping.findForward("success"));
else
return(mapping.findForward("bad-password"));
}
 else
     return(mapping.findForward("bad-address"));
  }
}

1.1. Basic Struts Example...


UserFormBean.java

package coreservlets;
import java.sql.*;
import org.apache.struts.action.*;


public class UserFormBean extends ActionForm {
  private String email = "Missing address";
  private String password = "Missing password";
  public String getEmail() {
    return(email);
  }


  public String getName()
  {
Connection con=null;
String name="not defined";
try{
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql:///omdatabase","root","root");
Statement stat=con.createStatement();
ResultSet rs=stat.executeQuery("Select * from Student");
while(rs.next())
{
name=rs.getString(1);
}
}
catch(Exception ex)
{
name=""+ex;
}
return name;
  }
  public void setEmail(String email) {
    this.email = email;
  }


  public String getPassword() {
    return(password);
  }


  public void setPassword(String password) {
    this.password = password;
  }
}

1.0.Basic Struts Example


index.jsp

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD><TITLE>New Account Registration</TITLE></HEAD>
<BODY BGCOLOR="#FDF5E6">
<CENTER>
<H1>New Account Registration</H1>
<FORM ACTION="register1.do" METHOD="POST">
  Email address: <INPUT TYPE="TEXT" NAME="email"><BR>
  Password: <INPUT TYPE="PASSWORD" NAME="password"><BR>
  <INPUT TYPE="SUBMIT" VALUE="Sign in me!">
</FORM>
</CENTER>
</BODY></HTML>


\WEB-INF

web.xml


<web-app>
  <!-- Standard Action Servlet Configuration  -->
  <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>
  </servlet>
  <!-- Standard Action Servlet Mapping -->
  <servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>
</web-app>

struts-config.xml

<struts-config>
  <form-beans>
    <form-bean name="userFormBean"
               type="coreservlets.UserFormBean"/>
  </form-beans>
      <global-forwards>
    <forward name="success" 
             path="/WEB-INF/results/confirm.jsp"/>
  </global-forwards>
    <action-mappings>
    <action path="/register1" 
            type="coreservlets.RegisterAction1"
name="userFormBean"
            scope="request">
<forward name="bad-password" 
               path="/WEB-INF/results/bad-password.jsp"/>
<forward name="bad-address" 
               path="/WEB-INF/results/bad-address.jsp"/>
    </action>
  </action-mappings>
</struts-config>

Note- struts.jar must be in /WEB-INF/lib