• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

How can I pass variables in Javascript function to another page scriptlet?

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Index.jsp
<%@page import="java.sql.PreparedStatement"%>
<%@page import="com.sun.faces.context.RequestParameterValuesMap"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="uts.isd.model.dao.DBManager"%>
<%@page import="uts.isd.model.dao.DBConnector"%>
<%@page import="java.sql.Connection"%>
<%@page import="uts.isd.controller.*"%>
<%@page import="java.util.*"%>
<%@page contentType="text/html" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
   <head>
       <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
       <link rel="stylesheet" href="css/demo.css">
       <script type="text/javascript" src="js/script.js"></script>
       <title>Welcome Page</title>
   </head>
   <body onload="startTime()">        
       <div><span class="time" id="time" ></span></div>
       <h1>welcome to Logs Page</h1>
       <h2 align="center"><font><strong>Retrieve login data</strong></font></h2>
       <table align="center" cellpadding="5" cellspacing="5" border="1">
       <tr bgcolor="#7FFF00">
       <td><b>Log record ID</b></td>
       <td><b>Login Date and Time</b></td>
       <td><b>Logout Date and Time</b></td>
       <td><b>Select record<b></td>
       </tr>
       
       <%try{
           DBConnector connector = new DBConnector(); //Create a connection and initialize DB conn-field
           Connection conn = connector.openConnection(); //Get the protected connection instance from DB superclass to share for the application classes
           DBManager db = new DBManager(conn);
           
           //String UserID = request.getParameter("ID");
           String UserID = "C012311";
           java.sql.Statement stmt = conn.createStatement();
           String sql = "Select * from ROOT.\"Loginlogs\" where ID = " + "'" + UserID+ "'" ;
           ResultSet rs = stmt.executeQuery(sql);
           while (rs.next()){
       %>
       
               <tr bgcolor="#DEB887">
               <td><%=rs.getInt("LogID") %></td>
               <td><%=rs.getString("LOGIN_DATE") %></td>
               <td><%=rs.getString("LOGOUT_DATE") %></td>  
               <td><button type="button" id="selectID" onclick="Delete(value)" value="<%=rs.getInt("LogID") %>" >Delete</td>
               </tr>
       <%
       }
       } catch (Exception ex) {
       ex.printStackTrace();
       }
       %>
       </table>

       <button type="button" name="Refresh" onClick="Refresh()">Clear</button>
       
       <br>
       <script type="text/javascript">
           function Delete(value){
               if(confirm("Do You Want to Delete this record? LogID " + value))
               {
                   var LogID = value;
                   document.getElementById("LogID").value = LogID;
                   window.location.replace("Delete.jsp?value="+LogID);
               }
               else {return false;}
           }
           function Refresh() {
               window.parent.location = window.parent.location.href;
           }
       </script>
   </body>
</html>

Pass to Delete.jsp
<%@page import="javax.lang.model.element.Element"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.Timestamp"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="uts.isd.model.dao.DBManager"%>
<%@page import="uts.isd.model.dao.DBConnector"%>
<%@page import="java.sql.Connection"%>
<%@page import="uts.isd.controller.*"%>
<%@page import="java.util.*"%>
<%@page contentType="text/html" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE html>
   <%try{
       DBConnector connector = new DBConnector(); //Create a connection and initialize DB conn-field
       Connection conn = connector.openConnection(); //Get the protected connection instance from DB superclass to share for the application classes
       DBManager db = new DBManager(conn);
       
       //var LogID = localStorage.getItem("LogID");
       String LogID = "3";
       //String LogID = request.getParameter("LogID");
       java.sql.Statement stmt = conn.createStatement();
       String sql = "DELETE FROM ROOT.\"Loginlogs\" where LogID = ? "  ;
       PreparedStatement ps =  conn.prepareStatement(sql);
       ps.setString(1,LogID);

       ps.executeUpdate();
       
   %>

   <html>
   <head>

   </head>
       <body>  
       Login record  :
       <%
           out.println(LogID);
       %>
       has been deleted , click
       <a href="logs.jsp">Here</a>  go back Logs page
       </body>
       <%
           }
           catch (Exception ex) {
               ex.printStackTrace();
       }
       %>
   </html>



i want to bring the Index.jsp value  to Delete.jsp scriptlet
 
Master Rancher
Posts: 451
6
IntelliJ IDE Spring Fedora
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can pass values using page attributes like session attribute I think.  You are probably better of taking the business logic out of the jsp.  JSPs generally use be used for presenting stuff to the user rather than manipulating the data.  If you use the JSP as just a view then what you are doing will probably be easier.

It looks like you are using request parameters so that should work fine.
 
lok yu
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can i use window.location.replace("Delete.jsp?value="+ value + "&ID="+ ID);

to get two or more values?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!