Your browser (Internet Explorer 6) is out of date. It has known security flaws and may not display all features of this and other websites. Learn how to update your browser.
X
Post

Lab Excercise

Lab Excercise 1 : Shape

Lab Exercise 1 : answer

Post

Timer quiz

timer quiz ni berfungsi untuk korang buat countdown timer dalam sesuatu quiz. sebab quiz kalau takde timer bukan quiz namenye. So timer ni berfungsi terus mengira walaupun pengguna tu bertukar ke next page sekalipun. jadi timer tu akan static lah. sebelum ni aku dah berjaya buat, tapi untuk satu page jer lah.. tukar ke page lain timer pun terus jadi pening.

last2, hari ni berjaya juga selepas bertungkus lumus hampir sbulan mencari cara macam mana untuk buat timer quiz dalam jsp.

so duk pikir-pikir then dapat jugak idea. iaitu aku gunakan <iframe> tag yang mana quiz module tu aku letakkan dalam iframe dan timer tu aku letak dalam parent. so bawah ni coding setelah penat2 mencari

<%@ page contentType=”text/html; charset=utf-8″ language=”java” import=”java.sql.*” errorPage=”" %>
<jsp:include page=”include/Blankheader.jsp” />
<jsp:useBean id=”data” />
<jsp:useBean id=”test” scope=”session”/>
<jsp:useBean id=”temuduga” scope=”session”/>
<jsp:useBean id=”user” scope=”session”/>
<Script Language=”JavaScript”>

/*    Script By hafriz

http://mohdhafriz.com

Please Keep The Credit Above
No Copyrights but be fair
*/
function dis(mins,secs) {
var disp;
if(mins <= 9) {
disp = ” 0″;
} else {
disp = ” “;
}
disp += mins + “:”;
if(secs <= 9) {
disp += “0″ + secs;
} else {
disp += secs;
}
return(disp);
}

function display(){
rtime=etime-ctime;
if (rtime>60)
m=parseInt(rtime/60);
else{
m=0;
}
s=parseInt(rtime-m*60);
if(s<10)
s=”0″+s
document.cd.disp.value = dis(m,s);
window.status=”Time Remaining :  “+m+”:”+s
window.setTimeout(“checktime()”,1000)
}

function settimes(){
alert(“You have 20 minutes time !”)
var time= new Date();
hours= time.getHours();
mins= time.getMinutes();
secs= time.getSeconds();
etime=hours*3600+mins*60+secs;
etime+=3600;  //letak masa korng. cth 20minit = 1200, 60 minit 3600
checktime();
}

function checktime(){
var time= new Date();
hours= time.getHours();
mins= time.getMinutes();
secs= time.getSeconds();
ctime=hours*3600+mins*60+secs
if(ctime>=etime){
expired();
}
else
display();
}

function expired(){
alert(“Test Ended”);
location.href=”keputusan.jsp”;  //Put here the next page
}
</Script>
<style type=”text/css”>
#txt {
border:none;
font-family:verdana;
font-size:16pt;
font-weight:bold;
border-right-color:#FFFFFF;
float:left;
position:absolute;top:5px;left:5px;height:22px;padding:5px;font-size:16pt;background-color:#000000;color:#ffffff;text-align:center
}

<!–
.visibleDiv, #topLeft, #topRight, #bottomLeft, #bottomRight
{
position: fixed;
width: 150px;
border: solid 1px #e1e1e1;
vertical-align: middle;
background: #ffdab9;
text-align: center;
}

#topLeft
{
top: 10px;
left: 10px;
}

#topRight
{
top: 10px;
right: 10px;
}

#bottomLeft
{
bottom: 10px;
left: 10px;
}

#bottomRight
{
bottom: 10px;
right: 10px;
}
//–>

</style>

//bila page bukak terus timer start
<body onLoad=”settimes()”>
//paparan untuk countdown
<div id=”topLeft”>
<form name=”cd”>
<input id=”txt” readonly=”true” type=”text” value=”10:00″ border=”0″ name=”disp”>
</form></div>
//iframe kat sini
<iframe src=”quiz.jsp” frameborder=”0″ scrolling=”no” width=”750″ height=”1000″></iframe>

</body>
</html>

Post

sending mail using jsp

setelah seharian suntuk buat test itu ini. akhirnya berjaya aku buat automatic mailing script untuk email kepada user menggunakan jsp. walaupun banyak kena lakukan tapi pas berjaya tu puas hati.

so step-step nye

1st download javamail dan juga java activation framework

2nd. korang extract dan korang copy paste ke dalam folder JAVA dalam program file

3rd buat CLASSPATH kat environment windows korang kalau tak reti camne ikut tutorial dari java api sendiri

then seterusnya, ni part yang aku pening skit. aku guna jcreator, so tak dapat nak compile sebab ada error sebab jcreator tak jumpa library mende alah ni. so caranya kembali ke zaman tok kaduk balik iaitu setiap kali korang nak compile. sila gunakan command prompt…. ya command promt

so ni script yang aku berjaya test and berjaya. aku gunakan server smtp aku sendiri so korang yang takde server tu korang bleh gunakan gmail. tapi caranya berlainan. sila sutdy faq javamail

import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
import java.io.UnsupportedEncodingException;
public class mail {
public static void main (String args[]) {
String host = “smtp server korang”;
String from = “email korang
String to = “nak bagi kat sapa email ni”;
try {
// Get system properties
Properties props = System.getProperties();
// Setup mail server
Authenticator auth = new PopupAuthenticator();
props.put(“mail.smtp.auth”, “true”);
props.put(“mail.smtp.host”, host);
props.put(“mail.smtp.port”,”26″); //sila tukar port smtp server korang
//props.put(“mail.smtp.starttls.enable”,”true”);
// Get session
Session session = Session.getInstance(props, auth);
//session.setDebug(true);
// Define message
MimeMessage message = new MimeMessage(session);
// Set the from address
message.setFrom(new InternetAddress(from,”FromName”));
// Set the to address
message.addRecipient(Message.RecipientType.TO,new InternetAddress(to,”ToName”));
// Set the subject
message.setSubject(“Hello JavaMail”);
// Set the content
message.setText(“Welcome to JavaMail”);
//System.out.println(“OK Man”);
// Send message
Transport.send(message);
System.out.println(“OK Man”);
}
catch (MessagingException e) {e.toString();}
catch (UnsupportedEncodingException e) {e.toString();}
}
static class PopupAuthenticator extends Authenticator {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(“username korang”, “tpassword korang”);
}
}
}