login.jsp

----

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"

pageEncoding="ISO-8859-1"%>

<%@page language="java" import="java.util.*" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

html

head

meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

title>Insert title here</title>

</head>

body

<BODY BGCOLOR="white">

center

<H1<font color="red">User ID and Password does not match!</font</H1>

<H2>Please Enter your User ID and Password</H2>

br

<FORM method="post" ACTION="validate">

User ID:

<INPUT TYPE="TEXT" NAME="user_name" VALUE=""<p>

Password:

<INPUT TYPE="password" NAME="password" VALUE=""<p>

<label>Department ID:

<select name="dept">

<option selected="selected">Select Dept </option>

<option value="1">Registrar</option>

<option value="2">BCTC</option>

<option value="3">Zicklin</option>

</select>

</label>

<P>

<INPUT TYPE="SUBMIT" value="validate"/> <!-- Press this button to submit form -->

</FORM>

</CENTER>

</body>

</html>

------

login.java

----

import java.sql.*;

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

@SuppressWarnings("serial")

public class Login extends HttpServlet

{

public void doPost (HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException

{

// Get the requested name and the output writer.

PrintWriter out = response.getWriter ();

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

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

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

String user = request.getParameter ("user_name");

//Used to instantiate a connection

Connection conn = null;

try

{

String driver = "oracle.jdbc.OracleDriver";

Class.forName(driver);

String url = "jdbc:oracle:thin:@localhost:1521:xe"; //orcl is the SID

String myusername = "krunal"; // Your DB login ID

String mypassword = "krunal"; //Your Db pass

conn = DriverManager.getConnection(url, myusername,mypassword);

// Create a statement and a query and get the ResultSet.

Statement stmt = conn.createStatement ();

ResultSet rs = stmt.executeQuery ("...");

/* If it is a valid user: create the session and direct user to

*correct page. Creates session. Keeps the username name in the

*session till the browser is close */

if (rs.next()) {

HttpSession session = request.getSession(true);

session.setAttribute("user", user_name);

if(

(!(user_name.equals ("null")) & user_name.equals(rs.getString ("username")))

& (!(password.equals ("null")) & password.equals(rs.getString ("password")))

& (dept.equals(rs.getString ("dept"))))

{

//Creates session with username and direct to next page.

response.sendRedirect("update.jsp");

}

}

} catch (SQLException e)

{

// TODO Auto-generated catch block

e.printStackTrace();

} catch (ClassNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

finally

{

//Clean up resources, close the connection.

if(conn != null)

{

try

{

conn.close();

}//try

catch (Exception ignored) {}

}

}

}

}

------

validate.java

----

import java.io.*;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import java.sql.*;

import javax.sql.*;

import javax.naming.*;

@SuppressWarnings("unused")

public class Validate

{

public static boolean checkUser(String user_name, String password, String dept )

{

boolean st = false;

try{

String driver = "oracle.jdbc.OracleDriver";

Class.forName(driver);

String url = "jdbc:oracle:thin:@localhost:1521:xe"; //orcl is the SID

String myusername = "krunal"; // Your DB login ID

String mypassword = "krunal"; //Your Db pass

Connection con = DriverManager.getConnection(url, myusername,mypassword);

PreparedStatement ps = con.prepareStatement("select * from ADMIN_USER_SCHEDULE_READER where user_name=? and password=? and dept=?" );

ps.setString(1, user_name);

ps.setString(2, password);

ps.setString(3, dept);

ResultSet rs = ps.executeQuery();

st=rs.next();

}catch(Exception e)

{

e.printStackTrace();

}

return st;

}

}

------

update.jsp

-----

<%@ page import="java.sql.*,

javax.sql.*,

java.io.*,

java.sql.Date,

javax.naming.InitialContext,

javax.naming.Context"

%>

<%@ include file="username1.jsp" %>

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"

pageEncoding="ISO-8859-1"%>

<%

String Semester = request.getParameter("semester");

if(Semester != null) {

%>

Selected value id: <%= Semester %>

<%

}

%>

<%

String Start_date = request.getParameter("startDate");

if(Start_date != null) {

%>

Selected value id: <%= Start_date %>

<%

}

%>

<%

String End_date = request.getParameter("endDate");

if(End_date != null) {

%>

Selected value id: <%= End_date %>

<%

}

%>

<%

Connection conn = null;

PreparedStatement psSelectRecord=null;

String sqlSelectRecord=null;

ResultSet rsSelectRecord=null;

int semester_id;

String start_date="";

String end_date="";

String semester_name;

String semester;

Class.forName(driver);

conn = DriverManager.getConnection(url,myusername,

mypassword);

sqlSelectRecord="SELECT SEMESTER_ID, START_DATE, END_DATE, SEMESTER_NAME, SEMESTER FROM SEMESTER_SCHEDULE_READER";

psSelectRecord=conn.prepareStatement(sqlSelectRecord);

rsSelectRecord=psSelectRecord.executeQuery();

if(rsSelectRecord.next())

{

start_date=rsSelectRecord.getString("START_DATE");

end_date=rsSelectRecord.getString("END_DATE");

Semester=rsSelectRecord.getString("SEMESTER_NAME");

}

/*

finally

{

//Clean up resources, close the connection.

if(conn != null)

{

try

{

conn.close();

}

catch (Exception ignored) {}

}

}

*/

%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

html

head

meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

title>Insert title here</title>

</head>

body

<BODY BGCOLOR="white">

<CENTER>

<H1>Welcome to Update Baruch Course Schedule</H1>

br

<FORM ACTION="Display.jsp" >

<label >Semester:

<select name="semester" id="semester" >

<option selected="selected" >Semester Select</option>

<option value='Spring'>Spring 2007 </option>

<option value='January Intersession'>January Intersession 2007 </option>

<option value='Fall'>Fall 2006 </option>

<option value='Summer ALL'>Summer ALL 2006 </option>

<option value='Summer00'>Summer00 2006 (06/01/2006 to 08/17/2006)</option>

<option value='Summer02'>Summer02 2006 (07/10/2006 to 08/17/2006)</option>

<option value='Summer01'>Summer01 2006 (06/01/2006 to 07/06/2006)</option>

</select>

</label&nbsp;&nbsp;

Start Date:

<INPUT type="TEXT" name="startDate" id="startDate" value =<%=start_date%>/&nbsp;&nbsp;&nbsp;

End Date:</B>

<INPUT TYPE="TEXT" NAME="endDate" id="endDate" value =<%=end_date%>/<brbrbr

<INPUT TYPE="SUBMIT" value="Update Schedule"> <!-- Press this button to submit form -->

</FORM>

</CENTER>

</body>

</html>

<%

try{

if(psSelectRecord!=null)

{

psSelectRecord.close();

}

if(rsSelectRecord!=null)

{

rsSelectRecord.close();

}

}

finally

{

//Clean up resources, close the connection.

if(conn != null)

{

try

{

conn.close();

}

catch (Exception ignored) {}

}

}

%>

------

web.xml

------

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns="http://java.sun.com/xml/ns/j2ee"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"

version="2.4">

welcome-file-list

<welcome-file>login.jsp</welcome-file>

</welcome-file-list>

<servlet

<servlet-name>Validate</servlet-name>

<servlet-class>Validate</servlet-class>

</servlet>

<servlet-mapping

<servlet-name>Validate</servlet-name>

<url-pattern>/validate</url-pattern>

</servlet-mapping>

</web-app>