Reputation: 4743
I am trying to answer a question using expression language in JSP. I tried some code, but it is not working properly. Can someone please help me ?
Question - Source = How to call servlet on jsp page load
I want to call a servlet 'latest_products' on load of index.jsp page.This servlet has records in List. I want to pass this List to index.jsp. but i don't want to display the name of servlet in url. is there any way by which i can do this.
Expected output - Heading plus list of three product names.
Actual output - Heading only.
What I tried -
Servlet:
public class ProductList extends HttpServlet {
private static final long serialVersionUID = 1L;
public ProductList() {
super();
// TODO Auto-generated constructor stub
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List<String> products = new ArrayList<String>();
products.add("Car");
products.add("Gun");
products.add("Shades");
request.setAttribute("productsList", products);
}
}
JSP:
<%@ page language="java" contentType="blah..."%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "blah...">
<html>
<head>
<meta http-equiv="blah...">
</head>
<body>
<c:import url="/ProductList" />
<c:set var="myProducts" scope="request" value="${param.productsList}"/>
<h1>List of products from servlet</h1>
<c:forEach var="product" items= "${myProducts}" varStatus="i">
${product}<br>
</c:forEach>
</body>
</html>
One small question besides this, why do I get an error Encountered illegal body of tag "c:set" tag, given its attributes
when I enclose the c:set tag with a </c:set>
after the </c:foreach>
tag ? I thought the c:set was like a block of code with a scope. But, it does not seem to be.
Thanks.
Upvotes: 2
Views: 1340
Reputation: 46841
Try with
<c:set var="myProducts" value="${requestScope.productsList}" />
or
<c:set var="myProducts" scope="request" value="${productsList}" />
instead of
<c:set var="myProducts" scope="request" value="${param.productsList}" />
to get the attribute value from the request scope. Only request parameters are made available in the implicit object param
. You can't access request attribute using the implicit object param
.
why do I get an error Encountered illegal body of tag
c:set
tag, given its attributes when I enclose the c:set tag with a</c:set>
after the</c:foreach>
tag ?
because It's already closed as shown below (look a slash at the end) and there is no opening tag for c:set
.
<c:set var="myProducts" scope="request" value="${param.productsList}"/>
<c:import url="/ProductList" />
or <jsp:include page="/ProductList" />
are GET requests that will call doGet()
method of Servlet and that is empty as per your code that's why nothing is shown on the JSP page.
Either call doPost()
method from the doGet()
method or provide the implementation of doGet()
method as well.
Upvotes: 2