Anda di halaman 1dari 16

package beans;

import dao.ExamDAO;
import java.util.ArrayList;

public class ExamBean {


public static int NOQ = 5;
public static int TOTAL = 15;

public static ArrayList<Question>


getQuestions() {
return ExamDAO.getQuestions();
}

public static int


processResult(ArrayList<Question> questions) {
int count = 0;
for(Question q : questions) {
if
(q.getAnswer().equals(q.getCans()))
count ++;
}
return count;
}

public static ArrayList<Examination>


getExamsHistory(String uname) {
return ExamDAO.getExamsHistory(uname);
}

public static void storeResult(String


uname,int score) {
ExamDAO.storeResults(uname,score);
}
}
package beans;

public class Examination {

public Examination(String examid, String dexam,


String score) {
this.examid = examid;
this.dexam = dexam;
this.score = score;
}
private String examid, dexam,score;

public String getDexam() {


return dexam;
}

public void setDexam(String dexam) {


this.dexam = dexam;
}

public String getExamid() {


return examid;
}

public void setExamid(String examid) {


this.examid = examid;
}

public String getScore() {


return score;
}

public void setScore(String score) {


this.score = score;
}

}
package beans;

public class Question {


private String qid, question, ans1, ans2, ans3,
ans4, cans, answer;

public String getAnswer() {


return answer;
}

public void setAnswer(String answer) {


this.answer = answer;
}

public String getAns1() {


return ans1;
}

public void setAns1(String ans1) {


this.ans1 = ans1;
}

public String getAns2() {


return ans2;
}

public void setAns2(String ans2) {


this.ans2 = ans2;
}

public String getAns3() {


return ans3;
}

public void setAns3(String ans3) {


this.ans3 = ans3;
}

public String getAns4() {


return ans4;
}
public void setAns4(String ans4) {
this.ans4 = ans4;
}
public String getCans() {
return cans;
}
public void setCans(String cans) {
this.cans = cans;
}
public String getQid() {
return qid;
}
public void setQid(String qid) {
this.qid = qid;
}
public String getQuestion() {
return question;
}
public void setQuestion(String question) {
this.question = question;
}
public Question(String qid, String question,
String ans1, String ans2, String ans3, String ans4,
String cans) {
this.qid = qid;
this.question = question;
this.ans1 = ans1;
this.ans2 = ans2;
this.ans3 = ans3;
this.ans4 = ans4;
this.cans = cans;
}

public Question() {
}

}
package beans;

import dao.UserDAO;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class UserBean {

private String uname = "", pwd = "", email,


dor, newpwd;

public String getNewpwd() {


return newpwd;
}

public void setNewpwd(String newpwd) {


this.newpwd = newpwd;
}

public String getDor() {


return dor;
}

public void setDor(String dor) {


this.dor = dor;
}

public String getEmail() {


return email;
}

public void setEmail(String email) {


this.email = email;
}
public String getPwd() {
return pwd;
}

public void setPwd(String pwd) {


this.pwd = pwd;
}

public String getUname() {


return uname;
}

public void setUname(String uname) {


this.uname = uname;
}

public boolean register() {


return UserDAO.register((uname), pwd,
email);
}

public boolean login() {


return UserDAO.login(uname, pwd);
}

public boolean changePassword() {


return UserDAO.changePassword(uname, pwd,
newpwd);
}

public boolean recoverPassword() {


String password =
UserDAO.getPassword(email);
if (password == null) {
return false;
}
// send mail to the user
try {
String from = "admin@homepc.com";
Properties props =
System.getProperties();
Session session =
Session.getDefaultInstance(props, null);
// construct the message
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));

msg.setRecipient(Message.RecipientType.TO, new
InternetAddress(email));
msg.setDataHandler(new DataHandler(new
String("Dear Member<p/>Please use the following
password to login again. <p>Password : " + password
+ "<p>Webmaster<br/>javaexam.com"),
"text/html"));
msg.setSubject("Password Recovery");
// send message
Transport.send(msg);
return true;
}
catch (Exception ex) {
System.out.println(ex.getMessage());
return false;
}
}
}
package dao;

import java.sql.Connection;
import java.sql.DriverManager;

public class DBUtil {

public static Connection getConnection() {


try {

Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con =
DriverManager.getConnection("jdbc:oracle:thin:@loca
lhost:1521:xe",
"javaexam","javaexam");
return con;
}
catch(Exception ex) {
System.out.println(ex.getMessage());
return null;
}
}

public static void close(Connection con) {


try {
con.close();
}
catch(Exception ex) {
}
}
}
package dao;

import beans.ExamBean;
import beans.Examination;
import beans.Question;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

