Jamsiya N P
question
2 Answer(s)      a year and 11 months ago
Posted in : Java Beginners

simple jsp code for login and validation using jsp and simple java script

View Answers

June 15, 2011 at 10:04 AM


<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<script type="text/javascript">
function validate()
{
    var userName=document.valid.user.value;
    var userPass=document.valid.pass.value;
    if(userName=="")
        {
        alert("Enter User Name and Password!");
        document.form.user.focus();
        return false;
        }
    else if(userPass=="")
        {
        alert("Enter User Name and Password!");
        document.form.user.focus();
        return false;
        }
    else
        {
        document.valid.submit();
        }
    return true;
    }


</script>
<body>
<form action="Validation.jsp" method="post" name="valid" >
<table>
<tbody>
<tr>
<td>User Name:</td>
<td><input type="text" name="user"></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="password" name="pass">
</td>
</tr>
<tr>
<td>
<input type="button" value="Login" onclick="validate();">
</td>
</tr>



</tbody>
</table>
</form>

<%String st=request.getParameter("msg");
if(st!=null)
out.println(st);
%>
</body>
</html>

June 15, 2011 at 10:27 AM


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>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="Submit"></td></tr>
    </table>
    </form>
    </html>

2)check.jsp:

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

    <%
    try{
    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");
              }
    }

    catch(Exception e){
        System.out.println(e);
    }
    %>









Related Pages:
question
question   sir plz tell me what should i give in title box. just i want java program for the question typed in this area
question
question   sir plz tell me what should i give in title box. just i want java program for the question typed in this area
Question
Question   When there is an exception in my program how java runtime system handles
question
question  dear sir/madam my question is how to compare two text format in java..we are java beginners..so we need the complete source code for above mentioned question...we have to compare each and every word
question
question  Dear sir i had some typing mistake at previous question so its my humble request to let me know the steps to start the tomcat6 under the tomcat directory
question
question  Gud morning sir, I have asked u some question regarding jsp in saturaday for that i didnot find any answere in which u send me the some of the links.U have asked me the specify some details. There is a entity name
Question?
Question?  My question is how to: Add a menu bar to the program with a File menu. In the File menu, add a submenu (JMenuItem) called About. When the user clicks on the About menu item, display a JOptionPane message dialog

Ask Questions?

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.