Struts 2
struts-config.xml
<?xml version = '1.0' encoding = 'windows-1252'?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
<action-mappings>
<action path="/register1"
type="strutsproject2.RegisterAction">
<forward name="emptyUserName" path="/emptyusername.jsp"/>
<forward name="emptyPassword" path="/emptypassword.jsp"/>
<forward name="tooManyMatches" path="/tomanymatches.jsp"/>
<forward name="illegalUsernamePassword"
path="/illegalentry.jsp"/>
<forward name="notStudent" path="/notstudent.jsp"/>
<forward name="notUser" path="/notuser.jsp"/>
<forward name="success" path="/success.jsp"/>
</action>
</action-mappings>
</struts-config>
register.jsp
<HTML>
<HEAD><TITLE>Login</TITLE></HEAD>
<BODY>
<H1
align="center">
Enter your login
</H1>
<FORM
ACTION="register1.do" METHOD="POST">
Username: <INPUT TYPE="TEXT"
NAME="username">
<BR> Password: <INPUT
TYPE="PASSWORD" NAME="password">
<BR> <INPUT TYPE="SUBMIT"
VALUE="Log in">
</FORM>
</BODY>
</HTML>
RegisterAction.java
//
An Action class to handle Requests
package strutsproject2;
import org.apache.struts.action.*;
import javax.servlet.http.*;
import java.sql.*;
public class RegisterAction extends Action
{
public ActionForward execute(ActionMapping
mapping, ActionForm form,
HttpServletRequest request,
HttpServletResponse
response)
throws Exception
{
PreparedStatement
stmt;
PreparedStatement
stmt2;
PreparedStatement
stmt3;
String username = request.getParameter("username");
if (username ==
null || username.trim().equals(""))
{
return (mapping.findForward("emptyUserName"));
}
String password = request.getParameter("password");
if (password ==
null || password.trim().equals(""))
{
return (mapping.findForward("emptyPassword"));
}
String url =
"jdbc:odbc:registrar";
Connection con;
String query =
"select COUNT(*)
as MATCHES from USERS where USERID like ? " +
"
and PASSWORD like ?;";
String query2 =
"select
COUNT(*) from USERROLES where ROLEID = 1 and " +
"
USERID like ?;";
String query3 =
"select COUNT(*)
as MATCHES from USERS where USERID like ? ";
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(java.lang.ClassNotFoundException e)
{
System.err.print("ClassNotFoundException:
");
System.err.println(e.getMessage());
}
try
{
con = DriverManager.getConnection(url,
"", "");
stmt3 = con.prepareStatement(query3);
stmt3.setString(1,
username);
ResultSet
rs3 = stmt3.executeQuery();
rs3.next();
int
matches = rs3.getInt(1);
if (matches !=
1)
return (mapping.findForward("notUser"));
stmt = con.prepareStatement(query);
stmt.setString(1, username);
stmt.setString(2, password);
ResultSet rs = stmt.executeQuery();
rs.next();
int
match_count = rs.getInt("MATCHES");
if (match_count > 1)
return (mapping.findForward("tooManyMatches"));
else if (match_count == 0)
return (mapping.findForward("illegalUsernamePassword"));
else
{
stmt2 = con.prepareStatement(query2);
stmt2.setString(1,
username);
ResultSet
rs2 = stmt2.executeQuery();
rs2.next();
matches
= rs2.getInt(1);
if
(matches != 1)
return
(mapping.findForward("notStudent"));
}
}
catch(SQLException ex)
{
System.err.println("SQLException: " + ex.getMessage());
}
return (mapping.findForward("success"));
}
}
emptyusername.jsp
<HTML>
<HEAD>
<TITLE>No Username</TITLE>
</HEAD>
<BODY>
<DIV align="center">
<H1>No Username</H1>
You forgot to enter the username.
<A HREF="register.jsp">Try
again</A>.
</DIV>
</BODY>
</HTML>
emptypassword.jsp
<HTML>
<HEAD>
<TITLE>No Password</TITLE>
</HEAD>
<BODY>
<DIV align="center">
<H1>No Password</H1>
You forgot to enter the password.
<A HREF="register.jsp">Try
again</A>.
</DIV>
</BODY>
</HTML>
toomanymatches.jsp
<HTML>
<HEAD>
<TITLE>Too
many matches</TITLE>
</HEAD>
<BODY>
<DIV align="center">
<H1>
Too Many Matches
</h1>
Bad primary key.
<A HREF="register.jsp">Try
again</A>.
</DIV>
</BODY>
</HTML>
illegalentry.jsp
<HTML>
<HEAD>
<TITLE>Bad entry</TITLE>
</HEAD>
<BODY>
<DIV align="center">
<H1>
Bad Password/Username
</h1>
No match for username and password.
<A HREF="register.jsp">Try
again</A>.
</DIV>
</BODY>
</HTML>
notstudent.jsp
<HTML>
<HEAD>
<TITLE>Not a
student</TITLE>
</HEAD>
<BODY>
<DIV align="center">
<H1>
Not a student!
</h1>
Not a student.
<A HREF="register.jsp">Try
again</A>.
</DIV>
</BODY>
</HTML>
notuser.jsp
<HTML>
<HEAD>
<TITLE>Not a
user</TITLE>
</HEAD>
<BODY>
<DIV align="center">
<H1>
Not a user!
</h1>
Not a user.
<A HREF="register.jsp">Try
again</A>.
</DIV>
</BODY>
</HTML>
success.jsp
<HTML>
<HEAD><TITLE>Success</TITLE></HEAD>
<BODY>
<H1
align="center">
You have logged on successfully.
</H1>
Congratulations
</BODY>
</HTML>