2

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

1
  • Wondering whether the answers helped..
    – T J
    Oct 22, 2014 at 11:25

4 Answers 4

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();
        }
}
1

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.

0

You just need to submit your form - (Instead of document.location.href="JsonServlet";)

document.getElementByName('getdata').submit();
1
  • That just makes no sense. He can just submit as is with the form input submit.
    – Setrino
    Mar 18, 2017 at 16:34
0

I want to know how can we call the servlet class on that button click.I m using button in place of submit button

You don't need js here, you can do it with the existing form itself. Replace the input tag as follows:

<input type="submit" name="fetchdata" value="CLick to get data" />

If you still want to use js, try below script instead:

function callservlet() {
    document.forms.getdata.submit();
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.