You are on page 1of 7

Index.

html

<html>

<body>

<form action="LoginServlet" method="POST">

<center>

<div>

<br />

User Id:

<input type="text" name="userId" />

<br />

Password:

<input type="password" name="password" />

<br />

<input type="submit" name="submit" value="submit" />

</div>

</center>

</form>

</body>

</html>

LoginServlet

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import javax.servlet.ServletException;

import javax.servlet.http.HttpSession;
import java.io.*;

import java.sql.*;

import java.util.*;

public class LoginServlet extends HttpServlet

protected void doPost(HttpServletRequest request, HttpServletResponse


response) throws ServletException,IOException

String id = request.getParameter("userId");

String password = request.getParameter("password");

System.out.println ("id = "+id+ " and password = "+password);

String url="jdbc:mysql://localhost:3306/exam";

String driverClass = "com.mysql.jdbc.Driver";

try

Class.forName(driverClass);

catch(Exception e)

System.out.println ("Connection failed");


e.printStackTrace();

try

Connection con =
DriverManager.getConnection(url,"root","root");

Statement stmt = con.createStatement();

ResultSet rs = stmt.executeQuery("select id from


exam.login_table where id='"+id+"' and pass ='"+password+"'");

HttpSession ses=null;

if(rs.next())

System.out.println ("correct login id and password ");

ses = request.getSession();

ses.setAttribute("userid",id);

response.setContentType("text/html");

PrintWriter out = response.getWriter();

out.println("<html>");

out.println("<body>");

out.println("You are logged in ");

out.println("<form action = \"OnlineExam\"


method=\"POST\">");

out.println("<input type = \"submit\" name=\"submit\"


value=\"Start Test\"/>");
out.println("</form>");

out.println("</body></html>");

out.close();

else

ses.setAttribute("userid",null);

catch(SQLException e)

System.out.println ("Error in connection ");

e.printStackTrace();

finally

con.close();

Online Exam

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import javax.servlet.ServletException;

import javax.servlet.http.HttpSession;

import java.io.*;

import java.sql.*;

import java.util.*;

public class OnlineExam extends HttpServlet

protected void doPost(HttpServletRequest request, HttpServletResponse


response) throws ServletException, IOException

HttpSession ses = request.getSession(true);

String session = (String)ses.getAttribute("userid");

System.out.println (session);

if(session!=null)

System.out.println (session);

PrintWriter out = response.getWriter();

//-----------------------------------------------------------

out.println("<html><body><form>");

out.println("<input type=\"text\"")

//------------------------------------------------------------
}

else

System.out.println ("bye");

Web.xml

<web-app>

<servlet>

<servlet-name>Login</servlet-name>

<servlet-class>LoginServlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>Login</servlet-name>

<url-pattern>/LoginServlet</url-pattern>

</servlet-mapping>

<servlet>

<servlet-name>Online</servlet-name>

<servlet-class>OnlineExam</servlet-class>

</servlet>
<servlet-mapping>

<servlet-name>Online</servlet-name>

<url-pattern>/OnlineExam</url-pattern>

</servlet-mapping>

</web-app>