Stack Exchange
How to show data of current loged in user?
I want to show firstname, lastname, username and password of logged in user in jsp  textbox, but it show details of every user like this:

For now I have only two users in MySql database.
But, for example if I log in as a gordon_k, how to show only his data?
I have servlet (LoginServlet.java) like this:
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();
        }
    }
}

And jsp file like this:
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>


you have asked this before,why do you ask again?

@lucumt 'Cuz no one gave me the answer, maybe?
Also I deleted previous post.

Change in changePersonalData.jsp like following to get username from session and pass this username in your sql query to get selected user data:
<%@ 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 username = (String) request.getSession().getAttribute("username");
                    String sql = "select * from users where username='"+username+"'";
                    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>

