简体   繁体   中英

Unable to read JSON file in jsp

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

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>

¿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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM