
Hi all,
I am doing a project using JSP. My Issue is..... I have two departments name Customs and Accounts in the database with different tables. I have designed the HTML page in such a way the I have a drop down list consisting of Customs and Accounts. I have user accounts of both the departments.
Now, my intention is to select the department from the drop down box and then login to the page.
I have a drop down list box with Customs and Accounts..A Username and a Password field.Now want to login to the page by selecting the drop down box. Suppose the Customs username is 1001 and Accounts Username is 1002. and password is abcd.
In my project I have different departments say, Customs in one table and Accounts in other table. The code provided is having the departments in the same table. Bcoz as per the SQL Query select * from login where username='"+user+"' and password='"+pass+"'and department='"+dep+"' the departments is in the same table. And my issue is I want to login using different departments Customs and Accounts which are in different tables and as soon as I login I have to get individual page of Customs and Accounts.
Suppose In Customs, I have a DEPTID DEPTNAME DEPTHEAD PASSWORD ---- THIS IS ONE TABLE THE SAME LIKE ACCOUNTS. Now I want to login when I select department from the drop down list. Since I cannot populate the departments name from each table into drop down I use Select tag and declare as CustomsDept and AccountsDept
Please provide me the code on how to login using the drop down box and the Username and passwords must match with the Database during Login. One of my friend suggest me to use if(option.equals) condition for LOGIN...Please provide me the code for the above senario.....I'll be grateful to you forever.
Thank You Very Much
Prem Sagar.
Code provided:
1)login.jsp:
<html>
<script>
function validate(){
var username=document.form.user.value;
var password=document.form.pass.value;
if(username==""){
alert("Enter Username!");
return false;
}
if(password==""){
alert("Enter Password!");
return false;
}
return true;
}
</script>
<form name="form" method="post" action="check.jsp" onsubmit="javascript:return validate();">
<table>
<tr><td>Select Department: </td>
<td>
<select name="dep">
<option value="-1">Select</option>
<option value="Customs">Customs</option>
<option value="Accounts">Accounts</option>
</select>
</td></tr>
<tr><td>Username:</td><td><input type="text" name="user"></td></tr>
<tr><td>Password:</td><td><input type="password" name="pass"></td></tr>
<tr><td></td><td><input type="submit" value="Login"></td></tr>
</table>
</form>
</html>
2)check.jsp:
<%@page import="java.sql.*"%>
<%
try{
String user=request.getParameter("user");
String pass=request.getParameter("pass");
String com=request.getParameter("dep");
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select * from login where username='"+user+"' and password='"+pass+"'and department='"+dep+"'");
int count=0;
while(rs.next()){
count++;
}
if(count>0){
out.println("welcome "+user);
}
else{
response.sendRedirect("login.jsp");
}
}
catch(Exception e){
System.out.println(e);
}
%>