
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.
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.....
Thank You Very Much Prem.

The given code allow the user to select department from the dropdown and enter username and password. If the user s valid, it will display welcome message otherwise, you will get back to login page.
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);
}
%>
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.