-1

I have created a class DemoUtil and inside this class created a method getName() inside this method I am declaring String value. I have one jsp page index.jsp where I have imported the class <%@ page import="com.example.demo.util.DemoUtil" %> and created a instance of the class and calling the method and storing in the variable. In this jsp page I have include a menu using <jsp:include page="menu.jsp" /> and in this menu.jsp I am trying to print value like this <%= name %> which we stored in index.jsp but it's giving me error name cannot be resolved to a variable. I am putting the jsp and class file below

index.jsp

<!DOCTYPE html>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ page import="com.example.demo.util.DemoUtil"  %>
<html lang="en">
<head>
<title>Demo</title>
</head>
<body>
<%
DemoUtil demoutil = new DemoUtil();
String name = demoutil.getName();
%>
<jsp:include page="menu.jsp" />
</body>
</html>

menu.jsp

<a href="page.jsp">Page</a><br>
<a href="page1.jsp"><%= name %> Page 1</a><br>
<a href="page2.jsp">Page 2</a><br>
<a href="page3.jsp">Page 3</a><br>
<a href="page4.jsp">Page 4</a><br>

DemoUtil.java

package com.example.demo.util;

public class DemoUtil {
public String getName() {
String name = "Jason";
return name;
}
}

Help me to figure out this problem

Jason
  • 403
  • 1
  • 6
  • 14
  • Maybe you should try using the directive. See https://stackoverflow.com/questions/14580120/whats-the-difference-between-including-files-with-jsp-include-directive-jsp-in – rickz May 26 '22 at 21:46

0 Answers0