public class ExamDAO {


public static ArrayList<Question>
getQuestions() {
// create an array of random numbers based
on the number of question we have in the database.
int[] qnos = new int[ExamBean.NOQ];
int qno, i = 0;
while (i < 5) {
qno = (int) Math.round(Math.random() *
ExamBean.TOTAL);
boolean found = false;
for (int n : qnos) {
if (n == qno) {
found = true;
}
}
if (!found) {
qnos[i] = qno;
i++;
}
}
for (int n : qnos) {
System.out.println(n);
}

Connection con = null;


PreparedStatement ps = null;
try {
con = DBUtil.getConnection();
StringBuffer qs = new StringBuffer("");
for (int n : qnos) {
qs.append(n + ",");
}

qs.delete(qs.length() - 1,
qs.length()); // delete extra ,
String query = "select * from questions
where qid in (" + qs + ")";
System.out.println(query);
ps = con.prepareStatement(query);
ResultSet rs = ps.executeQuery();

ArrayList<Question> questions = new


ArrayList<Question>();
while (rs.next()) {
Question q = new
Question(rs.getString("qid"),
rs.getString("question"), rs.getString("ans1"),
rs.getString("ans2"),
rs.getString("ans3"),
rs.getString("ans4"), rs.getString("cans"));
questions.add(q);
System.out.println("Questions id "
+ q.getQid());
}
return questions;

} catch (Exception ex) {


System.out.println(ex.getMessage());
return null;
} finally {
DBUtil.close(con);
}
} // end of getQuestions()
public static ArrayList<Examination>
getExamsHistory (String uname) {
Connection con = null;
PreparedStatement ps = null;
try {
con = DBUtil.getConnection();
ps = con.prepareStatement("select *
from exams where uname = ?");
ps.setString(1,uname);
ResultSet rs = ps.executeQuery();
ArrayList<Examination> exams = new
ArrayList<Examination>();
while (rs.next()) {
Examination e= new
Examination(rs.getString("examid"),
rs.getString("dexam"), rs.getString("score"));
exams.add(e);
}
return exams;
} catch (Exception ex) {
System.out.println(ex.getMessage());
return null;
} finally {
DBUtil.close(con);
}
} // end of getExamsHistory

public static void storeResults(String


uname, int score) {
Connection con = null;
PreparedStatement ps = null;
try {
con = DBUtil.getConnection();
ps = con.prepareStatement("insert into
exams values( (select nvl( max(examid),0) + 1 from
exams) ,?,sysdate,?)");
ps.setString(1,uname);
ps.setInt(2,score);
ps.executeUpdate();
} catch (Exception ex) {
System.out.println(ex.getMessage());
} finally {
DBUtil.close(con);
}
} // end of storeResults
}
package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class UserDAO {

public static boolean register(String uname,


String pwd, String email) {
Connection con = null;
PreparedStatement ps = null;
try {
con = DBUtil.getConnection();
ps = con.prepareStatement("insert into
users values(?,?,?, sysdate)");
ps.setString(1, uname);
ps.setString(2, pwd);
ps.setString(3, email);
if (ps.executeUpdate() == 1) {
return true;
} else {
return false;
}
} catch (Exception ex) {
System.out.println(ex.getMessage());
return false;
} finally {
DBUtil.close(con);
}
}

public static boolean login(String uname,


String pwd) {
Connection con = null;
PreparedStatement ps = null;
try {
con = DBUtil.getConnection();
ps = con.prepareStatement("select *
from users where uname = ? and pwd = ?");
ps.setString(1, uname);
ps.setString(2, pwd);
ResultSet rs = ps.executeQuery();
if (rs.next()) // found
{
return true;
} else {
return false;
}
} catch (Exception ex) {
System.out.println(ex.getMessage());
return false;
} finally {
DBUtil.close(con);
}
}

public static boolean changePassword(String


uname, String pwd, String newpwd) {
Connection con = null;
PreparedStatement ps = null;
try {
con = DBUtil.getConnection();
ps = con.prepareStatement("update users
set pwd = ? where uname = ? and pwd = ?");
ps.setString(1, newpwd);
ps.setString(2, uname);
ps.setString(3, pwd);
if (ps.executeUpdate() == 1) {
return true;
} else {
return false;
}
} catch (Exception ex) {
System.out.println(ex.getMessage());
return false;
} finally {
DBUtil.close(con);
}
}
public static String getPassword(String email)
{
Connection con = null;
PreparedStatement ps = null;
try {
con = DBUtil.getConnection();
ps = con.prepareStatement("select pwd
from users where email = ?");
ps.setString(1,email);
ResultSet rs = ps.executeQuery();
if ( rs.next())
return rs.getString(1);
else
return null;
} catch (Exception ex) {
System.out.println(ex.getMessage());
return null;
} finally {
DBUtil.close(con);
}

}
}
package filter;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class AuthFilter implements Filter {


private FilterConfig filterConfig = null;
public AuthFilter() {
}
public void doFilter(ServletRequest request,
ServletResponse response,
FilterChain chain)
throws IOException, ServletException {
try {

// check whether session variable is


set
HttpServletRequest req =
(HttpServletRequest) request;
HttpServletResponse res =
(HttpServletResponse) response;
HttpSession ses =
req.getSession(false);
if ( ses != null &&
ses.getAttribute("uname") != null ||
req.getRequestURI().indexOf("/all") >= 0 ) // OK.
User Logged in so allow this
chain.doFilter(request, response);
else

res.sendRedirect(req.getContextPath() +
"/all/login.jsp"); // Anonymous user. Redirect to
login page
}
catch(Throwable t) {
System.out.println( t.getMessage());
}
}

public FilterConfig getFilterConfig() {


return (this.filterConfig);
}
public void setFilterConfig(FilterConfig
filterConfig) {
this.filterConfig = filterConfig;
}
public void destroy() {
}
public void init(FilterConfig filterConfig) {
this.filterConfig = filterConfig;
}
}

Anda mungkin juga menyukai