Hi all
i am creating an application in which i m using jsp,servlet and javascript. on jsp page there are text boxes named ID and Passwprd and a submit button.
When we submit jsp page first javascript will be executed if we left any of the text boxes blank, that means both the text boxes are mandatory else it will go to the servlet.
The issue is after executing javascript it goes to the servlet and i want if javascript is executing then it must not go to the servlet and remain at jsp page.

Please solve my problem....
Thanks

Post your code. There are various way to do that. If you say that you have javascript that executes then post your code.

JSP PAGE

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Online Booking</title>
<link href="style.css" rel="stylesheet" type="text/css" />
<script language="JavaScript">

     function val(){

        if(document.getElementById("txtAdminId").value.length==0){
            alert("Please Enter Your ID")
            document.frmAdminLogin.txtAdminId.focus()



        }
         else if(document.getElementById("txtAdminPassword").value.length==0){
            alert("Please Enter password.")
           

        }
     }
        
</script>
</head>
<body>
    <form  name="frmAdminLogin" action="AdminLogin" method="post">

        <%
       String str1 = (String)request.getAttribute("loginFailed");
       
%>
<jsp:include page="adminHeader1.jsp" />

<div id="login" class="fixed">



                    <table align="center">
                        <tr><td>  <img src="images/login_icon.jpg" alt=".." width="80" height="80" /></td>
                          <td colspan="100%" align="center">Admin Login Form</td>
           </tr>

            

            <tr>
                <td width="100">ID</td>
                <td><input type="text" name="txtAdminId" id="txtAdminId" /></td>
            </tr>

            <tr>
                <td width="100">Password</td>
                <td><input type="Password" name="txtAdminPassword" id="txtAdminPassword" /></td>
            </tr>

            <tr>
                <td align="right"><input type="submit" value="Login" name="btnLogIn" onclick="val()" /></td>
                <td align="center"><input type="reset" value="Reset" name="btnReset" /></td>
            </tr>
               
                </table>
    <%
          if(str1!=null){
%>


<h3 class="text-center">  <%=str1%></h3>

                    <%}%>
</div>
  <jsp:include page="adminFooter.jsp" />
</form>
</body>
</html>

Servlet

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.etravel;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;
import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpSession;
//import com.etravel.ConnectionClass;

public class AdminLogin extends HttpServlet {
   
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        //String connectionURL = "jdbc:mysql://localhost/OnlineTraveling";
         boolean flag = false;
         String strLoginFailed = "Login Failed!!! Please try again";
          Connection con=null;

        HttpSession session = request.getSession(true);
        String strId = request.getParameter("txtAdminId");
        String strPass = request.getParameter("txtAdminPassword");
        session.setAttribute("id", strId);
        session.setAttribute("pass", strPass);
         
            if(strId.equals("") || strPass.equals("")){

                response.sendRedirect("adminLogin.jsp");
            }
        
            else{

               con = (Connection) ConnectionClass.connection();

        try {

                Statement st = con.createStatement();
               ResultSet rs = st.executeQuery("Select * from Login");
                while(rs.next()){

                    String id = rs.getString(1);
                    String pass = rs.getString(2);

                    if(strId.equals(id) && strPass.equals(pass)){

                       flag = true;

                    }

                }
             if(flag==true){
                   response.sendRedirect("adminHomePage.jsp");
               }
               else{
                   request.setAttribute("loginFailed",strLoginFailed );
                    RequestDispatcher disp = request.getRequestDispatcher("adminLogin.jsp");
                     if(disp!=null){

                        disp.forward(request, response);
                       }
               }
        }
        catch(Exception ex){

            out.println("Exception:::::::" +ex);
        }
                finally {
            out.close();
        }
    }
              

          
    }
}

One simple way is this:

<input type="button" value="Login" name="btnLogIn" onclick="val()" />

And at the val function:

function val(){

        if(document.getElementById("txtAdminId").value.length==0){
            alert("Please Enter Your ID");
        } else if(document.getElementById("txtAdminPassword").value.length==0){
            alert("Please Enter password.");
        } else {
             document.frmAdminLogin.submit();
         }
     }

If you want to have your button to be of type 'submit' then:

<form name="frmAdminLogin" action="AdminLogin" method="post" onsubmit=" call the method here ">

...

<input type="submit" value="Login" name="btnLogIn" />

</form>

If you want the later way the method at the onsubmit event must return true or false. I don't remember if you must enter it like this:
onsubmit="return val();"
OR
onsubmit="val();"

But I do know that val in this case must return boolean

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.