tag:blogger.com,1999:blog-5447607396316157632024-03-18T15:17:49.568+05:30●.. My B.Tech ..● 2K8618.blogspot.com - 2K8CSEComputer Science & Engineering PortalNnhttp://www.blogger.com/profile/09802208823852709501noreply@blogger.comBlogger34613tag:blogger.com,1999:blog-544760739631615763.post-36721188464535560122014-10-09T01:13:00.001+05:302015-12-05T15:43:50.283+05:30Simple Line Chart Using D3 js with Animation, Value points and Tooltip<div dir="ltr" style="text-align: left;" trbidi="on">
<div style="text-align: center;">
<b><span style="font-family: inherit; font-size: x-large;">A Simple Line Chart Using D3 js with Animation, Value points and Tooltip</span></b></div>
<br />
Inspired from great visualizations of data using <b><a href="http://d3js.org/" target="_blank">d3js.org</a>, </b>a simple line chart using d3js with animation, value points and tooltip.<br />
<script src="https://cdn.jsdelivr.net/d3js/3.4.12/d3.min.js" type="text/javascript"></script>
<div id="tooltip" style="background-color: #dddddd; display: none;border-radius: 5px; border: 1px solid #000; height: 20px; padding-left: 15px; padding-right: 15px; padding: 5px; position: absolute; width: 30px;">
<br /></div>
<br />
<br />
<svg height="600" id="my-line-chart" width="600"></svg>
<script>
drawLineChart();
function drawLineChart(){
var chartArea=d3.select('#my-line-chart');
var lineData=[ { x: 0, y: 5},{ x: 20, y: 50 },{ x: 30, y: 45 },{ x: 40, y: 100 },{ x: 50, y: 65 },
{ x: 60, y: 75 },{ x: 70, y: 15 },{ x: 80, y: 85 },{ x: 90, y: 30 },{ x: 100, y: 0 }];
//setting scale functions
var xScale=d3.scale.linear().range([0,500]).domain(d3.extent(lineData, function(d){ return d.x;}));
var yScale=d3.scale.linear().range([500,0]).domain(d3.extent(lineData, function(d){ return d.y;}));
//setting axis functions
xAxis = d3.svg.axis().scale(xScale).tickSize(2);
yAxis = d3.svg.axis().scale(yScale).tickSize(2).ticks(20).orient("left");
//drawing axes
chartArea.append('g').attr('class','x axis').call(xAxis).attr('transform', 'translate(30,510)');
chartArea.append('g').attr('class','y axis').call(yAxis).attr('transform', 'translate(30,10)');
//line path generator function
var linePathGenerator = d3.svg.line().x( function(d){
return xScale(d.x);
}).y(function(d){
return yScale(d.y);
});
//drawing line
var linePath=chartArea.append('path').datum(lineData)
.attr({
'd' : linePathGenerator,
'fill': 'none',
'transform': 'translate(30,10)',
'stroke': 'rgb(9, 112, 184)',
'stroke-width' : 3
});
// animation
var pathLength = linePath.node().getTotalLength();
linePath.attr({
"stroke-dasharray" : pathLength + " " + pathLength,
"stroke-dashoffset" : pathLength
}).transition().duration(3000).ease("linear")
.attr("stroke-dashoffset", 0);
//points
var circlePoints=chartArea.selectAll('circle')
.data(lineData)
.enter()
.append('circle')
.attr({cx : function(d){
return xScale(d.x)+30;
}, cy : function (d){
return yScale(d.y)+10;
}, r :5,
fill : 'silver'
});
// tooltip
var div= d3.select('#tooltip');
circlePoints.on("mouseover", function(d) {
div.transition()
.duration(200).style("display", "block");
div.html(d.y)
.style("left", (d3.event.pageX) + "px")
.style("top", (d3.event.pageY - 28) + "px");
}).on("mouseout", function(d) {
div.transition()
.duration(500)
.style("display", "none");
});
}
</script>
<br />
<pre class="brush:jfx"><!DOCTYPE html>
<html>
<head>
<style>
svg{
background-color : none;
}
.axis{
stroke : #0000001;
fill : silver;
}
#tooltip{
position : absolute;
border : 1px solid #000;
background-color : #ddd;
padding : 5px;
padding-left: 15px;
padding-right : 15px;
border-radius : 5px;
}
</style>
<script src="https://cdn.jsdelivr.net/d3js/3.4.12/d3.min.js" type="text/javascript"></script>
</head>
<body>
<svg id="my-line-chart" width="600" height="600"></svg>
<div id="tooltip"></div>
<script>
drawLineChart();
function drawLineChart(){
var chartArea=d3.select('#my-line-chart');
var lineData=[ { x: 0, y: 5},{ x: 20, y: 50 },{ x: 30, y: 45 },{ x: 40, y: 100 },{ x: 50, y: 65 },
{ x: 60, y: 75 },{ x: 70, y: 15 },{ x: 80, y: 85 },{ x: 90, y: 30 },{ x: 100, y: 0 }];
//setting scale functions
var xScale=d3.scale.linear().range([0,500]).domain(d3.extent(lineData, function(d){ return d.x;}));
var yScale=d3.scale.linear().range([500,0]).domain(d3.extent(lineData, function(d){ return d.y;}));
//setting axis functions
xAxis = d3.svg.axis().scale(xScale).tickSize(2);
yAxis = d3.svg.axis().scale(yScale).tickSize(2).ticks(20).orient("left");
//drawing axes
chartArea.append('g').attr('class','x axis').call(xAxis).attr('transform', 'translate(30,510)');
chartArea.append('g').attr('class','y axis').call(yAxis).attr('transform', 'translate(30,10)');
//line path generator function
var linePathGenerator = d3.svg.line().x( function(d){
return xScale(d.x);
}).y(function(d){
return yScale(d.y);
});
//drawing line
var linePath=chartArea.append('path').datum(lineData)
.attr({
'd' : linePathGenerator,
'fill': 'none',
'transform': 'translate(30,10)',
'stroke': 'rgb(9, 112, 184)',
'stroke-width' : 3
});
// animation
var pathLength = linePath.node().getTotalLength();
linePath.attr({
"stroke-dasharray" : pathLength + " " + pathLength,
"stroke-dashoffset" : pathLength
}).transition().duration(3000).ease("linear")
.attr("stroke-dashoffset", 0);
//points
var circlePoints=chartArea.selectAll('circle')
.data(lineData)
.enter()
.append('circle')
.attr({cx : function(d){
return xScale(d.x)+30;
}, cy : function (d){
return yScale(d.y)+10;
}, r :8,
fill : 'silver'
});
// tooltip
var div= d3.select('#tooltip');
circlePoints.on("mouseover", function(d) {
div.transition()
.duration(200)
.style("opacity", .9);
div.html(d.y)
.style("left", (d3.event.pageX) + "px")
.style("top", (d3.event.pageY - 28) + "px");
}).on("mouseout", function(d) {
div.transition()
.duration(500)
.style("opacity", 0);
});
}
</script>
</body>
</html>
</pre>
</div>Nnhttp://www.blogger.com/profile/09802208823852709501noreply@blogger.com129tag:blogger.com,1999:blog-544760739631615763.post-36492373932900110422013-07-31T07:46:00.002+05:302013-07-31T07:48:02.207+05:30Struts Login Form Example Eclipse MySql Apache Tomcat<div dir="ltr" style="text-align: left;" trbidi="on">
<div style="text-align: center;">
<span style="font-family: Times, Times New Roman, serif; font-size: x-large;">Struts Login Example</span></div>
<div style="text-align: center;">
<br /></div>
<div style="text-align: center;">
<span style="font-family: Verdana, sans-serif; font-size: large;">Struts 1.3 Java Eclipse MySql Apache Tomcat</span></div>
<br />
<b>Project Screenshot:</b><br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh1SbX15KyXy6YEkwax80y6rWAPb6QWfoV15Pro1TaY5WtzIwKmkEHG4X16c1Jm81BjcQXaQzmhBr6cmDTYJSp_qglIo3hgPm2nv-sHxmaiYpvlox3eh_Pz6TRDhuKXEnPWk5kXFiZ57vg/s1600/screenshot.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="126" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh1SbX15KyXy6YEkwax80y6rWAPb6QWfoV15Pro1TaY5WtzIwKmkEHG4X16c1Jm81BjcQXaQzmhBr6cmDTYJSp_qglIo3hgPm2nv-sHxmaiYpvlox3eh_Pz6TRDhuKXEnPWk5kXFiZ57vg/s320/screenshot.JPG" width="320" /></a></div>
<br />
<br />
<b>Project Explorer:</b><br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhGUEKx4ij8R1wokJErMQJzRSA3dQPj0k8DOQ-X2-pqZ6PvyNvB7dbAL37iAzCBBxOSImVT9iKReyeA9vvATKm6wWFzMNtPoQfcn_uJbCLMVcDVwaBl5Hj39eYGxjHKq1lc84cozG3y9cY/s1600/project+explorer.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhGUEKx4ij8R1wokJErMQJzRSA3dQPj0k8DOQ-X2-pqZ6PvyNvB7dbAL37iAzCBBxOSImVT9iKReyeA9vvATKm6wWFzMNtPoQfcn_uJbCLMVcDVwaBl5Hj39eYGxjHKq1lc84cozG3y9cY/s1600/project+explorer.JPG" /></a></div>
<br />
<br />
<b>Source code:</b><br />
<br />
<b style="background-color: white; color: #343333; font-family: Arial, Helvetica, sans-serif; font-size: 12px; line-height: 20px;">struts-config.xml </b><br />
<pre class="brush:jfx"><?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-beans>
<form-bean name="loginForm" type="com.loginexample.forms.LoginForm"/>
</form-beans>
<!-- Action Mappings -->
<action-mappings>
<action name="loginForm" path="/Login" type="com.loginexample.actions.LoginAction" scope="request" input="/Login.jsp">
<forward name="failure" path="/Failure.jsp" redirect="true"/>
<forward name="success" path="/Success.jsp" redirect="true"/>
</action>
</action-mappings>
<!-- Message Source -->
<message-resources parameter="com.loginexample.resources.Application"/>
</struts-config>
</pre>
<b style="background-color: white; color: #343333; font-family: Arial, Helvetica, sans-serif; font-size: 12px; line-height: 20px;"><br /></b>
<b style="background-color: white; color: #343333; font-family: Arial, Helvetica, sans-serif; font-size: 12px; line-height: 20px;">web.xml</b><br />
<pre class="brush:jfx"><?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>
<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>
</pre>
<b style="background-color: white; color: #343333; font-family: Arial, Helvetica, sans-serif; font-size: 12px; line-height: 20px;">Login.jsp</b><br />
<pre class="brush:jfx"><%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@taglib prefix="html" uri="http://struts.apache.org/tags-html"%>
<%@taglib prefix="logic" uri="http://struts.apache.org/tags-logic"%>
<%@taglib prefix="bean" uri="http://struts.apache.org/tags-bean"%>
<!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><bean:message key="loginform.label.title"/></title>
</head>
<body>
<center>
<h1><bean:message key="loginform.label.title"/></h1>
<html:form action="/Login" focus="username">
<table>
<tr><td>
<bean:message key="loginform.label.usernane"/>
</td>
<td>
<html:text property="username"/>
</td>
<td>
<logic:messagesPresent property="username">
<html:messages id="error" property="username">
<font color="red"><bean:write name="error"/></font>
</html:messages>
</logic:messagesPresent>
</td></tr>
<tr><td>
<bean:message key="loginform.label.password"/>
</td>
<td>
<html:password property="paswd"/>
</td>
<td>
<logic:messagesPresent property="paswd">
<html:messages id="error" property="paswd">
<font color="red"><bean:write name="error"/></font>
</html:messages>
</logic:messagesPresent>
</td></tr>
<tr><td></td>
<td align="left">
<html:submit value="Login"/>
<html:reset value="Cancel"/>
</td>
<td>
</td></tr>
</table>
</html:form>
</center>
</body>
</html>
</pre>
<b style="background-color: white; color: #343333; font-family: Arial, Helvetica, sans-serif; font-size: 12px; line-height: 20px;"><br /></b>
<b style="background-color: white; color: #343333; font-family: Arial, Helvetica, sans-serif; font-size: 12px; line-height: 20px;">Success.jsp</b><br />
<pre class="brush:jfx"><%@ 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>Login Successful</title>
</head>
<body>
<h1>Login Successful</h1>
</body>
</html>
</pre>
<b style="background-color: white; color: #343333; font-family: Arial, Helvetica, sans-serif; font-size: 12px; line-height: 20px;"><br /></b>
<b style="background-color: white; color: #343333; font-family: Arial, Helvetica, sans-serif; font-size: 12px; line-height: 20px;">Failure.jsp</b><br />
<pre class="brush:jfx"><%@ 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>Login Failure</title>
</head>
<body>
<h1 style="color: red;">Login Failure</h1>
</body>
</html>
</pre>
<b style="background-color: white; color: #343333; font-family: Arial, Helvetica, sans-serif; font-size: 12px; line-height: 20px;"><br /></b>
<span style="background-color: white; font-size: 12px; line-height: 20px;"><span style="color: #343333; font-family: Arial, Helvetica, sans-serif;"><b>LoginForm.java</b></span></span><br />
<pre class="brush:jfx">package com.loginexample.forms;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
public class LoginForm extends ActionForm {
/**
* nn
*/
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;
}
@Override
public void reset(ActionMapping mapping, HttpServletRequest request) {
this.paswd = null;
}
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
ActionErrors errors = new ActionErrors();
if(username==null || username.length()==0 || username.trim().equals(""))
{
ActionMessage message = new ActionMessage("loginform.username.empty");
errors.add("username", message);
}
else if(!username.matches("^[a-zA-Z0-9 ]+$"))
{
errors.add("username", new ActionMessage("loginform.username.alphabets"));
}
else if(username.length()<3)
{
errors.add("username", new ActionMessage("loginform.username.length"));
}
if(paswd==null||paswd.length()==0||paswd.trim().equals(""))
{
errors.add("paswd", new ActionMessage("loginform.paswd"));
}
return errors;
}
}
</pre>
<span style="background-color: white; font-size: 12px; line-height: 20px;"><span style="color: #343333; font-family: Arial, Helvetica, sans-serif;"><b><br /></b></span></span>
<span style="background-color: white; font-size: 12px; line-height: 20px;"><span style="color: #343333; font-family: Arial, Helvetica, sans-serif;"><b>LoginAction,java</b></span></span><br />
<pre class="brush:jfx">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.LoginForm;
import com.loginexample.services.LoginService;
public class LoginAction extends Action {
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
LoginForm loginForm = (LoginForm)form;
LoginService loginService= new LoginService();
try
{
loginService.checkLogin(loginForm);
return mapping.findForward("success");
}
catch (Exception e)
{
return mapping.findForward("failure");
}
}
}
</pre>
<span style="background-color: white; font-size: 12px; line-height: 20px;"><span style="color: #343333; font-family: Arial, Helvetica, sans-serif;"><b><br /></b></span></span>
<span style="background-color: white; font-size: 12px; line-height: 20px;"><span style="color: #343333; font-family: Arial, Helvetica, sans-serif;"><b>LoginService.java</b></span></span><br />
<pre class="brush:jfx">package com.loginexample.services;
import java.sql.SQLException;
import com.loginexample.dao.impl.LoginDaoImplementation;
import com.loginexample.exceptions.InvalidUserException;
import com.loginexample.forms.LoginForm;
public class LoginService {
public boolean checkLogin(LoginForm loginForm) throws SQLException, InvalidUserException
{
LoginDaoImplementation loginDao= new LoginDaoImplementation();
if(!loginDao.checkLogin(loginForm))
throw new InvalidUserException("Incorrect Username/Password.");
else
return true;
}
}
</pre>
<span style="background-color: white; font-size: 12px; line-height: 20px;"><span style="color: #343333; font-family: Arial, Helvetica, sans-serif;"><b><br /></b></span></span>
<span style="background-color: white; font-size: 12px; line-height: 20px;"><span style="color: #343333; font-family: Arial, Helvetica, sans-serif;"><b>LoginDao.java</b></span></span><br />
<pre class="brush:jfx">package com.loginexample.dao;
import java.sql.SQLException;
import com.loginexample.forms.LoginForm;
public interface LoginDao
{
public boolean checkLogin(LoginForm loginForm) throws SQLException;
}
</pre>
<span style="background-color: white; font-size: 12px; line-height: 20px;"><span style="color: #343333; font-family: Arial, Helvetica, sans-serif;"><b><br /></b></span></span>
<span style="background-color: white; font-size: 12px; line-height: 20px;"><span style="color: #343333; font-family: Arial, Helvetica, sans-serif;"><b>LoginDaoImplementation.java</b></span></span><br />
<pre class="brush:jfx">package com.loginexample.dao.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.loginexample.dao.LoginDao;
import com.loginexample.forms.LoginForm;
import com.loginexample.utilities.DBUtilities;
public class LoginDaoImplementation implements LoginDao
{
public boolean checkLogin(LoginForm loginForm) throws SQLException
{
Connection con=null;
boolean access=false;
try
{
con=DBUtilities.getConnection();
String query= "select * from login where username=? and password=?";
PreparedStatement stmt= con.prepareStatement(query);
stmt.setString(1, loginForm.getUsername());
stmt.setString(2, loginForm.getpaswd());
ResultSet rs= stmt.executeQuery();
if(rs.next())
{
access=true;
}
}
finally
{
DBUtilities.closeConnection(con);
}
return access;
}
}
</pre>
<span style="background-color: white; font-size: 12px; line-height: 20px;"><span style="color: #343333; font-family: Arial, Helvetica, sans-serif;"><b><br /></b></span></span>
<span style="background-color: white; font-size: 12px; line-height: 20px;"><span style="color: #343333; font-family: Arial, Helvetica, sans-serif;"><b>DBUtilities.java</b></span></span><br />
<pre class="brush:jfx">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","");
}
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){}
}
}
}
</pre>
<span style="background-color: white; font-size: 12px; line-height: 20px;"><span style="color: #343333; font-family: Arial, Helvetica, sans-serif;"><b><br /></b></span></span>
<span style="background-color: white; font-size: 12px; line-height: 20px;"><span style="color: #343333; font-family: Arial, Helvetica, sans-serif;"><b>InvalidUserException.java</b></span></span><br />
<pre class="brush:jfx">package com.loginexample.exceptions;
public class InvalidUserException extends Exception
{
/**
*
*/
private static final long serialVersionUID = 1L;
public InvalidUserException(String arg0)
{
super(arg0);
}
}
</pre>
<span style="background-color: white; font-size: 12px; line-height: 20px;"><span style="color: #343333; font-family: Arial, Helvetica, sans-serif;"><b><br /></b></span></span>
<span style="background-color: white; font-size: 12px; line-height: 20px;"><span style="color: #343333; font-family: Arial, Helvetica, sans-serif;"><b>Application.properties</b></span></span><br />
<pre class="brush:jfx">loginform.label.title=Login Form
loginform.label.usernane=Username :
loginform.label.password=Password :
loginform.username.empty=Please enter username
loginform.username.alphabets=Please enter alphabets only
loginform.username.length=Please enter username of length of at least 3 characters
loginform.paswd=Please enter password
</pre>
<b style="background-color: white; color: #343333; font-family: Arial, Helvetica, sans-serif; font-size: 12px; line-height: 20px;"><br /></b>
<b style="background-color: white; color: #343333; font-family: Arial, Helvetica, sans-serif; font-size: 12px; line-height: 20px;"><br />Download: </b><a href="http://www.4shared.com/zip/LkpocX5y/StrutsLoginFormExample.html" target="_blank">StrutsLoginFormExample</a></div>
Nnhttp://www.blogger.com/profile/09802208823852709501noreply@blogger.com29tag:blogger.com,1999:blog-544760739631615763.post-88459274058075502892013-07-20T14:08:00.001+05:302013-07-23T20:17:18.432+05:30Registration Form - Struts 1.3 Example - Eclipse - MySQL - Apache Tomcat<div dir="ltr" style="text-align: left;" trbidi="on">
<div style="text-align: center;">
<h2>
<span style="font-family: Times, Times New Roman, serif; font-size: x-large;">Simple Struts Example: Registration Form</span></h2>
</div>
<div style="text-align: center;">
<h4>
<span style="background-color: white; color: #343333; font-family: Verdana, sans-serif; line-height: 20px;">Struts 1.3 - Java - MySQL- Eclipse - Apache Tomcat</span></h4>
</div>
<span style="background-color: white; color: #343333; font-family: Verdana, sans-serif; font-size: 12px; line-height: 20px; text-align: center;"><br /></span>
<span style="background-color: white; color: #343333; font-family: Verdana, sans-serif; font-size: 12px; line-height: 20px; text-align: center;"><br /></span><br />
<span style="background-color: white; color: #343333; font-family: Verdana, sans-serif; font-size: 12px; line-height: 20px; text-align: center;">Project Screenshots:</span><br />
<span style="background-color: white; color: #343333; font-family: Verdana, sans-serif; font-size: 12px; line-height: 20px; text-align: center;"><br /></span>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhDZxGUHgoqBmSipo2blwGE7Lq9-gnGwPY8B63nSGvdBYq_yMV3tUJ73pSZogzicIBCggzgEeXd6J-5f7J_o_qlRU_H1IxSlNo5B-mwDxfB-w8R7uv7RRERUg4mxDvnFeluQgDkl1IKAVc/s1600/reg.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="182" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhDZxGUHgoqBmSipo2blwGE7Lq9-gnGwPY8B63nSGvdBYq_yMV3tUJ73pSZogzicIBCggzgEeXd6J-5f7J_o_qlRU_H1IxSlNo5B-mwDxfB-w8R7uv7RRERUg4mxDvnFeluQgDkl1IKAVc/s320/reg.JPG" width="320" /></a></div>
<span style="background-color: white; color: #343333; font-family: Verdana, sans-serif; font-size: 12px; line-height: 20px; text-align: center;"><br /></span>
<span style="background-color: white; color: #343333; font-family: Verdana, sans-serif; font-size: 12px; line-height: 20px; text-align: center;"><br /></span>
<div class="separator" style="clear: both; text-align: center;">
</div>
<span style="background-color: white; color: #343333; font-family: Verdana, sans-serif; font-size: 12px; line-height: 20px; text-align: center;">Project Explorer:</span><br />
<span style="background-color: white; color: #343333; font-family: Verdana, sans-serif; font-size: 12px; line-height: 20px; text-align: center;"><br /></span>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhY0TCZdN6V3ThvRXLZV6oUWPzs6j1ZqKeL4-4jL9kioq-lHPZTzV-cwFQcF_EF5RWQI6_CYNCqbaAaXGO7kHrMLy4hmOThDCAfJmb4vwVUOMc2uVM1e7f1uFrUowzttZZP3Ndi2ZTz4_g/s1600/project+explorer.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="320" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhY0TCZdN6V3ThvRXLZV6oUWPzs6j1ZqKeL4-4jL9kioq-lHPZTzV-cwFQcF_EF5RWQI6_CYNCqbaAaXGO7kHrMLy4hmOThDCAfJmb4vwVUOMc2uVM1e7f1uFrUowzttZZP3Ndi2ZTz4_g/s320/project+explorer.JPG" width="153" /></a></div>
<span style="background-color: white; color: #343333; font-family: Verdana, sans-serif; font-size: 12px; line-height: 20px; text-align: center;"><br /></span>
<span style="background-color: white; color: #343333; font-family: Verdana, sans-serif; font-size: 12px; line-height: 20px; text-align: center;"><b>Source code:</b></span><br />
<span style="background-color: white; color: #343333; font-family: Verdana, sans-serif; font-size: 12px; line-height: 20px; text-align: center;"><b><br /></b></span>
<b>struts-config.xml
</b><br />
<br />
<pre class="brush:jfx"><?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>
</pre>
<b><br /></b>
<b>web.xml</b><br />
<pre class="brush:jfx"><?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>
</pre>
<br />
<span style="background-color: white; font-size: 12px; line-height: 20px; text-align: center;"><b>Register.jsp</b></span><br />
<pre class="brush:jfx"><%@ 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>
</pre>
<b>RegistrationForm.java</b><br />
<pre class="brush:jfx">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;
}
}
</pre>
<br />
<b>RegistrationAction.java</b><br />
<pre class="brush:jfx">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");
}
}
}
</pre>
<br />
<b>LoginService.java</b><br />
<pre class="brush:jfx">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;
}
}
</pre>
<br />
<b>LoginDao.java
</b><br />
<pre class="brush:jfx">package com.loginexample.dao;
import java.sql.SQLException;
import com.loginexample.forms.RegistrationForm;
public interface LoginDao
{
public boolean addLogin(RegistrationForm regForm) throws SQLException;
}
</pre>
<br />
<b>LoginDaoImplementation.java</b><br />
<pre class="brush:jfx">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;
}
}
</pre>
<br />
<b>DBUtilities.java</b><br />
<pre class="brush:jfx">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){}
}
}
}
</pre>
<br />
<b>Success.jsp</b><br />
<pre class="brush:jfx"><%@ 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>
</pre>
<br />
<b>Failure.jsp</b><br />
<pre class="brush:jfx"><%@ 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>
</pre>
<br />
<br />
MySql Database<br />
<pre style="background-color: #585454; color: white; width: 70%;">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)
</pre>
<br />
<br />
Download: <a href="http://www.4shared.com/zip/gzN3sl08/StrutsExampleRegistration_1.html" target="_blank">StrutsExampleRegistration</a></div>
Nnhttp://www.blogger.com/profile/09802208823852709501noreply@blogger.com16