1 Replies - 6017 Views - Last Post: 06 April 2009 - 06:27 PM Rate Topic: -----

#1 lamia70  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 21-January 08

create a multiple choice quizz using jsp html servelet

Posted 06 April 2009 - 09:39 AM

MCQ Test using at least one servlet and one JSP html
Database usage
example


<!-- index.html -->

<html>
<head><title>Student Registration Front</title></head>
<body bgcolor="#ffffcc">

<h2 align="center"><font color="#800000">Student Registration Webapp 2006</font></h2>

<p></p>
<p></p>

<form action="http://localhost:8080/eventRegister/front.jsp" method="post">
<table align="center">
<tr><th>Student Registration: </th>
<td><input type="radio" name="choice" value="register" checked> </td>
<td> <font color="blue">you must register here first</font></td></tr>

<tr><td></td><td></td><td></td></tr>
<tr><td></td><td></td><td></td></tr>

<tr><th>Login for registered students: </th>
<td><input type="radio" name="choice" value="login"> </td>
<td><font color="red">this is not yet implemented<br />
and leads to a blank page</font></td></tr>

<tr><td></td><td></td><td></td></tr>

<tr><td></td>
<th><input type="submit" value="Submit"></th>
<td></td></tr>
</table>
</form>

</body>
</html>

FrontController.java
// FrontController.java

import java.io.* ;
import java.util.* ;
import javax.servlet.* ;
import javax.servlet.http.* ;
import beans.*;

/*
The front controller:
* creates a new 'session' object,
* creates an empty 'error message' String which it stores in 'session', then
* redirects the request to either:
- 'register' (the real JSP, register.jsp), or
- 'login' (not implemented)
*/

public class FrontController extends HttpServlet
{
public void doPost( HttpServletRequest request, HttpServletResponse response )
throws IOException, ServletException
{
String strChoice = request.getParameter( "choice" );

if ( strChoice.equals( "register" ) )
{
HttpSession session = request.getSession();

String strError = " "; /* now 'store' this String object - this is not a bean
but can be stored in 'session' in exactly the same way */
session.setAttribute( "error", strError );

RequestDispatcher dispatcher = getServletContext().getRequestDispatcher( "/register.jsp" );
dispatcher.forward( request, response );
}
else
{
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher( "/login.jsp" );
dispatcher.forward( request, response );
}
}
}



RegisterStudent.java
import java.io.* ;
import javax.servlet.* ;
import javax.servlet.http.* ;
import java.sql.*;
import beans.*;

/*
This servlet:
* loads the MySQL driver class in its init() method - once off initialisation
* establishes a connection to the database, studentdb1
* creates a Statement object in preparation for submitting an SQL query to the database:
* extracts the student's data from the initial form and validates that data
* gets hold of the error message string, which will be updated with meaningful information if registration fails
* if all student's details are valid:
- insert student into database
- create a corresponding Student bean object
- store this Student bean in the current 'session'
- forward control to the JSP, accept.jsp
* else (the student's data is invalid) and
- store the error message string, strError in the current 'session'
- forward control back to register.jsp which will now output the appropriate error information
*/

