Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3594734
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T19:45:14+00:00 2026-05-18T19:45:14+00:00

I’m studying RESTful web service using Java. My environment is using Netbean with GlassFish

  • 0

I’m studying RESTful web service using Java.
My environment is using Netbean with GlassFish v3.

I have a page URL /inventoryList which is URL mapped to InventoryApp.java servlet in web.xml

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
    version="2.4">
    <servlet>
        <servlet-name>inventory servlet</servlet-name>
        <servlet-class>local.test.servlet.InventoryApp</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>inventory servlet</servlet-name>
        <url-pattern>/inventoryList</url-pattern>
    </servlet-mapping>
</web-app>

In the servlet, it obtains list of inventory item info from DB and display to the JSP page.

inventory.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt" %>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>

        <script type="text/javascript">

        function ajaxGet(inventoryId) {
            alert(inventoryId);
            var xmlHttp = new XMLHttpRequest();
            xmlHttp.onreadystatechange = function() {

                alert('sigh');

                if(xmlHttp.readyState == 4 && xmlHttp.status == 200) {
                    alert('ready 4');
                    alert('After ready4 ===> ' + xmlHttp.responseText);

                    displayInventoryHtml(xmlHttp);

                }
            }
            var url = "resources/inventory/" + inventoryId;
            xmlHttp.open("GET", url, true);
            xmlHttp.send(null);
        }

        function displayInventoryHtml(responseAjax) {
            document.getElementById('inventoryItem').innerHTML = responseAjax.responseText;
        }

        </script>

    </head>
    <body>
        <h1>Inventory page</h1>

        <table border="1" cellspacing="1" cellpadding="5">
            <th>id</th>
            <th>amount</th>
            <c:forEach items="${inventoryList}" var="inv" >
                <tr>
                    <td>${inv.id}</td>
                    <td><a href="" onclick="ajaxGet(${inv.id})">${inv.amount}</a></td>
                </tr>
            </c:forEach>
        </table>
        <hr />
        <div id="inventoryItem">
        </div>


    </body>
</html>

As you can see the inventory.jsp would successfully output the list of inventory item.

So far so good.

Here, I made the output of inventory amount value to be a link to Ajax call.

<td><a href="" onclick="ajaxGet(${inv.id})">${inv.amount}</a></td>

It calls HTTP GET method and REST service (code shown below) will get inventory data of specified id (database primary id) and I put the Ajax responseText into the div (id=inventoryItem)

InventoryResource.java

package local.test.jaxrs;

import java.util.List;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import local.test.dao.InventoryDao;
import local.test.session.Inventory;

@Path("/inventory")
public class InventoryResource {
    @Context
    private UriInfo context;

    /** Creates a new instance of InventoryResource */
    public InventoryResource() {
    }


    @Path("{inv_id}")
    @GET
    @Produces("text/html")
    public String getJson(@PathParam("inv_id") String inventory_id) {
        System.out.println("GET is being handled");

        Inventory invBean = new Inventory(Integer.valueOf(inventory_id));
        InventoryDao invDao = new InventoryDao();
        List<Inventory> inv = invDao.findById(invBean);

        String html = "<b>" + inv.get(0).getId() + "</b><br /><b>" + inv.get(0).getAmount() + "</b><br />";

        return html;

    }
}//end class

When I test this code, everything works just fine. The Ajax successfully get data and insert into the HTML DIV tag and the inventory data shows up for half second and disappear.

Using firebug and looking at glassfish v3 server log, I figure that at the end, it is calling InventoryApp.java servlet AGAIN which cause the page to redirect to /inventoryList

I know Ajax is partial request and should not cause page to refresh.

I’m stack in this for few days now, could anyone give me hint what’s going on?
I’m not sure if it is practical to mix servlet and web.xml with REST like I do.

FYI, my InventoryApp.java servlet code

package local.test.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import local.test.dao.InventoryDao;
import local.test.session.Inventory;


@WebServlet(name="InventoryApp", urlPatterns={"/InventoryApp"})
public class InventoryApp extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {

        } finally { 
            out.close();
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        InventoryDao invDao = new InventoryDao();
        List<Inventory> invList =  invDao.findAll();

        //this list looks ok...
        System.out.println("================= do get servelt ===" + invList.get(0));

        request.setAttribute("inventoryList", invList);
        request.getRequestDispatcher("inventory.jsp").forward(request, response);

        //processRequest(request, response); //commented not sure what it is..
    } 
}//end class
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T19:45:15+00:00Added an answer on May 18, 2026 at 7:45 pm
    <a href="" onclick="ajaxGet(${inv.id})">${inv.amount}</a>
    

    remove the href attribute

    <a onclick="ajaxGet(${inv.id})">${inv.amount}</a>
    

    or cancel the promotion of the click event

    <a href="" onclick="ajaxGet(${inv.id}); return false;">${inv.amount}</a>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm making a simple page using Google Maps API 3. My first. One marker
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Seemingly simple, but I cannot find anything relevant on the web. What is the
this is what i have right now Drawing an RSS feed into the php,
I have just tried to save a simple *.rtf file with some websites and
I have a French site that I want to parse, but am running into
I have a bunch of posts stored in text files formatted in yaml/textile (from
I am trying to loop through a bunch of documents I have to put
I have some data like this: 1 2 3 4 5 9 2 6

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.