我想在jsp文本框中显示登录用户的名字,姓氏,用户名和密码,但它会显示每个用户的详细信息:
目前我在MySql数据库中只有两个用户。 但是,例如,如果我以gordon_k身份登录,如何仅显示他的数据?
我有这样的servlet(LoginServlet.java):
package servlets;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public LoginServlet() {
super();
// TODO Auto-generated constructor stub
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
PrintWriter out = response.getWriter();
String username = request.getParameter("username");
String password = request.getParameter("password");
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/e-book", "root", "root");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select username,password from users where username='" + username
+ "' and password='" + password + "'");
if (rs.next()) {
response.sendRedirect("mainMenu.jsp");
HttpSession session = request.getSession();
session.setAttribute("username", username);
} else {
out.println("Wrong id and/or password");
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
和jsp这样的文件:
changePersonalData.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.Connection"%>
<%
String id = request.getParameter("id");
String driver = "com.mysql.jdbc.Driver";
String connectionUrl = "jdbc:mysql://localhost:3306/";
String database = "e-book";
String userid = "root";
String password = "root";
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
%>
<!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>Change</title>
</head>
<body>
<form method="post" action="ChangePersonalDataServlet">
<table align="left">
<%
try {
connection = DriverManager.getConnection(connectionUrl + database, userid, password);
statement = connection.createStatement();
String sql = "select * from users";
resultSet = statement.executeQuery(sql);
while (resultSet.next()) {
%>
<tr>
<td>First name</td>
<td><input type="text" name="firstname"
value="<%=resultSet.getString("first_name")%>"></td>
</tr>
<tr>
<td>Last name</td>
<td><input type="text" name="lastname"
value="<%=resultSet.getString("last_name")%>"></td>
</tr>
<tr>
<td>User name</td>
<td><input type="text" name="username"
value="<%=resultSet.getString("username")%>"></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="password"
value="<%=resultSet.getString("password")%>"></td>
</tr>
<%
}
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
%>
<tr>
<td></td>
<td><input type="submit" value="Change"></td>
</tr>
</table>
</form>
</body>
</html>