0% found this document useful (0 votes)
65 views

JDK 1.5 Myeclipse Ide Server Tomcat 6.0 Struts 1.3 Jar File Mysql 5.0 Database Server

This document describes a login example using Struts 1.3 that connects to a MySQL database. It includes the directory structure, configuration files like web.xml and struts-config.xml, Java classes for the form bean, action, and DAO, and JSP pages for the login form, success page, and error page. The action class uses a DAO to check the username and password against the database and forwards to success.jsp or error.jsp accordingly.

Uploaded by

Ankit Jain
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views

JDK 1.5 Myeclipse Ide Server Tomcat 6.0 Struts 1.3 Jar File Mysql 5.0 Database Server

This document describes a login example using Struts 1.3 that connects to a MySQL database. It includes the directory structure, configuration files like web.xml and struts-config.xml, Java classes for the form bean, action, and DAO, and JSP pages for the login form, success page, and error page. The action class uses a DAO to check the username and password against the database and forwards to success.jsp or error.jsp accordingly.

Uploaded by

Ankit Jain
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 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