How to call servlet when button click

54,586

Solution 1

try this :

function callservlet() {

    var servletname=document.getdata.fetchdata.value;

    if(servletname== "")
        {
        alert("NO value..");
        return false;
        }
    else
        {
        alert("value"+servletname);
            document.forms[0].action = "JsonServlet"
            document.forms[0].submit();
        }
}

Solution 2

You can't set the location to a servlet. Instead, what you should do to hit the servlet is to submit your form:

function callservlet() {
  //do your processing.
  document.getElementsByName('getdata')[0].submit();
}

Or you can simply use a submit type button.

Share:
54,586
blackjack
Author by

blackjack

Fresher in the android world

Updated on July 09, 2022

Comments

  • blackjack
    blackjack almost 2 years

    As new to the jsp and servlet and only having basic idea of it and still learning .I want to know how can we call the servlet class on that button click.I m using button in place of submit button

    Here is the page content:-

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%>
    <!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=ISO-8859-1">
    <title>JSON DATA EXAMPLE</title>
    <script type="text/javascript">
    function callservlet() {
    
        var servletname=document.getdata.fetchdata.value;
    
        if(servletname== "")
            {
            alert("NO value..");
            return false;
            }
        else
            {
            alert("value"+servletname);
            document.location.href="JsonServlet";
            return false;
            }
    }
    
    </script>
    </head>
    <body>
    <div>
    <form name="getdata" action="JsonServlet" method="post">
    <input type="button" name="fetchdata" value="CLick to get data" onclick="return callservlet();">
    
    </form>
    
    
    </div>
    
    </body>
    </html>
    

    and here the servlet class content

    public class JsonServlet extends HttpServlet 
    {
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp)
                throws ServletException, IOException {
    
            JsonParser parser=new JsonParser();
    
            if(req.getParameter("fetchdata")!=null)
            {
                System.out.println("Button Clicked");
    
            }
            else
            {
                System.out.println("Button not clicked");
            }
        }
    
    }
    

    and here is the web.xml part

    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
      <display-name>JsonDataExample</display-name>
      <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    
      </welcome-file-list>
    
    
      <servlet>
    
          <servlet-name>Jsonfetch</servlet-name>
          <servlet-class>com.text.JsonServlet</servlet-class>
      </servlet>
    
      <servlet-mapping>
          <servlet-name>Jsonfetch</servlet-name>
          <url-pattern>/JsonServlet</url-pattern>
      </servlet-mapping>
    </web-app>
    

    so is there any part i m write the method wrong because its gives me 404 error resource not found so i want the concept that on that button click how can i call my servlet class.Thanks for any reply

  • Setrino
    Setrino about 7 years
    That just makes no sense. He can just submit as is with the form input submit.