Latest Tutorials| Questions and Answers|Ask Questions?|Site Map




Have Programming Question? Ask it here!
 
 
 


soujanyareddy
codecode
1 Answer(s)      3 years and 7 months ago
Posted in : JSP-Servlet


hi please give me code for login page using jsp and servlets and also having validations


Advertisement
View Answers

January 13, 2011 at 3:28 PM


Hi Friend,

Try this:

1)login.jsp:

<html>
<script>
function validate(){
var username=document.form.user.value;
var password=document.form.pass.value;
if(username==""){
  document.getElementById('lab1').innerHTML="<small><font color='red'>Enter Username!</font></small>";
  return false;
}
if(password==""){
  document.getElementById('lab2').innerHTML="<small><font color='red'>Enter Password!</font></small>";
  return false;
}
return true;
}
</script>
<form name="form" method="post" action="check.jsp" onsubmit="javascript:return validate();">
<table>
<tr><td>Username:</td><td><input type="text" name="user"><label id="lab1"></label></td></tr>
<tr><td>Password:</td><td><input type="password" name="pass"><label id="lab2"></label></td></tr>
<tr><td></td><td><input type="submit" value="Submit"></td></tr>
</table>
</form>
</html>
<%String msg=request.getParameter("msg");
if(msg!=null){
    %>
<label><font color="red"><%=msg%></font></label> 
<%
}
    %>

2)check.jsp:

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

<%
String user=request.getParameter("user");
String pass=request.getParameter("pass");
 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+"'");
int count=0;
          while(rs.next())
          {

                   count++;
          }

                    if(count>0)
          {
            out.println("welcome "+user);
          }
          else
          {
                       response.sendRedirect("login.jsp?msg=Invalid Username or Password");
          }
%>

For more information, visit the following link:

Login Application

Thanks



Related Tutorials/Questions & Answers:
 

 

 

DMCA.com