Registration Form - Struts 1.3 Example - Eclipse - MySQL - Apache Tomcat

Simple Struts Example: Registration Form

Struts 1.3 - Java - MySQL- Eclipse - Apache Tomcat




Project Screenshots:




Project Explorer:



Source code:

struts-config.xml

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
          "http://struts.apache.org/dtds/struts-config_1_3.dtd">

<struts-config>

    
    <form-beans>
        
  <form-bean name="registrationForm" type="com.loginexample.forms.RegistrationForm"/>

    </form-beans>

    
    <action-mappings>

     <action name="registrationForm" path="/register" type="com.loginexample.actions.RegistrationAction" scope="request" input="/Register.jsp">
         <forward name="failure" path="/Failure.jsp" redirect="true"/>
         <forward name="success" path="/Success.jsp" redirect="true"/>
     </action>

    </action-mappings>
     

</struts-config>

web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>StrutsLoginFormExample</display-name>
  <!-- WWW.2K8618.BLOGSPOT.COM -->
  <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>
    <load-on-startup>2</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

Register.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"   pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Registration Form</title>
</head>
<body>
<center>
<h1>Registration Form </h1>
<html:form action="/register" >
 Username : <html:text property="username"/><br/>
 <br/>
 Password : <html:password property="paswd"/><br/>
 <br/>
 <html:submit value="Register" />
</html:form>
</center>
</body>
</html>

RegistrationForm.java
package com.loginexample.forms;

import org.apache.struts.action.ActionForm;


public class RegistrationForm extends ActionForm
{
  /**
  * 
  */
 private static final long serialVersionUID = 1L;
  private String username = null;
  private String paswd = null;
  
  public String getUsername() {
   return username;
  }
  
  public void setUsername(String username) {
   this.username = username;
  }
  
  public String getpaswd() {
   return paswd;
  }
  
  public void setpaswd(String paswd) {
   this.paswd = paswd;
  }
}

RegistrationAction.java
package com.loginexample.actions;

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.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.loginexample.forms.RegistrationForm;
import com.loginexample.services.LoginService;

public class RegistrationAction extends Action {
 @Override
 public ActionForward execute(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response)
   throws Exception {
  RegistrationForm regForm=(RegistrationForm) form;
  LoginService loginService= new LoginService();
  
  try
  {
   if(loginService.addLogin(regForm))
    return mapping.findForward("success");
   else
    return mapping.findForward("failure");
  }
  catch (Exception e)
  {
   return mapping.findForward("failure");
  }
 
 }

}


LoginService.java
package com.loginexample.services;

import java.sql.SQLException;

import com.loginexample.dao.impl.LoginDaoImplementation;
import com.loginexample.forms.RegistrationForm;

public class LoginService {
 LoginDaoImplementation loginDao= new LoginDaoImplementation();
 
 public boolean addLogin(RegistrationForm regForm) throws SQLException
 {
  if(loginDao.addLogin(regForm))
   return true;
  else
   return false;
 }
}


LoginDao.java
package com.loginexample.dao;

import java.sql.SQLException;

import com.loginexample.forms.RegistrationForm;

public interface LoginDao 
{
 public boolean addLogin(RegistrationForm regForm) throws SQLException;
}


LoginDaoImplementation.java
package com.loginexample.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import com.loginexample.dao.LoginDao;
import com.loginexample.forms.RegistrationForm;
import com.loginexample.utilities.DBUtilities;

public class LoginDaoImplementation implements LoginDao
{
 public boolean addLogin(RegistrationForm regForm) throws SQLException
 {
  Connection con=null;
  boolean access=false;
  
  try
  {
   con=DBUtilities.getConnection();
   
   String query= "insert into login values (?,?)";
   PreparedStatement stmt= con.prepareStatement(query);
   stmt.setString(1, regForm.getUsername());
   stmt.setString(2, regForm.getpaswd());
   int result= stmt.executeUpdate();
   
   if(result>0)
   {
    access=true;
   }
  }
  finally
  {
   DBUtilities.closeConnection(con);
  }
  return access;
 }
}


DBUtilities.java
package com.loginexample.utilities;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class DBUtilities {
 
    public static Connection getConnection()
    {
     Connection con = null;
     
     try 
     {
    Class.forName("com.mysql.jdbc.Driver");
    con  =DriverManager.getConnection("jdbc:mysql://localhost:3306/nn","root","nn");
     } 
     catch (ClassNotFoundException e) 
     {
   // TODO Auto-generated catch block
      e.printStackTrace();
     } 
     catch (SQLException e) 
     {
   // TODO Auto-generated catch block
      e.printStackTrace();
     }
     
     return con; 
     
    }
 
     public static void closeConnection(Connection con){      
      if(con!=null){
       try{
       con.close();
       }catch(SQLException e){}
      }      
     }   
       public static void closePreparedStatement(PreparedStatement ps){      
      if(ps!=null){
       try{
       ps.close();
       }catch(SQLException e){}
      }
      
     }

}


Success.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Registration Successful</title>
</head>
<body>
<center>
 <h1>Registration Successful</h1>
</center> 
</body>
</html>

Failure.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Registration Failure</title>
</head>
<body>
<center>
 <h1 style="color: red;">Registration Failed.</h1>
</center>
</body>
</html>


MySql Database
mysql> desc login;
+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| username | varchar(20) | YES  |     | NULL    |       |
| password | varchar(25) | YES  |     | NULL    |       |
+----------+-------------+------+-----+---------+-------+
2 rows in set (0.06 sec)



Download: StrutsExampleRegistration

16 comments:

  1. I have read your article, it is very informative and helpful for me.I admire the valuable information you offer in your articles. Thanks for posting it..
    Flipkart Credit Card Offers
    Flipkart hdfc debit card offers

    ReplyDelete
  2. I have read through other blogs, but they are cumbersome and confusing more than your post. I hope you continue to have such quality articles to share with everyone! I believe a lot of people will be surprised to read this article!

    ReplyDelete
  3. Thank you for sharing. Thanks to this article I can learn more things. Expand knowledge. Thank you
    happy wheels

    ReplyDelete
  4. We believe in practice what you preach and thus the Amazon Web Services Training at APTRON Gurgaon involves "Hands-on-experience" therefore each person is motivated to practically conduct each topic which is discussed in the sessions provided at APTRON Gurgaon.
    For More Info: AWS Course in Gurgaon

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete
  6. Grab the best fitness tracker under 5000 and start monitoring your fitness routine with the best fitness tracker of India. best fitness bands under 5000

    ReplyDelete
  7. Apply for freelance acting jobs, freelance modelling jobs, freelance singing jobs, freelance voice over artists jobs, freelance musicians jobs in India. jobs for actor

    ReplyDelete
  8. Best place to purchase amazing gifts for Christmas for your parents , grandma , grandpa, dad etc. Buy your gifts today. thoughtful gifts for men

    ReplyDelete
  9. Browse all the current and trending updates going on in India, get the accurate news if what's the latest viral news and all other news. trending india

    ReplyDelete
  10. Avail the best motivational, inspirational, facebook and whatsapp love, life, sad, attitude status in hindi. motivational stories in hindi

    ReplyDelete
  11. This post is very simple to read and appreciate without leaving any details out. Great work! hosted call center solutions

    ReplyDelete
  12. Get your hands in the best best convection microwave oven under 15000 design To Cook
    Better, Faster & More Efficiently Make Healthy Dishes Everyday. best microwave convection oven in india

    ReplyDelete

Related Posts Plugin for WordPress, Blogger...