You are on page 1of 6

Introduction:

Database Connection Example in Struts 1.3 the following tool are required for run this example JDK 1.5 MyEclipse IDE Server Tomcat 6.0 Struts 1.3 jar file MySql 5.0 Database Server

Directory Structure of Login Example in Struts 1.3 Using MyEclipse IDE

Descriptions:
index.jsp <%@taglib uri="http://struts.apache.org/tags-html" prefix="html" %> <html> <head> <title>Login Form</title> </head> <body> <html:form action="/login" > <table border="1" bordercolor="red">

<tr> <td width="500px" align="center" height="50px" style="background: gray;" valign="middle"> <h3 style="color:lime;">Login Example in Struts 1.3 Using MySql Database</h3></td> </tr> <tr><td align="center" width="250px" height="150px" style="background: gray;"> <table> <tr> <td>User Name</td> <td><html:text name="loginForm" property="name"/></td> </tr> <tr> <td>Password</td> <td> <html:password name="loginForm" property="password"/></td> </tr> <tr> <td colspan="2" align="right"> <html:submit value="login"/></td> </tr> </table></td></tr> </table> </html:form> </body> </html> web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <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> <init-param> <param-name>debug</param-name> <param-value>3</param-value> </init-param> <init-param> <param-name>detail</param-name> <param-value>3</param-value> </init-param> <load-on-startup>0</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> 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="loginForm" type="org.r4r.struts.LoginForm"/> </form-beans> <global-exceptions /> <global-forwards> <forward name="login" path="/login.do"/> </global-forwards> <action-mappings> <action path="/login" type="org.r4r.struts.LoginAction" name="loginForm"> <forward name="success" path="/success.jsp"/> <forward name="error" path="/error.jsp"/> </action> </action-mappings> <message-resources parameter="org.r4r.struts.ApplicationResources" /> </struts-config> LoginForm.java package org.r4r.struts; import org.apache.struts.action.ActionForm; @SuppressWarnings("serial") public class LoginForm extends ActionForm { private String name; private String password; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } } LoginAction.java package org.r4r.struts; import import import import import import javax.servlet.http.HttpServletRequest; javax.servlet.http.HttpServletResponse; org.apache.struts.action.Action; org.apache.struts.action.ActionForm; org.apache.struts.action.ActionForward; org.apache.struts.action.ActionMapping;

public class LoginAction extends Action { public ActionForward execute(ActionMapping mapping,ActionForm form, HttpServletRequest request,HttpServletResponse response){

LoginForm loginForm=(LoginForm)form; DAO dao=new DAO(); if(dao.find(loginForm.getName(), loginForm.getPassword())){ return mapping.findForward("success"); }else{ return mapping.findForward("error"); } } } DAO.java package org.r4r.struts; import import import import java.sql.Connection; java.sql.DriverManager; java.sql.PreparedStatement; java.sql.ResultSet;

public class DAO { public boolean find(String name,String password){ try{ Class.forName("com.mysql.jdbc.Driver"); Connection con=DriverManager.getConnection ("jdbc:mysql://localhost/test","root","root"); PreparedStatement stmt=con.prepareStatement ("select * from user where name=? and password=?"); stmt.setString(1, name); stmt.setString(2, password); ResultSet rset=stmt.executeQuery(); while(rset.next()){ return true; } }catch(Exception e){ System.out.println(e); } return false; } } success.jsp <%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %> <html> <head> <title>Login Form</title> </head> <body> <table border="1" bordercolor="red"> <tr> <td width="500px" align="center" height="50px" style="background: gray;" valign="middle"> <h3 style="color:lime;"> Login Example in Struts 1.3 Using MySql Database</h3></td> </tr> <tr><td align="center" width="250px" height="150px" style="background: gray;"> <table>

<tr> <td>Welcome, <bean:write name="loginForm" property="name"/> <br/><br/><a href="index.jsp">Logout</a> </td> </tr> </table></td></tr> </table> </body> </html> error.jsp <%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %> <html> <head> <title>Login Form</title> </head> <body> <table border="1" bordercolor="red"> <tr> <td width="500px" align="center" height="50px" style="background: gray;color:red;" valign="middle"> <h3>Sorry, <bean:write name="loginForm" property="name"/></h3><h4> You are not valid user please try again.</h4></td> </tr> <tr><td align="center" width="250px" height="150px" style="background: gray;"> <table> <tr> <td> <br/><br/><jsp:include page="index.jsp"></jsp:include> </td> </tr> </table></td></tr> </table> </body> </html> Output

You might also like