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.
Related
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.
I have one question about spring tag form and radiobuttons. I have a task something like testing. User logged in and can choose some test. On the test page i'm using spring tag form and radiobuttons for answering questions. For one question 4 answers. But on the test page when i'm trying to select one answer for question one is selected for all question. I mean i can select only one radio button. I know that in html for create group of radio buttons they should have same names. How i can create a group of radio buttons with spring tag form?
Here test_page.jsp
<%# page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" language="java" %>
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%# taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<html>
<head>
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.0/js/bootstrap.min.js"></script>
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="/resources/js/timer.js" type="text/javascript"></script>
<link rel="stylesheet" type="text/css" media="screen" href="/resources/styles/style.css"/>
<title>Test page</title>
</head>
<body onload="startTimer()">
<div id="info">
<h3>
Subject: <c:out value="${test.subject}"/>
</h3>
<h3>
Number of questions:
<c:out value="${test.questionNumber}"/>
</h3>
<h3>
Difficulty:
<c:out value="${test.difficulty}"/>
</h3>
<div id="clock">
<p>
<label for="timer">Time for test</label>
<span id="timer" style="color: #4af; font-size: 150%; font-weight: bold;">
<c:out value="${test.timer}"/></span>
</p>
</div>
</div>
<div id="test">
<form:form action="result" commandName="result" method="post">
<form:input type="hidden" path="testSubject" value="${test.subject}"/>
<form:input type="hidden" path="difficulty" value="${test.difficulty}"/>
<c:forEach items="${test.questionAnswersMap}" var="entry">
<h4><c:out value="${entry.key}"/></h4>
<div class="form-check">
<ul style="list-style: none">
<form:radiobuttons path="answers" items="${entry.value.existingAnswers}" element="li" class="form-check-input"/>
</ul>
</div>
</c:forEach>
<button id="finish" type="submit" class="btn btn-primary btn-block">Finish</button>
</form:form>
</div>
</body>
</html>
I'm using spring 4.
If someone knows how to solve that i will be grateful.
I want to get table data from table to new database through jsp servlet this is my sample code in jsp this table contains database elements
I struggled for a week. Please help me.
I want to get table data from table to new database through jsp servlet this is my sample code in jsp this table contains database elements
<%#page import="java.sql.Connection"%>
<%#page import="java.sql.PreparedStatement"%>
<%#page import="Servlets.Db"%>
<%#page import="java.sql.ResultSet"%>
<%# 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>
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
<script type="text/javascript" src="js/jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<div class="row">
<h1>Place your Order</h1>
<div class="jumbotron">
<form action="order" method="post">
<!--
<label>name</label>
<input class="form-control" name="name" type="text" >
<label>Member Id</label>
<input class="form-control" name="memberid" type="text" >
<label>Orders</label>
<div class="container">
<div class="row">
<div class="jumbotron">
-->
<table class="table">
<tr>
<th>id</th>
<th>name</th>
<th>quantity</th>
</tr>
<%
Connection con=Db.getCon();
String sql="SELECT * FROM drugs";
PreparedStatement ps=con.prepareStatement(sql);
ResultSet rs=ps.executeQuery();
while(rs.next()) {
%>
<tr>
<td><input type="text" name="id" value="<%=rs.getInt("id") %>"></td>
<td><input type="text" name="name" value="<%=rs.getString("name") %>"></td>
<td><input type="text" name="quntity"></td>
</tr>
<%
}
%>
</table>
<input type="submit" value="submit order">
</form>
</div>
</div>
<div class="row">
<%# include file="WEB-INF/Footer.jsp" %>
</div>
</div>
</body>
</html>
The work you done is get data from database and show data in jsp,if you want to put data into database,you should change sql to "add" and jsp to "input".
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)
Spring WebFlow2 Javascript only working on first radio button
I am trying to use Spring JavaScript that comes with WebFlow2 to submitted my page with a transition value if the user clicks on one of the radio buttons.
I inserted my javascript on the page using onclick and onchange and it only works if the first radio button is selected. I dont know why but I think I tried everything... can someone please tell me if this is a issue with Spring JavaScript.
JSP:
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%# taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<html>
<head>
<title>Spring 3.0 MVC - Web Flow Example</title>
<script type="text/javascript"
src="<c:url value="/resources/dojo/dojo.js" />">
</script>
<script type="text/javascript"
src="<c:url value="/resources/spring/Spring.js" />">
</script>
<script type="text/javascript"
src="<c:url value="/resources/spring/Spring-Dojo.js" />">
</script>
<link type="text/css" rel="stylesheet"
href="<c:url value="/resources/dijit/themes/tundra/tundra.css" />" />
</head>
<body>
<h2>Customer Registration</h2>
<form:form commandName="customer" id="customer">
<input type="hidden" name="_flowExecutionKey"
value="${flowExecutionKey}" />
<table>
<tr>
<td><font color=red><form:errors path="name" /></font><b>Name:
</b></td>
<td><form:input path="name" id="name" /> <script
type="text/javascript">
Spring
.addDecoration(new Spring.ElementDecoration(
{
elementId : "name",
widgetType : "dijit.form.ValidationTextBox",
widgetAttrs : {
promptMessage : "This is the name you would like entered into the system."
}
}));
</script> <br />
<p></td>
</tr>
<tr>
<td><font color=red><form:errors path="phoneNumber" /></font>
<b>Phone number: </b></td>
<td><form:input path="phoneNumber" id="phoneNumber" /><br />
<script type="text/javascript">
Spring
.addDecoration(new Spring.ElementDecoration(
{
elementId : "phoneNumber",
widgetType : "dijit.form.ValidationTextBox",
widgetAttrs : {
promptMessage : "This is the phone number for the above name"
}
}));
</script></td>
</tr>
<tr>
<td></td>
</tr>
<tr>
<td><b>Row:</b></td>
<td><form:radiobutton value="B" path="row" id="rowBtn" />Row: B<BR>
<form:radiobutton value="A" path="row" id="rowBtn" />Row: A<BR>
<script type="text/javascript">
Spring.addDecoration(new Spring.AjaxEventDecoration({
elementId: "rowBtn",
event: "onchange",
formId:"customer",
params: {fragments:"body", _eventId: "proceed"}
}));
</script>
<script type="text/javascript">
Spring.addDecoration(new Spring.AjaxEventDecoration({
elementId: "rowBtn",
event: "onclick",
formId:"customer",
params: {fragments:"body", _eventId: "proceed"}
}));
</script>
</td>
</tr>
<tr>
<td>Year of Birth:</td>
<td>
<form:select path="byear">
<form:option value="2012" label="2012" />
<form:option value="2011" label="2011" />
<form:option value="2010" label="2010" />
</form:select>
<script type="text/javascript">
Spring.addDecoration(new Spring.AjaxEventDecoration({
elementId: "byear",
event: "onchange",
formId:"customer",
params: {fragments:"body", _eventId: "proceed"}
}));
</script>
</td></tr>
</table>
<input type="submit" name="_eventId_proceed" value="proceed"
id="proceed" />
<input type="submit" name="_eventId_cancel" value="Cancel" />
</form:form>
</body>
</html>
You have:
<form:radiobutton value="B" path="row" id="rowBtn" />Row: B<BR>
<form:radiobutton value="A" path="row" id="rowBtn" />Row: A<BR>
Your two radiobuttons have the same element id.
The AjaxEventDecoration does a lookup based on that elementId. It assumes it is unique on the page and it does not expect multiple elements with the same id. You should give the second radiobutton another id.
You should probably just reenter the AjaxEventDecoration for that id as well.
If Spring.AjaxEventDecoration allows you to do something similar based on element class(instead of elementId) maybe you can do that, instead of duplicating the current Spring.AjaxEventDecoration snippet.