I have a web.xml that calls selectfoods.jsp first, the there is this form:
<form name="ingredientsform" method="post" action="table.jsp">
<select name="ingredients" multiple>
<option value="tofu">Tofu</option>
<option value="pepper">Pepper</option>
<option value="spaghetti">Spaghetti</option>
<option value="paprika">Paprika</option>
<option value="onion">Onion</option>
<option value="beef">Beef</option>
<option value="mushrooms">Mushrooms</option>
</select>
<input type="submit">
</form>
which forwards to table.jsp where I would like to print out the ingredients selected but no error appears just an empty page here is the relevant code in the table.jsp
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Table</title>
</head>
<body>
<table>
<thead> <td> <b> Products </b></td></thead>
<%
String items[] = (String[]) request.getAttribute("ingredients");
for (int i = 0; i < items.length; i++)
{
%>
<tr> <td> <% out.println(items[i]); %> </td> </tr>
<%
}
%>
</table>
</body>
</html>
I would recommend using servlet-JSP MVC model for developing an application.
With MVC it would be easy to make a separate view and business logic and also it is easy to handle also.
To get parameter from submitted form , request.getParameter() is used not request.getAttribute().
Here you have make multiple selections so you have to use request.getParameterValues() which will fetch all selected values.
Related
In my system i need to use a java for loop inside an html table to print time from 8.00-17.00. Can someone please explain me how to do it.Thank you.
Html file
<!DOCTYPE html>
<html xmlns:th="https://www.thymeleaf.org">
<head>
<link rel="stylesheet" type="text/css" href="static/css/timeTable.css" th:href="#{/css/timeTable.css}">
<meta charset="UTF-8">
<title>Time Table</title>
</head>
<body>
<div class="container" style="margin-top:30px">
<br><br>
<table border="1" class="table2">
<thead>
<tr>
</tr>
<br>
<tr>
<td></td>
<th>Monday</th>
<th>Tuesday</th>
<th>Wednesday</th>
<th>Thursday</th>
<th>Friday</th>
</tr>
</thead>
<tbody>
<tr>
<% for(int i=0;i<17;i++){
<td> i.":00 - ".($i+1).":00" </td>
}
</tr>
</tbody>
</table>
</div>
</body>
</html>
You should install JSTL as follows:
<%#taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
and then iterate over the list as follows:
<c:forEach var = "i" begin = "8.00" end = "17.00">
Item <c:out value = "${i}"/><p>
</c:forEach>
I was able to do it by using the following way. Thx everyone who tried to help
<option th:each="i : ${#numbers.sequence( 8, 17)}">
<tr>
<th th:text="${ i }+':00 - ' + ${ i+1 }+':00'"></th>
</tr>
</option>
Why don't you use Javascript instead of going nut with inventing stuff like that?
Javascript is a powerfull language and have a for loop as well.
let to the front which belongs to the front.
I am currently working on a small web app project. The homepage has a link that, once clicked, calls a servlet that retrieves data from a database (in the form of an arraylist of objects), adds arraylist to session(as customers), and redirects to a jsp where the objects (customers) are displayed in a table. There is a "more details" column where each row has a button which submits a form, to another jsp, with a hidden input value with customerNumber as its value and num as its name.
I want, in the second jsp, to loop over all customers and only display the one whose customerNumber matches the one sent from the first jsp.
Current code:
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<% String hidden = request.getParameter("num"); %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Customer Details Page</title>
</head>
<body>
<div>
<c:forEach var="c" items="${customers}">
<c:if test = "${hidden == c.customerNumber}">
<table>
<thead>
<tr>
<th>Customer Phone</th><th>Customer Country</th>
</tr>
</thead>
<tbody>
<tr>
<td>${c.phone}</td><td>${c.country}</td>
</tr>
</tbody>
</table>
</c:if>
</c:forEach>
</div>
</body>
</html>
The above code only prints the table heading. Any help appreciated.
Found the issue, was trying to access variable from scriplet directly from jstl tags. The following code works fine:
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
String hidden = request.getParameter("num");
pageContext.setAttribute("reqNum",hidden);
%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Customer Details Page</title>
</head>
<body>
<div>
<c:forEach var="c" items="${customers}">
<c:if test = "${reqNum == c.customerNumber}">
<table border="1">
<thead>
<tr>
<th>Customer Phone</th><th>Customer Country</th>
</tr>
</thead>
<tbody>
<tr>
<td><c:out value="${c.phone}" /></td><td><c:out value="${c.country}" /></td>
</tr>
</tbody>
</table>
</c:if>
</c:forEach>
</div>
</body>
</html>
Instead of using
<td>${c.phone}</td><td>${c.country}</td>
You can try:
<td><c:out value="${c.phone}" /></td><td><c:out value="${c.country}" /></td>
I am having task that i have to move the selected row to top in table and when i refresh the page i want to restore the previous changes.
I completed the moveRow logic.
But i am not getting how to restore the row data, please assist me for this.
Below is my code
<%# 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>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<script language="JavaScript">
var whichrow = false;
var TableLocation;
function thisrow(x)
{
TableLocation = x.sectionRowIndex;
}
function MoveUp()
{
var tablebody = document.getElementById('table1');
if(TableLocation > 0)
{
tablebody.moveRow(TableLocation, 1);
}
}
</script>
</head>
<body>
<form>
<table id="table1" name="table1" border="1">
<tr>
<th>Name</th>
<th>Address</th>
<th>Select</th>
</tr>
<tr id="tr3" onclick="thisrow(this)">
<td><input type="text" name="name"></td>
<td><input type="text" name="address"></td>
<td><input type="checkbox" name="checkbox1"></td>
</tr>
<tr id="tr4" onclick="thisrow(this)">
<td><input type="text" name="name"></td>
<td><input type="text" name="address"></td>
<td><input type="checkbox" name="checkbox1"></td>
</tr >
<tr id="tr5" onclick="thisrow(this)">
<td><input type="text" name="name"></td>
<td><input type="text" name="address"></td>
<td ><input type="checkbox" name="checkbox1"></td>
</tr>
</table>
<input type="button" value="move row up" onClick="MoveUp();">
</form>
</body>
</html>
Thanx in advance...
looking forword for your reply.
You need to save your changes on the server to reload your page (you don't need to save on database, you can store it in the session and use use on the next page rendering). Then you can use your javascript to move up/down your rows.
I suggest you to put data-attributes in your html tags to order it in javascript or order on the server before rendering.
I have a drop down menu in my JSP and instead of hardcoding the values with text I would like to call constants from a class. Here is a snippet of my constants class called master.dao.util.MasterDataConstants
//DIVISIONS FOR DROPDOWN
public static final String DIVISION_TYPE_DROPDOWN_AUDIT_MANAGEMENT_GLOBAL_ID = "Audit Management - Global";
public static final String DIVISION_TYPE_DROPDOWN_CHANGE_MANAGEMENT_GLOBAL_ID = "Change Management - Global";
public static final String DIVISION_TYPE_DROPDOWN_DEA_MANAGEMENT_GLOBAL_ID = "DEA Management - Global";
public static final String DIVISION_TYPE_DROPDOWN_EHS_MANAGEMENT_GLOBAL_ID = "EH&S Management - Global";
public static final String DIVISION_TYPE_DROPDOWN_EVENT_MANAGEMENT_GLOBAL_ID = "Event Management - Global";
And here is my JSP Page:
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%#taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%# page import="java.sql.*"%>
<%# page import="java.io.*"%>
<%# page import="java.util.*"%>
<%# page import="javax.servlet.*"%>
<%# page import="master.dao.MasterDataDao"%>
**<%# page import="master.dao.util.MasterDataConstants"%>**
<%# page import="master.dto.SiteDto"%>
<!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>Master Data</title>
</head>
<script>
</script>
<body>
<form name="input" action="getMasterData" method="get">
<br />
<br />
<h1 align='center'>Master Data File</h1>
<br />
<br />
<table border="0" align='center'>
<tr>
<td>
<h2>Site Name</h2>
</td>
<td align='left'>
<jsp:useBean id="masterDao" clas s="master.dao.MasterDataDao"/>
<select name="siteId" id="siteId">
<option value="0">ALL</option>
<c:forEach items="${masterDao.allSites}" var="siteDto">
<option value="${siteDto.id}">${siteDto.name}</option>
</c:forEach>
</select></td>
</tr>
<tr>
<td>
**<h2>Division</h2>
</td>
<td align='left'>
<jsp:useBean id="masterDaoUtil" class="master.dao.util.MasterDataConstants"/>
<select name="divisionId" id="divisionId">
<option value="33">${MasterDataConstants.DIVISION_TYPE_DROPDOWN_AUDIT_MANAGEMENT_GLOBAL_ID} </option>
<option value="31">${MasterDataConstants.DIVISION_TYPE_DROPDOWN_CHANGE_MANAGEMENT_GLOBAL_ID} </option>
<option value="34">${MasterDataConstants.DIVISION_TYPE_DROPDOWN_DEA_MANAGEMENT_GLOBAL_ID}</option>
<option value="35">${MasterDataConstants.DIVISION_TYPE_DROPDOWN_EHS_MANAGEMENT_GLOBAL_ID}</option>
<option value="23">${MasterDataConstants.DIVISION_TYPE_DROPDOWN_EVENT_MANAGEMENT_GLOBAL_ID}</option>**
</select></td>
</tr>
</table>
<br />
<br />
<div style="text-align: center">
<input type="submit" value="Submit">
</div>
</form>
</body>
</html>
When I execute this page I get blank values for the second dropdown labeled Division. I have copied down the portion from the JSP that represents division below:
<td>
<h2>Division</h2>
</td>
<td align='left'>
<jsp:useBean id="masterDaoUtil" class="master.dao.util.MasterDataConstants"/>
<select name="divisionId" id="divisionId">
<option value="33">${MasterDataConstants.DIVISION_TYPE_DROPDOWN_AUDIT_MANAGEMENT_GLOBAL_ID}</option>
<option value="31">${MasterDataConstants.DIVISION_TYPE_DROPDOWN_CHANGE_MANAGEMENT_GLOBAL_ID}</option>
<option value="34">${MasterDataConstants.DIVISION_TYPE_DROPDOWN_DEA_MANAGEMENT_GLOBAL_ID}</option>
<option value="35">${MasterDataConstants.DIVISION_TYPE_DROPDOWN_EHS_MANAGEMENT_GLOBAL_ID}</option>
<option value="23">${MasterDataConstants.DIVISION_TYPE_DROPDOWN_EVENT_MANAGEMENT_GLOBAL_ID}</option>
</select></td>
I'm not sure exactly what I am missing. Please help me with this. Thanks in advance. Please let me know if I have provided enough information or if more is needed.
Thanks again
Have you missed to import the class?
<%# page import="master.dao.util.MasterDataConstants" %>
Create getter methods in the MasterDataConstants class corresponding to each constant.
For example as shown below. Do in the same way for others as well.
MasterDataConstants.java
public static final String DIVISION_TYPE_DROPDOWN_AUDIT_MANAGEMENT_GLOBAL_ID = "Audit Management - Global";
public String getDIVISION_TYPE_DROPDOWN_AUDIT_MANAGEMENT_GLOBAL_ID() {
return DIVISION_TYPE_DROPDOWN_AUDIT_MANAGEMENT_GLOBAL_ID;
}
JSP:
${masterDaoUtil.getDIVISION_TYPE_DROPDOWN_AUDIT_MANAGEMENT_GLOBAL_ID()}
Please have a look at accessing constants in JSP (without scriptlet)
This is my code for index.jsp. I want it so that when I select an option in the drop-down menu, the value should be printed out and also the value should be set. For example, if we select "grapes" then it should print Grapes and set the value to Grapes. I have tried many things but have been unable to do so.
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form method="post" action="index.jsp" name="productForm">
<select name="colour" onchange="document.productForm.submit();">
<option value="dropdown">Pls select one
<option value="apple">Apple
<option value="oragne">Orange
<option value="grapes">Grapes
</select>
<input type="hidden" name="dropdown" id="dropdown">
<input type="submit" value="click" name="dropdown" id="dropdown">
<form>
<%
String colour = request.getParameter("colour");
out.println(colour);
%>
</body>
</html>
Try This and let me know..
<script type="text/javascript">
function setValue(){
document.getElementById("dropdown").value=document.getElementById("colour").value;
document.productForm.submit();
return true;
}
</script>
<form method="post" action="index.jsp" name="productForm">
<select id="colour" name="colour" onchange="return setValue();">
<option value="dropdown">Pls select one
<option value="apple">Apple
<option value="oragne">Orange
<option value="grapes">Grapes
</select>
<input type="hidden" name="dropdown" id="dropdown">
<input type="submit" value="click" name="btn_dropdown">
<form>
<%
String colour = request.getParameter("colour").toString();
out.println(colour);
%>
form and all option tags are not closed
Example for correct markup of option:
<option value="apple">Apple</option>
Your updated code should be something like this:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form method="post" action="index.jsp" name="productForm">
<select name="colour" id="dropdown">
<option value="dropdown">Pls select one</option>
<option value="apple">Apple</option>
<option value="oragne">Orange</option>
<option value="grapes">Grapes</option>
</select>
<input type="submit" value="click">
</form>
<%
String colour = request.getParameter("colour");
out.println(colour);
%>
<script>
document.getElementById("dropdown").value = '<% out.print(colour); %>';
</script>
</body>
</html>
1st thing first, If you want to set value to the dropdown on change of value, well it does that itself. But if you want to set the value selected to session, again it does that itself all you have to do is use request.getParameter(color) on the next page or backend where you processing request. For printing on console, you have correct code. Only close your tags properly.