我的jsp页面分为左右两部分。用户登录后将显示其名称,如果他们点击左侧或右侧,也会显示。
在左侧部分,没有用户交互数据应显示在表格和右侧,方法是从下拉列表中选择一个名称,然后单击按钮,数据将显示在表格中。我希望在关闭浏览器后存储点击次数。
如果我将每个var传递给新的servlet并更新MySQL表,请提供一些指导吗?另一个问题是,当用户点击按钮,因为它将发布到servlet类,他们的名字和左侧部分的计数将被删除。我想我应该改变编程的风格,但我无法弄清楚如何去做。
这就是我所做的:
的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 * from users where uemail='" + user + "' ");
if (rs.next()) {
session.setAttribute("uname", user);
out.println("welcome " + user);
} else {
int i = st.executeUpdate("insert into 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 from 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类:
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 FROM table2 where 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();
}
finally {
try { statement.close(); } catch (SQLException e) { e.getErrorCode(); }
try { connect.close(); } catch (SQLException e) { e.getErrorCode(); }
}
}}
答案 0 :(得分:0)
非常不稳定的环境!
想知道为什么你需要计算点击次数,但如果这是一项必不可少的业务,那么你需要考虑桌面应用程序,使用网络,你不能让它100%稳定和有效。
如果您坚持要解决问题,我建议您使用WebSocket,并每隔x秒将点击次数传递给服务器。
同时尝试使用连接池和预准备语句,您的数据库连接状态现在是最糟糕的。