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.
Related
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 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".
Am trying to link from a JSP to a servlet . On clicking the button with the name="conf" I need to redirect to a servlet "/Initial" . The problem is when I use type="button" nothing happens, while when I use type="submit" the page gets directed to servlet "/Initial" and does the action there. Am not able to identify the problem.
Here is my code:
<%# page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%# page import="reg.serv.*"%>
<!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>
</head>
<body>
<form method="post">
<center>
<table border="1" width="30%" cellpadding="3">
<thead>
<tr>
<th colspan="2">Register Here</th>
</tr>
</thead>
<tbody>
<tr>
<td>Username</td>
<td><input type="text" class="" id="username" name="username1" value="" /></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="password1" id="password" value="" /></td>
</tr>
<tr>
<td>Confirm Password</td>
<td><input type="password" name="confirmpassword1" id="confirmpassword" value="" /></td>
</tr>
<tr>
<td>Mobile Number</td>
<td><input type="text" class="" id="mob" name="mob1" value="" /></td>
</tr>
<tr>
<td>Email ID</td>
<td><input type="text" class="" id="email" name="email1" value=" " /></td>
</tr>
<tr>
<td>Address</td>
<td><textarea id="address" name="address1"></textarea></td>
</tr>
<tr>
<td colspan="2">Already registered Login Here</td>
</tr>
</tbody>
<tr>
<td><input type="button" value="confirm" name="conf" /></td>
<td><input type="reset" value="Reset" /></td>
<td><input type="button" value="Cancel" name="Cr" onclick="openPage('Initial.jsp')" /></td>
</tr>
</table>
</form>
<script type="text/javascript">
function openPage(pageURL) {
window.location = pageURL;
}
</script>
<%
String x = request.getParameter("conf");
if (x != null && x.equals("confirm")) {
//response.sendRedirect("/Initial");
RequestDispatcher dispatcher = request.getRequestDispatcher("/Initial");
dispatcher.forward(request, response);
}
%>
</body>
</html>
Please help me . Any help would be greatly appreciated. Thanking You.
you have to write
<form action=/your_servlet_page_name>
And you have to use
<input type="submit" value="confirm" name="conf"/>
And also you have to map your servlet page into web.xml file like
<servlet-mapping>
<servlet-name>CheckLogin</servlet-name>
<url-pattern>/CheckLogin</url-pattern>
</servlet-mapping>
<form action = "servlet-name" method = "method in the servlet">
<input type ="submit" value = "val">
</form>
This is a simple way to do this. If you are using the latest jre i think 7 and up, you don't need to declare the servlet in your web.xml file. the #WebServlet("/servlet-url") will do the trick.
if you want to use type="button" instead of type="submit". you can use javascript function on the click on the button. Like
<script>
function doSubmit(){
var actionURL ="MENTION URL YOU WANT TO REDIRECT";
// perform your operations
myForm.submit(actionURL); OR
myForm.submit();
}
</script>
<form name="myForm">
<input type="button" name="conf" value="conf" obclick="doSubmit();">
</form>
hope it will help you.
try by changing the script only
<script type="text/javascript">
function openPage(pageURL)
{
window.location.href = pageURL;
}
</script>
function openPage(pageURL) {
window.location = pageURL;
}
In above code snippet, pageURL has to be an absolute URL which in case of dealing with servlets may vary.So instead of this below can be used
location.href = (location.href).substr(0, (location.href).lastIndexOf('xyz.jsp'))+"/abc";
Here 'abc' is the servlet to which we have to redirect the 'xyz.jsp' .This can even work if there are many buttons.The respective functions could be written to redirect to respective servlets.
Also it works in case of input type is "button" or "submit".
I dont get exactly what you are trying to do,but redirect is working with:
response.sendRedirect(request.getContextPath());
or
response.sendRedirect(String url);
I am a newbie trying to create a simple web application using JSP and Mysql.
I am using following thing:
Eclipse EE IDE
Tomcat 7.0
Dtabase: MySQL
MySQL Connector
In my page I have row user add row dynamically that s work fine. I want my values should get stored in database after submit.I have searched lot on net but not find anything which can solve problem.I am giving code of current page.I know have to do connection also however I tried but not succeeded.
Thanks in advance.
JSP & Javascript:
<%# 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>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#add").click(function() {
$('#mytable tbody>tr:last').clone(true).insertAfter('#mytable tbody>tr:last');
$('#mytable tbody>tr:last #name').val('');
$("#mytable tbody>tr:last").each(function() {this.reset();});
return false;
});
});
</script>
</head>
<body>
<form>
<a id="add" href="javascript:void(0)">Add another Credit card</a>
<table id="mytable" width="300" border="1" cellspacing="0" cellpadding="2">
<tbody>
<tr class="person">
<td><input type="text" name="name" id="name" /></td>
<td><input type="button" value="name" /></td>
<td><select>
<option>value1</option>
<option>value2</option></select>
<td><input type="text" name="name" id="name" /></td>
</tr>
</tbody>
</table>
<input type="submit" value="submit">
</form>
Logout
</body>
</html>
I'm brand new to the javascript. I'm trying to call a javaScript function from my jsp.Its not working. I was trying to debug it through bugzila. It saying "No Javascript on this page"
<%# 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>
<script type="text/javascript" src="ajaxs.js"></script>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
</head>
<body>
<%
out.println("Bismillahir Rahmanir Rahim");
%>
<jsp:useBean id="numBean" class="beans.NumberBean">
<jsp:setProperty name="numBean" property="*" />
</jsp:useBean>
<form action="index.jsp" method="post">
<table>
<tr>
<td>Enter Integers:</td>
<td><input type="text" id="inputString" name="inputString"
value="<jsp:getProperty name="numBean" property="inputString" />"
/> </td>
<td>(Use semi-colon to separate number)</td>
</tr>
<tr>
<td>M - number of largest element to find.:</td>
<td>
<input type="text" id="nthHighest" name="nthHighest"
value="<jsp:getProperty name="numBean" property="nthHighest" />" />
</td>
</tr>
</table>
<input type="submit" value="Identify" onclick="ajaxFunction()" />
<div id="result"></div>
</form>
</body>
</html>
I'm trying to call "ajaxFunction()" on onclick event.
I included js file as following at the top:
<script type="text/javascript" src="ajaxs.js"></script>
can u pls help me to identify what I'm doing wrong here...
Thanks in advance
Try with including script tag with "src="ajaxs.js" (your current script that including "src="ajaxs.js") within head tag.The contents within head tag load at the loading page .
thanks....