Hello, there is a servlet SignInServlet.java

@WebServlet(name = "SignInServlet", urlPatterns = {"/SignInServlet"}) public class SignInServlet extends HttpServlet { User user; public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { user = new User(); String login = req.getParameter("login"); String password = req.getParameter("password"); String role; resp.setContentType("text/html;charset=utf-8"); PrintWriter pw = resp.getWriter(); Connection conn = null; String url = "jdbc:postgresql://127.0.0.1:5432/crm"; String name = "vladimir"; String pass = "1234"; try { Class.forName("org.postgresql.Driver"); conn = DriverManager.getConnection(url, name, pass); Statement statement = conn.createStatement(); ResultSet result1 = statement.executeQuery("SELECT * FROM users"); while (result1.next()) { if((password.equals(result1.getString("password"))) && (login.equals(result1.getString("login")))) { user.setID(result1.getInt("iduser")); user.setLogin(result1.getString("login")); user.setUserrole(result1.getString("userrole")); req.setAttribute("login", login); req.getRequestDispatcher("/signiinsucces.jsp"). forward(req, resp); } /*else if((!password.equals(result1.getString("password"))) && (!login.equals(result1.getString("login")))) { getServletContext(). getRequestDispatcher("/signiinfail.jsp"). forward(req, resp); }*/ } } catch (Exception ex) { ex.printStackTrace(); getServletContext(). getRequestDispatcher("/error.jsp"). forward(req, resp); } finally { if (conn != null) { try { conn.close(); } catch (SQLException ex) { getServletContext(). getRequestDispatcher("/error.jsp"). forward(req, resp); } } } } } 

Have jsp to enter the site

 <%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Вход</title> </head> <body> <form action="SignInServlet"> <p> Ваш логин<br> <input type="text" size="40" max="30" min="1" name="login"> </p> <p> Ваш пароль<br> <input type="password" size="40" max="25" min="1" name="password"> </p> <p> <input type="submit" value="Войти"> </p> </form> <br><br> <p> <a href="index.jsp">На главную страницу</a> </p> </body> 

After user gets on site.jsp

 <%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Начальная страница создания сайта</title> </head> <body> <form action="SiteServlet"> <p> Название сайта<br> <input type="text" size="40" max="30" min="1" name="sitename"> </p> <p> <input type="submit" value="Отправить"> </p> </form> <br><br> <p> <a href="signiinsucces.jsp">Назад</a> </p> </body> 

which handles servlet

 @WebServlet(name = "SiteServlet", urlPatterns = {"/SiteServlet"}) public class SiteServlet extends HttpServlet { User user; public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { try { user = new User(); String sitename = req.getParameter("sitename"); int id = user.getID(); String login = user.getLogin(); resp.setContentType("text/html;charset=utf-8"); PrintWriter pw = resp.getWriter(); Connection conn = null; String url = "jdbc:postgresql://127.0.0.1:5432/crm"; String name = "vladimir"; String pass = "1234"; try { Class.forName("org.postgresql.Driver"); conn = DriverManager.getConnection(url, name, pass); Statement statement = conn.createStatement(); //JOptionPane.showMessageDialog(null,conn.toString()); if(id != 0) { String insert = "insert into site values(" + id + ", '" + login + "', '" + sitename + "')"; statement.executeUpdate(insert); req.getRequestDispatcher("/template.jsp").forward(req, resp); } else { req.getRequestDispatcher("/error2.jsp").forward(req, resp); } } catch (Exception ex) { ex.printStackTrace(); getServletContext(). getRequestDispatcher("/error.jsp"). forward(req, resp); } finally { if (conn != null) { try { conn.close(); } catch (SQLException ex) { getServletContext(). getRequestDispatcher("/error.jsp"). forward(req, resp); } } } } catch(Exception e) { e.printStackTrace(); } } } 

How can I get his username and id after logging in?

    1 answer 1

    As an option:

     httpSession.setAttribute("id", id); httpSession.setAttribute("login", login); 

    You will receive attributes accordingly:

     Long id = (Long) httpSession.getAttribute("id"); String login = (String) httpSession.getAttribute(login"); 

    More details can be read here .

    • Thanks, I will try - Zhuk
    • Added HttpSession session = req.getSession (true); session.setAttribute ("iduser", iduser); session.setAttribute ("loginuser", loginuser); This is the code in SignInServlet.java, as well as the code in SiteServlet.java. HttpSession httpSession = req.getSession (true); int iduser = (int) httpSession.getAttribute ("iduser"); String loginuser = (String) httpSession.getAttribute ("loginuser"); As a result, inserting data into the table does not work - Zhuk
    • @Zhuk, not in the end, but now. The operation of setting and receiving the session attribute is not related to the insertion of data into the table. You are not connecting to the database correctly. It is necessary to connect not through DriverManager , but through DataSource . - not a Programmer