Discussions

Web tier: servlets, JSP, Web frameworks: Is it possible to use java script in servlets?

  1. Is it possible to use java script in servlets? (7 messages)


    I wanted to use javascript that is sent to client browser for client side validation from the servlet. I am not sure if the javascript works with servlets. I commented all the javascript except for the script tag and it still says "object not found" when I submit the form.. Am I supposed to include something? Please help

    import java.io.*;
    import javax.servlet.*;
    import javax.servlet.http.*;

    public class Login extends HttpServlet
    {
    public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException
    {
    doPost(request,response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)throws IOException, ServletException
    {
    PrintWriter pw = response.getWriter();
    response.setContentType("text/html");
    pw.println("<html><head>"+
    "<script language=\"text/javascript\">"+
    //"function validate()"+
    //"{"+
    //"x=document.LoginForm"+
    //"pass=x.txt_password.value"+
    //"submitOK=\"True\""+
    //"if (pass.length>6)"+
    //"{"+
    //"alert(\"Your password must be 6 characters or less\")"+
    //"submitOK=\"False\""+
    //"}"+
    //"if (submitOK==\"False\")"+
    //"{"+
    //"return false"+
    //"}"+
    //"}"+
    "</script></head>"+
    "<body bgcolor =\"FFFFFF\">"+
    "<form name=\"LoginForm\" method=\"POST\" action=\"LoginCheck\" onsubmit=\"return validate()\">
    <b>Enter ID<input type=\"password\" value=\"\" name=\"txt_username\">

    <b>Enter PIN</b><input type=\"password\" name=\"txt_password\" size=\"20\">

    <input type=\"submit\" value=\"Login\" name=\"bttn_logincheck\"></td></tr></table></center>"+
    "</form></body></html>");
      }//doPost
    }//main servlet

    Threaded Messages (7)

  2. You can use JavaScript in Servlets. The reason I believe that you are getting the object not found error is that you are calling the validate function, but it is commented out in your code.
  3. Thank you very much. I was not using semicolons at the end to end the statement. This works with html on a browser. When it coming from a servlet it doesnt and semicolons are required when ending statements.

  4. check it[ Go to top ]

          out.println("<html><head>"+"<script type=\"text/javascript\">"+"document.write(\"click on this button\");"+"var str"+"function cc()"+"{"+ "str=prompt(\"enter your name:\")"+ "alert(str+\"is MAD!\")"+"}"+"</script>"+"</head>"+"<body>"+"<input type='button' value='click me money' onclick='cc()'>"+"</body>"+"</html>");

  5. You can use JavaScript in Servlets. The reason I believe that you are getting the object not found error is that you are calling the validate function, but it is commented out in your code.
  6. Yes it is possible to use Java Script with servlets.

    When you get into client side problems like this,
    1. save a copy of the page that you see in your browser
    2. debug it
    3. make sure your servlet generates the debugged version.

    Regards,
    Manoj
  7. Hi,
       Ya, its possible and i think better way is to use netscape scripti API. Just try to download that api. And by using the functions of that API, u can easily manupulate with scripts. I don't exactly remember the URL for download the api, but i'll give it to u ASAP.

    Thanks,
    Atit.
  8. confusion[ Go to top ]

    y u used "//" in betwwen the code..? these are used for commenting purposes... will it work as u hav made it comments