0

I am aware that this question has been asked before but even after going through the answers for the past 5 hours I still haven't found what I'm looking for.

I am trying to read JSON via jsp using jstl. I have imported the json-taglib jar file in WEB-INF/lib, but I'm getting

No tag "parse" defined in tag library imported with prefix "json"] with root cause org.apache.jasper.JasperException: /index-test.jsp (line: 7, column: 0) No tag "parse" defined in tag library imported with prefix "json"

This is my code

<%@ page contentType="text/html; charset=utf-8" language="java" %>
<%@ taglib tagdir="/WEB-INF/tags/layout" prefix="layout" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib prefix="json" uri="http://www.atg.com/taglibs/json" %>

<c:import var="dataJson" url="http://localhost:10081/test.json"/>
<json:parse json="${dataJson}" var="parsedJSON" />
Fetch the name of the node at index 1 : ${parsedJSON.node[1]}

and this is the JSON object I'm reading from

{
    "firstName": "Joe"
    "lastName": "Bloggs"
}

The application works fine in angular but I want to migrate the implementation such that it reads json server-side

Please help

2 Answers2

0

I used this and it worked for me :

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1" import="java.io.*, java.net.*"%>
<!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>JSP Reading Text File</title>
</head>
<body>
<%
String fileName = "/WEB-INF/json/test.json";
InputStream ins = application.getResourceAsStream(fileName);
try
{
    if(ins == null)
    {
        response.setStatus(response.SC_NOT_FOUND);
    }
    else
    {
        BufferedReader br = new BufferedReader((new InputStreamReader(ins)));
        String data;
        while((data= br.readLine())!= null)
        {
            out.println(data+"<br>");
        }
    }   
}
catch(IOException e)
{
out.println(e.getMessage());
}
%>
</body>
</html>
Hiren Gohel
  • 4,942
  • 6
  • 29
  • 48
Anas Elbenney
  • 33
  • 1
  • 2
  • 11
-1

¿Is the 10081 port the same as the web server that executes the JSP? Maybe you need to enable CORS, by adding pertinent HTTP headers by means of an HTTP filter:

https://en.wikipedia.org/wiki/Cross-origin_resource_sharing

Mario
  • 1,661
  • 13
  • 22
  • Yes, the ports are the same – Phakamani Mkulise Dec 17 '15 at 17:53
  • the approach I took was incorrect because I assumed that Steven from a link I saw this approach from was using atg, but after reading the answer carefully I saw that he created his own tag lib. A possible approach is to convert the JSON object to an ArrayList of HashMaps. But that is not reusable. I want something that is reusable such that it can be implemented on any JSON object. – Phakamani Mkulise Dec 17 '15 at 18:01