Tuesday, January 29, 2013

ONLINE EXAMINATION USING JSP


                                ONLINE EXAMINATION USING JSP

AIM:
      To create a  three tier application for conducting online examination using JSP and database.

ALGORITHM:
        Step 1 :  Design the HTML page (ExamClient.html) with the following
a)           Step 2: Create a form to get the input from the user.
b)            Step 3 : Use radio buttons to make various options for the questions.
c)            Step 4:  Set the URL of the server (ExamServer.jsp) as the value of the action attribute.
d)            Step 5:  Use submit button to invoke the server and send the form data to the server.
        Step 6: Create the JSP file with the following
        Step 7:  Read the input from the client.
         Step 8: Retrieve the answers from the database.
         Step 9: Match the answers from the user with the correct answers from the database table.
         Step 10: For each correct answer increment the mark by 5.
         Step 11: Server displays the mark and result to the client as a response.

PROGRAM:
ExamClient.html
<html>
<head>
<title>Online Exam Client</title>
<style type="text/css">
   body{background-color:black;font-family:courier;color:blue}
</style>
</head>
<body>
<h2 style="text-align:center">ONLINE EXAMINATION</h2>
<h3>Answer the following questions (5 marks for each correct answer)</h3>
<hr/>
<form name="examForm" method="post" action="ExamServer.jsp">
1.Who is called as the father of computer?<br/>
<input type="radio" name="ans1" value="Sachin">Sachin
<input type="radio" name="ans1" value="Stuart">Stuart
<input type="radio" name="ans1" value="Charles Babbage">Charles Babbage
<input type="radio" name="ans1" value="Napier">Napier
<br/><br/>
2.C++ was developed by?<br/>
<input type="radio" name="ans2" value="Dennis Ritchie">Dennis Ritchie
<input type="radio" name="ans2" value="None">None
<input type="radio" name="ans2" value="David Ritchie">David Ritchie
<input type="radio" name="ans2" value="John">John
<br/><br/>
3.C was developed by?<br/>
<input type="radio" name="ans3" value="Dennis Ritchie">Dennis Ritchie
<input type="radio" name="ans3" value="Stroustrup">Stroustrup
<input type="radio" name="ans3" value="David Ritchie">David Ritchie
<input type="radio" name="ans3" value="Charles Babbage">Charles Babbage
<br/><br/>
<input type="submit" value="Check Your Result"/>
</form>
</body>
</html>
ExamServer.jsp

<%@page contentType="text/html" language="java" import="java.sql.*"%>
<html>
<head>
<title>Online Exam Server</title>
<style type="text/css">
   body{background-color:black;font-family:courier;color:blue}
</style>
</head>
<body>
<h2 style="text-align:center">ONLINE EXAMINATION</h2>
<p>
<a href="ExamClient.html">Back To Main Page</a>
</p>
<hr/>
<%
String str1=request.getParameter("ans1");
String str2=request.getParameter("ans2");
String str3=request.getParameter("ans3");
int mark=0;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:examDS");
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("SELECT * FROM examTab");
int i=1;
while(rs.next())
{
 if(i==1)
 {
  String dbans1=rs.getString(1);
  if(str1.equals(dbans1))
  {
   mark=mark+5;
  }
 }
 if(i==2)
 {
  String dbans2=rs.getString(1);
  if(str2.equals(dbans2))
  {
   mark=mark+5;
  }
 }
 if(i==3)
 {
  String dbans3=rs.getString(1);
  if(str3.equals(dbans3))
  {
   mark=mark+5;
  }
 }
 i++;
}
if(mark>=10)
{
 out.println("<h4>Your Mark Is : "+mark+"</h4>");
 out.println("<h3>Congratulations....! You Are Eligible For The Next Round...</h3>");
}
else
{
 out.println("<h4>Your Mark is : "+mark+"</h4>");
 out.println("<h3>Sorry....!! You Are Not Eligible For  The Next Round...</h3>");
%>
</form>
</body>
</html>



RESULT:
         Thus the program to create three-tier application using JSP is written and the output is verified.

INVOKING SERVLETS FROM APPLETS


                                     INVOKING SERVLETS FROM APPLETS
AIM:

      To write a java program that invokes Servlet from applet.

ALGORITHM:
1.   Step 1:Create the java program with the following
a)        Step 2: Define the class Myapplet which extends the property of the class Applet and implements the interface ActionListener.
b)        Step 3:Define the objects for Button and add the button in the init() method of Applet class
c)          Step 4: Make the button to listen the action by using the method addActionListener().
d)         Step 5: Set the URL of the servlet program by using the object of the class URL.
e)         Step 6: Define the object for AppletContext in order to display the output of the servlet on new browser window.
 Step 7: Create HTML file that contains the applet tag and pass the class name to that applet code.
3.   Step 8:Create the simple servlet program that contains any response message
4.   Step9: Run the HTML file that contains the corresponding applet code.
5.    Step 10: Click the button on the applet window in order to invoke the servlet program.
       
PROGRAM:
AppletClient.java

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
public class AppletClient extends Applet implements ActionListener
{
 public void init()
 {
  Label la=new Label("INVOKING SERVLET FROM APPLET");
  la.setFont(new Font("Courier",Font.BOLD,15));
  la.setForeground(Color.blue);
  add(la);
  Button b1=new Button("Click Here To Display Date Information From Server");
  b1.setBackground(Color.black);
  b1.setForeground(Color.white);
  add(b1);
  b1.addActionListener(this);
 }
 public void actionPerformed(ActionEvent ae)
 {
  try
  {
   AppletContext ac=getAppletContext();
   URL url = new URL("http://localhost:8080/servlets-examples/servlet/MyServer");
   ac.showDocument(url);
  }
  catch(Exception e)
  {
   System.out.println(e);
  }
 }
}

AppletClient.html

<html>
<head>
<title>Invoking Servlets From Applet</title>
</head>
<body bgcolor="violet">
<applet code="AppletClient.class" width="400" height="200">
</applet>
</body>
</html>



MyServer.java

import java.io.*;
import java.util.*;
import javax.servlet.*;
public class MyServer extends GenericServlet
{
 public void service(ServletRequest req,ServletResponse res)throws  ServletException,IOException
 {
  PrintWriter pw=res.getWriter();
  Date d=new Date();
  pw.println("<html><body bgcolor=violet><h2>Srever Response</h2>");
  pw.println("<h3>Current Date and Time From Server:</h3>");
  pw.println("<b>"+d+"</b></body></html>");
 }
}
RESULT:
              Thus the program to invoke Servlets from Applets is written and output is verified.


RUN :