0

Shooping Servlet

package com.shopping;

import java.io.IOException;
import java.util.*;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class ShoppingServlet
 */
@WebServlet("/ShoppingServlet")
public class ShoppingServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;


    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        final Map<Integer,String> products= new HashMap<Integer,String>();
        products.put(1, "Motorola MotoX");
        products.put(2, "Google Pixel2");
        products.put(3, "Essential");
        products.put(4, "Iphone 6");
        request.setAttribute("product", products);
    }

}

Browse.jsp

<%@ 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">
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
Click on the products to add to the Cart.
<br/>
<c:forEach items="${product}" var="pro">
    ${pro.value};
    <br/>
</c:forEach>
</body>
</html>

The problem is it's not displaying map values in the jsp. it's only displaying Click on the products to add to the Cart but the valus are missing..

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Stone
  • 79
  • 9
  • Please don't use [java] tag as long as problem is not demonstrable using a plain vanilla Java application class with main() method. – BalusC Jan 20 '18 at 18:04

1 Answers1

-1

You can display it on jsp page using scriptlets.

<%@ 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">
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
Click on the products to add to the Cart.
<br/> <% for(String key: product.getKeys()){            out.println(product.get(key)+"<br/>");  } %>
</body>
</html>
Ketan Bhavsar
  • 5,338
  • 9
  • 38
  • 69
  • Please catch up. Scriptlets are officially discouraged since 2003. That's 15 years ago! See also https://stackoverflow.com/questions/3177733/how-to-avoid-java-code-in-jsp-files – BalusC Jan 20 '18 at 18:03