📄 online-quiz-application-in-jsp.shtml
字号:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>Online Quiz Application in JSP</title>
</head>
<body>
<h1><font color="#000080">Online Quiz Application in JSP</font></h1>
<font color="#000080">
<br>
In this section, we are going to implement of Online quiz application using of JSP.</font>
<p><font color="#000080">
Step 1: Create Question and answer insert form (questForm.jsp) .<br>
<br>
In this step first of all create quiz question and answer form using with JSP or JDBC database. <br>
<br>
<b>
Here is the code.</b><br>
<br>
</font></p>
<table border="0" cellpadding="0" cellspacing="1" width="300" bgcolor="#FFFFE6">
<tr>
<td><font color="#000080"><%@page language="java" import="java.sql.*" %><br>
<br>
<%<br>
if(request.getParameter("submit")!=null)<br>
{<br>
Connection con = null;<br>
String url = "jdbc:mysql://192.168.10.211:3306/";<br>
String db = "amar";<br>
String driver = "com.mysql.jdbc.Driver";<br>
String userName ="amar";<br>
String pass="amar123";<br>
<br>
try{<br>
Class.forName(driver);<br>
con = DriverManager.getConnection(url+db,userName,pass);<br>
<br>
try{<br>
Statement st = con.createStatement();<br>
String quest = request.getParameter("quest").toString();<br>
String QA = request.getParameter("QA").toString();<br>
String QB = request.getParameter("QB").toString();<br>
String QC = request.getParameter("QC").toString();<br>
String QD = request.getParameter("QD").toString();<br>
String correctAns = request.getParameter("correctAns").toString();<br>
out.println("quest : " + quest);<br>
<br>
String qry = "insert into question_deatil(quest,QA,QB,QC,QD,correctAns) values('"+quest+"','"+QA+"','"+QB+"','"+QC+"','"+QD+"','"+correctAns+"')";<br>
<br>
out.println("qry : " + qry);<br>
<br>
int val = st.executeUpdate(qry);<br>
<br>
con.close();<br>
if(val>0)<br>
{<br>
response.sendRedirect("quizeApplication.jsp");<br>
}<br>
<br>
}<br>
<br>
catch(SQLException ex){<br>
System.out.println("SQL satatment not found");<br>
}<br>
}<br>
catch(Exception e){<br>
e.printStackTrace();<br>
}<br>
}<br>
<br>
%><br>
<br>
<html><br>
<title>Quize application in jsp</title><br>
<head><br>
<br>
<script><br>
<br>
function validateForm(theForm){<br>
<br>
if(theForm.quest.value==""){<br>
//Please enter username<br>
alert("Please enter Question.");<br>
theForm.quest.focus();<br>
return false;<br>
}<br>
<br>
return true;<br>
} <br>
</script><br>
</head><br>
<br>
<body><br>
<br><br>
<br/><br>
<center><br>
<br>
<table border="1" width="450px" bgcolor="pink" cellspacing="0" cellpadding="0"><br>
<tr><br>
<td width="100%"><br>
<form method="POST" action="" onsubmit="return validateForm(this);"><br>
<br>
<h2 align="center"><font color="red">Quize Application in JSP</font></h2><br>
<table border="0" width="400px" cellspacing="2" cellpadding="4"><br>
<tr><br>
<td width="50%"><b>Enter Question:</b></td><br>
<td width="50%"><input type="text" name="quest" size="40"/> </td><br>
</tr><br>
<tr><br>
<td width="50%"><b>Enter Answer(A.):</b></td><br>
<td width="50%"><input type="text" name="QA" size="40"/> </td><br>
</tr><br>
<tr><br>
<td width="50%"><b>Enter Answer(B.):</b></td><br>
<td width="50%"><input type="text" name="QB" size="40"/> </td><br>
</tr><br>
<br>
<tr><br>
<td width="50%"><b>Enter Answer(C.):</b></td><br>
<td width="50%"><input type="text" name="QC" size="40"/> </td><br>
</tr><br>
<br>
<tr><br>
<td width="50%"><b>Enter Answer(D.):</b></td><br>
<td width="50%"><input type="text" name="QD" size="40"/> </td><br>
</tr><br>
<br>
<tr><br>
<td width="50%"><b>Correct Answer:</b></td><br>
<td width="50%"><input type="text" name="correctAns" size="10"/> </td><br>
</tr> <br>
<br>
</table><br>
<center><br>
<p><input type="submit" value="Submit" name="submit"><br>
<input type="reset" value="Reset" name="reset"></p><br>
</center> <br>
</form><br>
</td><br>
</tr><br>
</table><br>
</center><br>
</body><br>
</html></font></td>
</tr>
</table>
<p><font color="#000080">
<br>
<br>
<br>
<br>
<br>
<br>
Step 2 : Create "quizeApplication.jsp" for Process the Data and retrive the data from database and show th all data on browser.<br>
<br>
<b>Here is the code:<br>
</b></font></p>
<table border="0" cellpadding="0" cellspacing="1" width="538" bgcolor="#FFFFE6">
<tr>
<td width="532"><font color="#000080"><%@ page import="java.sql.*" %><br>
<br>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"<br>
"http://www.w3.org/TR/html4/loose.dtd"><br>
<br>
<html><br>
<head><br>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><br>
<title>JSP Page</title><br>
</head><br>
<body><br>
<% <br>
String ans=" ";<br>
if(request.getParameter("correctAns")!=null)<br>
{<br>
ans=request.getParameter("correctAns").toString();<br>
}<br>
</font>
<p><font color="#000080">Connection conn = null;<br>
String driver = "com.mysql.jdbc.Driver";<br>
String url = "jdbc:mysql://192.168.10.211:3306/";<br>
String db = "amar";<br>
String user = "amar";<br>
String pass = "amar123";<br>
Statement st = null;<br>
ResultSet qrst;<br>
ResultSet rs = null;<br>
<br>
String id=request.getParameter("id");<br>
System.out.println("id:"+id);<br>
<br>
int i=1;<br>
<br>
String s,g;<br>
<br>
int count=0;<br>
<br>
try {<br>
<br>
Class.forName(driver);<br>
conn = DriverManager.getConnection(url+db,user,pass);<br>
<br>
st = conn.createStatement();<br>
rs = st.executeQuery("select * from question_deatil");</font></p>
<p><font color="#000080">while(rs.next()) {<br>
%><br>
<br><br>
<br/><br>
<center><br>
<br>
<table border="1" width="500px" bgcolor="pink" cellspacing="0" cellpadding="0"><br>
<tr><br>
<td width="100%"><br>
<br>
<form name="form1"><br>
<br>
<h2 align="center"><font color="red">Online Quize Application</font></h2><br>
<br>
<b>Select Correct Answer</b><br>
<table border="0" width="500px" cellspacing="2" cellpadding="4"><br>
<tr><br>
<br>
<td width="50%"> Question:</td><br>
<input type="hidden" name="correctAns" value="<%=rs.getString(7)%>" /><br>
<tr><br>
<td><%= rs.getString("quest") %></td></tr><br>
<tr><br>
<td><br>
<br>
1: <input type="radio" name="a" value= "QA" /></td><br>
<td><%= rs.getString("QA") %></td></tr> <br>
<tr><br>
<td><br>
2: <input type="radio" name="a" value="QB" /></td><br>
<td><%= rs.getString("QB") %></td></tr><br>
<br>
<tr><br>
<td><br>
3: <input type="radio" name="a" value="QC" /></td><br>
<td><%= rs.getString("QC") %> </td></tr><br>
<br>
<tr><br>
<td><br>
4: <input type="radio" name="a" value="QD" /> </td><br>
<td> <%= rs.getString("QD") %> </td></tr><br>
<br>
<tr><br>
<td><br>
<center><br>
<input type="submit" value="Submit" name="submit"></center></td></tr><br>
</table><br>
<br>
</form><br>
</td><br>
</tr><br>
</table><br>
</center><br>
</body><br>
<% g=request.getParameter("a");<br>
%><br>
<% <br>
if(g.equals(ans)){<br>
<br>
count++;<br>
out.println("Correct");<br>
<br>
}<br>
else<br>
out.println("false");<br>
%><br>
<br>
<%<br>
}}<br>
<br>
catch (Exception ex) {<br>
ex.printStackTrace();<br>
<br>
%><br>
<br>
<%<br>
} finally {<br>
if (rs != null) rs.close();<br>
if (st != null) st.close();<br>
if (conn != null) conn.close();<br>
}<br>
out.println("Score="+count);<br>
%><br>
</html></font></td>
</tr>
</table>
<p><font color="#000080">
<br>
<br>
<br>
<b>
Output of this Application:</b></font></p>
<p><font color="#000080"><br>
<br>
<img border="0" src="quizeInsert.gif" width="408" height="390">
<br>
<br>
<br>
<br>
<br>
<img border="0" src="questioninsert.gif" width="407" height="391">
<br>
<br>
</font></p>
<p> </p>
<p><img border="0" src="questionPage.gif" width="460" height="322"><font color="#000080">
<br>
<br>
<br>
<br>
Download of this application.<br>
</font></p>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -