About Me

My photo
Talk to me... you will know

Tuesday, September 06, 2011

Sample Application using the frameworks of struts2, hibernate, spring and log4j


//User.java

package com.action;
import javax.servlet.http.HttpServletRequest;


import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;
import org.apache.struts2.interceptor.ServletRequestAware;
import com.opensymphony.xwork2.Action;


public class User implements Action,ServletRequestAware
{
static Logger l=Logger.getLogger("com.action.User");
HttpServletRequest request;
int id;
String userName, passWord;

public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
public String getUserName()
{
return userName;
}
public void setUserName(String userName)
{
this.userName = userName;
}
public String getPassWord()
{
return passWord;
}
public void setPassWord(String passWord)
{
this.passWord = passWord;
}
public String toString()
{
BasicConfigurator.configure();
l.info("Return the values as a string");
return "Username : "+userName+" , Password : "+passWord;

}
@Override
public String execute() throws Exception
{
BasicConfigurator.configure();
boolean userVal=request.getParameter("userName").toString().equals("");
if(userVal)
{
l.info("Guest User logging");

}
else if(!userVal&&request.getParameter("passWord").toString().equals(""))
{
l.error("User trying to log without password");
return INPUT;
}

l.info("successful login");
return SUCCESS;
}
@Override
public void setServletRequest(HttpServletRequest arg0)
{
request=arg0;

}
}


//applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--Bean 1-->
<bean id="user" class="com.action.User">
<property name="id" value="1"/>
<property name="userName" value="Guest"/>
<property name="passWord" value="No Password Required"/>
</bean>
</beans>



//home.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@taglib prefix="s" uri="/struts-tags"%>
    <%@ page import="org.springframework.context.support.ClassPathXmlApplicationContext,org.springframework.beans.factory.BeanFactory,org.apache.log4j.BasicConfigurator,com.action.*,org.apache.log4j.Logger" %>
    <%@page import="org.hibernate.cfg.Configuration,org.hibernate.Transaction,org.hibernate.SessionFactory,org.hibernate.Session" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">


<%@page import="org.apache.struts2.interceptor.SessionAware"%><html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Logger Home</title>
</head>
<body>
<%
Logger l=Logger.getLogger("com.action.User");
BeanFactory beanfactory = new ClassPathXmlApplicationContext("applicationContext.xml");
User u=(User)beanfactory.getBean("user");
BasicConfigurator.configure();
boolean flag=request.getParameter("userName").toString().equals("");
if(flag==false)
{
l.info("setting new username and password");
u.setUserName(request.getParameter("userName").toString());
u.setPassWord(request.getParameter("passWord").toString());
}
else
{
l.info("guest logged in");
/*u.setUserName(request.getAttribute("userName").toString());
u.setPassWord("No password required");*/
}
SessionFactory sf=new Configuration().configure().buildSessionFactory();
Session ses= sf.openSession();
Transaction tr= ses.beginTransaction();
ses.save(u);
tr.commit();
out.println(u.toString());

%>
</body>
</html>


//login.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@taglib prefix="s"  uri="/struts-tags"%>
<!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>MyLogger</title>
</head>
<body>
<s:form action="log" >
<s:textfield name="userName" label="Username:"/>
<s:password name= "passWord" label="Password"/>
<s:submit value="Submit"/>
</s:form>
</body>
</html>

//struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd ">
<struts>
<package name="logger" extends="struts-default">
<action name="log" class="com.action.User">
<result name="input">login.jsp</result>
<result>home.jsp</result>
</action>
</package>
</struts>


//user.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//hibernate/hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.action.User" table="Logger">
<id name="id" column="ID">
<generator class="increment" />
</id>
<property name="userName" column="UserName"/>
<property name="passWord" column ="PassWord"/>
</class>

</hibernate-mapping>


//hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//hibernate/hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.action.User" table="Logger">
<id name="id" column="ID">
<generator class="increment" />
</id>
<property name="userName" column="UserName"/>
<property name="passWord" column ="PassWord"/>
</class>

</hibernate-mapping>

//log4j.properties

log4j.appender.rollingFile=org.apache.log4j.RollingFileAppender
log4j.appender.User=org.apache.log4j.RollingFileAppender
log4j.appender.rollingFile.File=C:/appl.log
log4j.appender.rollingFile.MaxFileSize=2MB
log4j.appender.rollingFile.MaxBackupIndex=2
log4j.appender.rollingFile.layout = org.apache.log4j.PatternLayout
log4j.appender.rollingFile.layout.ConversionPattern=*Calling from line no %L* on Date: [%d] with Priority:(%p) Filename: %F At Thread: %t Using Class : %c from Method: {%M}- giving Message : %m%n
log4j.appender.User.File=C:/Userlog.log
log4j.appender.User.MaxFileSize=2MB
log4j.appender.User.MaxBackupIndex=2
log4j.appender.User.layout = org.apache.log4j.PatternLayout
log4j.appender.User.layout.ConversionPattern=*Calling from line no %L* on Date: [%d] with Priority:(%p) Filename: %F At Thread: %t Using Class : %c from Method: {%M}- giving Message : %m%n
log4j.category.com.action.User = INFO, User
log4j.rootLogger = INFO, rollingFile

No comments:

Post a Comment

There was an error in this gadget