Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Invoking JavaScript in Servlet

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Invoking JavaScript in Servlet

    hi guys.. i am new here and also new at java programming but trying to learn. Recently we had to study servlets so was tinkering around it. While working i thought of using javascripts in it. But after trying everything i failed to invoke javascript. This is my code. Its highly rookie code but i am just learning.

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    PrintWriter yash = response.getWriter();
    yash.println("<html>");
    yash.println("<head>");
    yash.println("<script type='text/javascript'>");
    yash.println("function callMe()");
    yash.println("{");
    yash.println("alert'Hello';");
    yash.println("}");
    yash.println("</script></head>");
    yash.println("<body>");
    yash.println("<center><table border = '4'></center>");
    yash.println("<form name = 'myform' action = '#' onSubmit = 'return callMe()'");
    yash.println("<tr><td>HEllO</td></tr></br>");
    yash.println("<tr><td>Enter Name<input type = 'text' name = 'tex'</td></tr>");
    yash.println("<tr><td> </td><td><select name = 'list' size = '4' multiple = 'multiple'>"+"<option>Hi</option>"+"<option>Hello</option>"+"<option>Shut</option>"+"</select></td></tr>");
    yash.println("<tr><td>Male<input type = 'checkbox' name = 'sex' value = 'Male'></td></tr>");
    yash.println("<tr><td>Female<input type = 'checkbox' name = 'sex' value = 'Female'></td></tr>");
    yash.println("<tr><td>ParthJr.<input type = 'checkbox' name = 'sex' value = 'ParthJr'></td></tr>");
    yash.println("<tr><td> </td><td><input type = 'submit' value = 'Submit'></td></tr></br>");
    yash.println("</form>");
    yash.println("</table>");
    yash.println("</body>");
    yash.println("</html>");

    i haven't imported any libraries except java.io. Please guys help me find out what the problem is.


  2. #2

    Default Re: Invoking JavaScript in Servlet

    Don't output HTML code directly in servlet's doGet() method. Instead, you should create a HTML page and get it redirected from the servlet.

  3. #3
    Junior Member
    Join Date
    Sep 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Invoking JavaScript in Servlet

    you are the second person to remind me this today.. ^_^ and i have learned this lesson.
    but i wanted to know why this javascript code is not getting executed despite everything being right. (at least i think so..)

  4. #4

    Default Re: Invoking JavaScript in Servlet

    Quote Originally Posted by yash188 View Post
    you are the second person to remind me this today.. ^_^ and i have learned this lesson.
    but i wanted to know why this javascript code is not getting executed despite everything being right. (at least i think so..)
    I think the problem is here:

    yash.println("alert'Hello';");
    that must be changed to:

    yash.println("alert('Hello');");

Similar Threads

  1. Replies: 0
    Last Post: March 11th, 2012, 05:57 PM
  2. [SOLVED] Invoking a superclass constructor in my subclass
    By kari4848 in forum Object Oriented Programming
    Replies: 5
    Last Post: April 29th, 2011, 02:12 PM
  3. collecting information/moving from servlet to servlet
    By CBird in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 1st, 2011, 07:04 PM
  4. Replies: 0
    Last Post: January 17th, 2011, 05:14 AM
  5. [SOLVED] Array of objects, invoking constructor for one changes others
    By BigFoot13 in forum Object Oriented Programming
    Replies: 4
    Last Post: October 24th, 2010, 02:30 PM