I'm unable to pass variables from a.jsp to b.jsp, following is what I tried and the output. Can someone please help? Thanks.
a.jsp
start including
<jsp:include page="b.jsp">
<jsp:param name="somevar" value="zzzz" />
</jsp:include>
stop including
b.jsp
${somevar}
${param.somevar}
<%=request.getParameter("somevar")%>
output
start including
null
stop including
You have to set your param before include.
start including
<jsp:params>
<jsp:param name="somevar" value="zzzz" />
</jsp:params>
<jsp:include page="b.jsp">
</jsp:include>
stop including
And can u try this in b.jsp:
<%=request.getParameter("somevar");%>
What is output?
Try the following code:
home.jsp
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%# page session="false"%>
<html>
<head>
<title>Home</title>
</head>
<body>
<h1>Hello world!</h1>
<jsp:include page="b.jsp">
<jsp:param name="something" value="something" />
</jsp:include>
<h1>Bye World</h1>
</body>
</html>
In the b.jsp page use the following code.
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!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 Param example</title>
</head>
<body>
<h1>
<c:out value="${param.something}" />
</h1>
</body>
</html>
The output must be
Hello world!
something
Bye World
Try to avoid java code in JSP pages as it is not advisable. For more details, have a look at this answer How to avoid Java Code in JSP-Files?
Related
JSP is not getting parsed on browser. Instead show the directive directly.
Can anyone help me out
<%# 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">
<html>
<head>
<title>
Sample
</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>
<button id="generateTicket">Generate Ticket</button>
<div id='ticket'></div>
<script>
$("#generateTicket").click(
function(){
console.log("Generate ticket");
$("#ticket").load("generateTicket");
});
</script>
</body>
</html>
below is the image of pom.xm
I am trying to use JSTL for my index.jsp page, but for some reason each time after packaging project to .war and running with Tomcat it gives me following errors:
HTTP Status 500 - /index.jsp (line: 12, column: 0) Unterminated <c:if tag
or
HTTP Status 500 - java.lang.ClassNotFoundException: org.apache.jsp.index_jsp
From what I found on google, there are 2 ways to install JSTL into your Maven project:
1) Add this to pom.xml
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
2) Add some jars to WEB-INF/lib, but here's the problem: no such folder was automaticaly created and if I do it manually it does not help. Project structure looks like this:
The code of index.jsp is the following:
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title><c:if> Tag Example</title>
</head>
<body>
<c:set var="salary" scope="session" value="${2000*2}"/>
<c:if test="${salary > 2000}">
<p>My salary is: <c:out value="${salary}"/><p>
</c:if>
</body>
</html>
So, what should I do to make those work? I can't find any guide or piece of information that could help me solving this. Thanks for having a look at my problem!
Just what compiler tells you: Unterminated c:if tag:
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title><c:if> Tag Example</title>
</head>
<body>
<c:set var="salary" scope="session" value="${2000*2}"/>
<c:if test="${salary > 2000}">
<p>My salary is: <c:out value="${salary}"/><p>
</c:if>
</body>
</html>
Look at line 4 of your jsp:
<title><c:if> Tag Example</title>
It must be
<title><c:if> Tag Example </c:if></title>
EDIT: As seems that I explained badly, I have tested in a running web-app, and this way (that is just what I was trying to explain) works:
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title><c:if> Tag Example</title>
</head>
<body>
<c:set var="salary" scope="session" value="${2000*2}"/>
<c:if test="${salary > 2000}">
<p>My salary is: <c:out value="${salary}"/><p>
</c:if>
</body>
</html>
I have a Spring + Apache Tiles 3 Aplication.
I have a layout and inside of this, the header, the body and the footer:
defaultLayout.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%# page isELIgnored="false" %>
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%# taglib prefix="tiles" uri="http://tiles.apache.org/tags-tiles" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title><tiles:getAsString name="title" /></title>
<link href="<c:url value='/static/css/bootstrap.css' />" rel="stylesheet"></link>
<link href="<c:url value='/static/css/app.css' />" rel="stylesheet"></link>
</head>
<body>
<header id="header">
Web Application Context Path="${pageContext.request.contextPath}"
<tiles:insertAttribute name="header" />
</header>
<section id="sidemenu">
<tiles:insertAttribute name="menu" />
</section>
<section id="site-content">
<tiles:insertAttribute name="body" />
</section>
<footer id="footer">
<tiles:insertAttribute name="footer" />
</footer>
</body>
</html>
The "${pageContext.request.contextPath}" works fine inside the Layout, but not on the JSP children's:
menu.jsp
<nav>
<img class="logo" src="${pageContext.request.contextPath}/static/img/Linux-icon.png">
<ul id="menu">
<li>Home</li>
<li>Products</li>
<li>Contact Us</li>
</ul>
This prints the "${pageContext.request.contextPath}" "as is" in the result HTML.
Try to use <%# page isELIgnored="false" %>. It will solve your problem
Since you mentioned HTML code and say that the context path is printed "as-is", is this code in HTML file? Then it will not work.
Check this question - ${pageContext.request.contextPath} is not working on plain HTML
I have tried 2 ways to load external css or js files in my spring project but neither of them is working fine.
Home.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%#taglib uri="http://java.sun.com/jstl/core" prefix="c"%>
<%#taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<!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">
<link rel="stylesheet" type="text/css"
href="/resources/css/font-awesome.min.css" />//NOT LOADING
<link rel="stylesheet" href="<c:url value="resources/css/style.css"/>" />//Not loading Method tried 1
<link rel="stylesheet"
href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<link rel="stylesheet" href="/resources/css/style.css">//Not loading Method tried 2
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"> </script>
I can see background images in design time. But can't see images in run time. I am running my application from asp.net development server.
I am applying background images by using css.
What is the solution?
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="BackgroundImageTest._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
<link rel="stylesheet" href="Stylesheet1.css" type="text/css" media="screen" />
</head>
<body>
<form id="form1" runat="server">
<div>
abcdef
</div>
</form>
</body>
</html>
div
{
background-image:url(App_LocalResources/database.1.jpg);
width:400px;
height:400px;
}
Did you check if the PATH is still there in runtime ?