java - how to store user clicks on two different parts of jsp page into mysql db -
my jsp page has 2 parts, left , right.after user log in name displayed , if click on left or right shown.
at left part, without user interaction data should displayed in table , @ right side choosing name drop down list , clicking button data displayed in table. want store number of clicks after close browser.
would please provide guidance, should pass each var new servlet , update mysql table? , other problem when user clicks on button since post servlet class name , counts on left part removed. think should change style of programming couldn't figure out how it.
this have done :
index.jsp
<%@ 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=utf-8"> <title>jsp example</title> </head> <body> <form method="post" action="second.jsp"> <center> <table border="1" width="30%" cellpadding="3"> <tbody> <tr> <td>user id</td> <td><input type="text" name="uname" value="" /></td> </tr> </tbody> </table> <input type="submit" value="login" /> </center> </form> </body>
second.jsp:
<%@ 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" %> <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> <%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %> <%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml" %> <%@ page import="java.io.*,java.util.*,java.sql.*"%> <%@ page import="javax.servlet.http.*,javax.servlet.*" %> <!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd"> <html> <%@ page import="java.util.arraylist" %> <head> <meta http-equiv="content-type" content="text/html; charset=iso-8859-1"> <title>title</title> </head> <body> <% string user = request.getparameter("uname"); class.forname("com.mysql.jdbc.driver"); connection con = drivermanager.getconnection("jdbc:mysql://localhost:3306/db", "root", "pass"); statement st = con.createstatement(); resultset rs; rs = st.executequery("select * users uemail='" + user + "' "); if (rs.next()) { session.setattribute("uname", user); out.println("welcome " + user); } else { int = st.executeupdate("insert users (uemail) values ('" + user + "')"); if (i > 0) { session.setattribute("uname", user); out.println("welcome " + user); } } %> <div style="width:100%;" > <div style="float:left; width:60%;" > <sql:setdatasource var="snapshot" driver="com.mysql.jdbc.driver" url="jdbc:mysql://localhost:3306/db" user="root" password="pass"/> <sql:query datasource="${snapshot}" var="resultt"> select a,b,c,d table; </sql:query> <table width="70%" onclick='lcounters()'> <c:foreach var="row" items="${resultt.rows}"> <tr> <td><a href="${row.a}" target="blank"><font size="5">${row.d}</font> </a></td> </tr> <tr> <td ><font color="green" size="4"><c:out value=" ${row.b}"/></font></td> </tr> <tr> <td ><c:out value=" ${row.c}"/></td> </tr> <tr> <td> </td> </tr> </c:foreach> <script> var lclicks = 0; function lcounters() { clicks += 1; document.getelementbyid("lclicks").innerhtml = lclicks; };</script> <p>clicks: <a id="lclicks">0</a></p> </table> </div> <div style="float:right; width:40%; "> <form action="actionservlet" method="post"> <table > <tr> <td> names:</td> <td> <select name="names"> <option value=""> </option> <option value="aaa">aaa</option> <option value="bbb">bbb</option> </select> </td> <td> <input type="submit" name="jdbc_query" value="show" /> </td> </tr> <script> var rclicks = 0; function rcounters() { rclicks += 1; document.getelementbyid("rclicks").innerhtml = rclicks ; };</script> <p>clicks: <a id="rclicks">0</a></p> </table> <c:if test="${result != null}"> <table border="1" width="100%" onclick='rcounter()'> <tr> <th>first</th> <th>second</th> <th>third</th> </tr> <c:foreach var="row" items="${result.rows}"> <tr> <td><c:out value="${row.u}"/></td> <td><c:out value="${row.v}"/></td> <td> <a href="${row.w}"> <font color="green"> ${row.x} </font> </a><br>${row.y} </td> </tr> </c:foreach> </table> </c:if> </div> </form> </div> </body> </html>
servlet class:
import java.io.ioexception; import java.io.printwriter; import javax.naming.namingexception; import javax.servlet.requestdispatcher; import javax.servlet.servletexception; import javax.servlet.http.httpservlet; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; import javax.servlet.http.httpsession; import com.mysql.jdbc.jdbc2.optional.mysqldatasource; import com.sun.xml.internal.bind.v2.schemagen.xmlschema.list; import java.sql.connection; import java.sql.preparedstatement; import java.sql.resultset; import java.sql.resultsetmetadata; import java.sql.sqlexception; import java.sql.statement; import java.util.arraylist; import java.util.hashmap; import javax.servlet.jsp.jstl.sql.result; import javax.servlet.jsp.jstl.sql.resultsupport; @suppresswarnings("serial") public class actionservlet extends httpservlet { @override public void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { // httpsession session = request.getsession(); string out = "\n"; string cname = request.getparameter("names"); mysqldatasource ds = null; connection connect = null; statement statement = null; try { ds = new mysqldatasource(); ds.seturl("jdbc:mysql://localhost:3306/db"); ds.setuser("root"); ds.setpassword("pass"); connect = ds.getconnection(); statement = connect.createstatement(); string query = "select u,v,x,w,y table2 name= ? " ; preparedstatement ps1 = connect.preparestatement(query); ps1.setstring(1, cname); resultset resultset = ps1.executequery(); result result = resultsupport.toresult(resultset); request.setattribute("result", result); requestdispatcher rd = request.getrequestdispatcher("second.jsp"); rd.forward(request, response); } catch (sqlexception e) { e.printstacktrace(); } { try { statement.close(); } catch (sqlexception e) { e.geterrorcode(); } try { connect.close(); } catch (sqlexception e) { e.geterrorcode(); } } }}
very unstable environment!
wonder why need count clicks, if it's business essential, need think desktop applications, web cannot have 100% stable , working.
but if insist fix problem, suggest have websocket, , pass clicks every x seconds server.
also try use connection pools , prepared statements, db connectivity state worst 1 right now.
Comments
Post a Comment