public class RegisterStudent extends HttpServlet
{
Connection conn;
Statement stmt;

public void init() throws ServletException
{
try
{
Class.forName( "com.mysql.jdbc.Driver" ).newInstance();

} catch (Exception ex)
{ System.out.println( "Exception is: " + ex.toString( )); }
}

public void doPost( HttpServletRequest request, HttpServletResponse response )
throws IOException, ServletException
{
try
{
conn = DriverManager.getConnection( "jdbc:mysql://localhost/"
+ "studentdb1?user=root&password=xxxx") ;

stmt = conn.createStatement( );
} catch (SQLException ex)
{ System.out.println("SQLException: " + ex.getMessage()); }

String strSurname = request.getParameter( "surname" );
String strFirstname = request.getParameter( "firstname" );
String strName = strSurname.toUpperCase() + ", " + strFirstname.toUpperCase();
String strReg = request.getParameter( "reg" );
String strModule = request.getParameter( "module" );
String strPassword = request.getParameter( "password" );
Student student = new Student( );
boolean bValid = true;

HttpSession session = request.getSession( );
String strError = (String) session.getAttribute( "error" );

The underlying logic for the data validation is given here.

//check whether any fields have not been filled in by user

if ( (strName.length( ) == 0) || (strReg.length( ) == 0) || (strPassword.length( ) == 0))
{
strError = "Try again - complete all fields"; // provide appropriate error message
bValid = false;
}

/* now check that the registration id is 8 characters long and consists only of alphanumerics
check also whether the student has already registered by calling queryReg( reg ) */

if ( bValid )
{
bValid = checkReg( strReg );
if ( !bValid )
strError = "Try again - invalid registration digits given"; // provide appropriate error message
}


/* now finally check that the password consists only of alphanumerics
if password is valid, then insert student's details into the database */

if ( bValid )
{
bValid = checkPassword( strPassword );
if ( bValid )
{
student.setName( strName );
student.setReg( strReg );
student.setModule( strModule );
student.setPassword( strPassword );

insertStudent( student ); // insert student details into DB
}
else
strError = "Try again - invalid password given"; // provide appropriate error message
}

try
{
if ( stmt != null ) stmt.close( );
if ( conn != null ) conn.close( );
} catch (SQLException ex)
{ System.out.println("SQLException: " + ex.getMessage()); }

if ( bValid )
{
session.setAttribute( "student", student );

RequestDispatcher dispatcher = getServletContext().getRequestDispatcher( "/accept.jsp" );
dispatcher.forward( request, response );
}
else
{
session.setAttribute( "error", strError );

RequestDispatcher dispatcher = getServletContext().getRequestDispatcher( "/register.jsp" );
dispatcher.forward( request, response );
}
}

/* check that the registration id is 8 characters long and consists only of alphanumerics
check also whether the student has already registered by calling queryReg( reg ) */
boolean checkReg( String reg )
{
boolean bReg = true;
if ( reg.length() != 8 )
{
bReg = false;
return bReg;
}

for ( int i = 0; i < 8; i++ )
{
if ( !Character.isDigit( reg.charAt(i)) )
{
bReg = false;
return bReg;
}
}

bReg = queryReg( reg ); // query if Registration digits already in DB

return bReg;
}

/* check that the password consists only of alphanumerics */
boolean checkPassword( String password )
{
boolean bPassword = true;
for ( int i = 0; i < password.length( ); i++ )
{
if ( !Character.isLetterOrDigit( password.charAt(i)) )
{
bPassword = false;
return bPassword;
}
}
return bPassword;
}

/* check whether student is already registered by simply
querying the database with the given registration id */
boolean queryReg( String strReg )
{
boolean bOK = true;

try
{
String strQuery = "select * from student where reg = '" + strReg + "'";
ResultSet rs = stmt.executeQuery( strQuery );
while ( rs.next())
{
bOK = false;
}
} catch (SQLException ex)
{ System.out.println("SQLException: " + ex.getMessage()); }

return bOK;
}

void insertStudent( Student student )
{
try
{
String strUpdate = "insert into student values( '"
+ student.getName() + "', '"
+ student.getReg() + "', '"
+ student.getModule() + "', '"
+ student.getPassword() + "' )";
stmt.executeUpdate( strUpdate );
} catch (SQLException ex)
{ System.out.println("SQLException: " + ex.getMessage()); }
}
}

Is This A Good Question/Topic? 0
  • +

Replies To: create a multiple choice quizz using jsp html servelet

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: create a multiple choice quizz using jsp html servelet

Posted 06 April 2009 - 06:27 PM

And the question is ?
And please go back to edit your post and put the [ code] tags
:code:
Your code is presently unreadable... and will be a pain to cut & paste if we want to try it
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1