Log4j Example For Login Form Check using Struts and Mysql Database:

Data in database: select * from login




Step1:  Develop login.jsp file

<html>
    <body>
         <%          
String s=request.getParameter("action");
if("invalid".equals(s))
{
out.println("<font color='red'>Invalid UserName Or Password</font>");
}
%>
<form action="./login.do">
     <table>
            <tr>
                <td>UserName:</td>
                <td><input type="text" name="uname"></input></td>
            </tr>
             <tr>
                <td>PassWord</td>
                <td><input type="password" name="pwd"></input></td>
            </tr>
             <tr>
                 <td colspan="2"><input type="submit" value="Login"/>
                 </td>
            </tr>
        </table></form> </body></html>

Step2: Develop a Action Class and Capturing a values from Form Using ActionForm and Create a Module class Object and call that maethod.If user name and password is correct,controller goes to success.jsp page ,otherwise goes to login.jsp page and configure in struts-config.xml file


package login;
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 LoginAction extends org.apache.struts.action.Action {
@Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
             System.out.println("controler");

//        String uname=request.getParameter("uname");
//         String pwd=request.getParameter("pwd");
         
                 LoginActionForm l=(LoginActionForm)form;
                  String uname=l.getUname();
                 String pwd= l.getPwd();
                
                 System.out.println("hasi ashok?:"+uname);
request.setAttribute("uname",uname);
                Module m=new Module();
             String result=m.login(uname,pwd);
             System.out.println(pwd);
        return mapping.findForward(result);
    }
}

struts-config.xml
<action-mappings>
        <action path="/login"  name="l" type="login.LoginAction">
            <forward name="success" path="/success.jsp"/>
            <forward name="invalid" path="/login.jsp?action=invalid"/>
        </action>
            </action-mappings>
    

Step3: This is Struts FormBean class and Configured in struts-config.xml file

package login;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
public class LoginActionForm extends org.apache.struts.action.ActionForm {
  String uname;
  String pwd;
    public String getPwd() {
        return pwd;
    }
    public void setPwd(String pwd) {
        this.pwd = pwd;
    }
    public String getUname() {
        return uname;
    }
    public void setUname(String uname) {
        this.uname = uname;
    }
}

struts-config.xml
<form-beans>
        <form-bean name="l" type="login.LoginActionForm"/>
    </form-beans>


Step4:  In this we can get the values from log in Form and get the data from Data Base and Check user Authorised or Not

package login;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import org.apache.log4j.Logger;
public  class Module {
    Logger logger=Logger.getLogger(Module.class);
            String result="invalid";
        Dao d=new Dao();
    public String login(String u,String p)
    {
        try{
        logger.debug("moduler layer===============================");
       Connection con=d.getConn();
        //System.out.println("con"+con);
    String sql="select * from login where uname="+"'"+u+"'"+"and"+" passward="+"'"+p+"'";
    PreparedStatement ps=con.prepareStatement("select * from login");
    System.out.println(sql);
        ResultSet rs=ps.executeQuery();
        //System.out.println("rs"+rs);
       while(rs.next()){
        if(u.equals(rs.getString("uname"))&& p.equals(rs.getString("passward")))
        {
           // System.out.println("if block of mod"+rs.getString("uname"));
 result="success";       
        }
        }
        }
        catch(Exception e){
        logger.error("error of from Module Class catch block:");
        }
        finally{d.closeConnection();}
        return result;
    } 
}

Step5: This Dao class used for getting the Connection from Database..(like ...DBUtil)

package login;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
class Dao {
    public static Connection con=null;
    public static Connection getConn()
    {
        try{
                                System.out.println("try of dao");

    Class.forName("com.mysql.jdbc.Driver");
     con= DriverManager.getConnection("jdbc:mysql://localhost:3306/fill", "root", "root");
                                    System.out.println("try of dao2");
        }
    catch(Exception e)
    {
    }    return con;
}
     public void closeConnection() {
        try {
            con.close();
        } catch (SQLException s) {
            System.out.println("IN DAO class closeConnection ,SQLEXCEPTION occurred");
            s.printStackTrace();
        }
    }
}


Step6:  This is for  success.jsp  is executed ,when user eneterd Valid Username and  Password .


Hi! <font align="right" color="red" size="5"><b><%=request.getAttribute("uname")%></b> </fonT><br><br>
<center><h3>This is Your Home Page</h3></center>

Step7: log4j.properties File

#define the console appender
log4j.appender.consoleAppender = org.apache.log4j.ConsoleAppender
# now define the layout for the appender
log4j.appender.consoleAppender.layout = org.apache.log4j.PatternLayout
log4j.appender.consoleAppender.layout.ConversionPattern=%t %-5p %c{3} - %m%n
log4j.appender.rollingFile=org.apache.log4j.RollingFileAppender
#log4j.appender.rollingFile.File=./webapps/ashokLogs/testlogs/ashok.log
log4j.appender.rollingFile.File=f:/moulali.txt
log4j.appender.rollingFile.MaxFileSize=10MB
log4j.appender.rollingFile.MaxBackupIndex=2
log4j.appender.rollingFile.layout = org.apache.log4j.PatternLayout
log4j.appender.rollingFile.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
# now map our console appender as a root logger, means all log messages will go to this appender
#for console printing
#log4j.rootLogger = DEBUG, consoleAppender  
#for file printing
log4j.rootLogger = DEBUG, rollingFile  

Output: 

User Entered InValid UserName or Password









Download Net Beans War File  DOWNLOAD

Note: After Downloading War File,Extracting For War File using Command

->

1 comments: