Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6015859
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:53:52+00:00 2026-05-23T02:53:52+00:00

In jsp Login or Session() what is better to put in the session the

  • 0

In jsp Login or Session() what is better to put in the session the Username or the Id ??

and do i test on a servlet or on an outher jsp page ??

im using this code in an outher jsp page but i think is not safe

 <%if(session.getAttribute("username") == null){%>
<%
String err ; 
Connection cnn;
  Class.forName(System.getProperty("database.driver","com.mysql.jdbc.Driver"));
 cnn = DriverManager.getConnection("jdbc:mysql://localhost:3306/sondage","root", "");
  Statement stat1 = cnn.createStatement();

  String Username,Password;

  Username=request.getParameter("UserName");
  Password=request.getParameter("passWord");

  //Le nomre de produits
ResultSet resultat = stat1.executeQuery("select * from utilisateur where username ='"+ Username +"' and password = '"+Password+"'");
if (resultat.next()) {

session.putValue("username",Username);

    RequestDispatcher rd =request.getRequestDispatcher("vote.jsp");
    rd.forward(request, response);

}
else {
    RequestDispatcher rd =request.getRequestDispatcher("authentification.jsp?err=1");
    rd.forward(request, response);
     }
    %>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T02:53:53+00:00Added an answer on May 23, 2026 at 2:53 am

    That’s all up to you. I personally just put a Javabean which represents the User entity in there so that I have instant access to all of its properties and I usually do this in a servlet.

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        User user = userDAO.find(username, password);
    
        if (user != null) {
            request.getSession().setAttribute("user", user);
            response.sendRedirect("userhome");
        } else {
            request.setAttribute("message", "Unknown login, try again");
            request.getRequestDispatcher("/WEB-INF/login.jsp").forward(request, response);
        }
    }
    

    There are only several things which springs out as weird/unsafe:

    <%if(session.getAttribute("username") == null){%>
    

    Why this check? Isn’t the user allowed to re-login as another user? You should rather use a Filter for this.

    ResultSet resultat = stat1.executeQuery("select * from utilisateur where username ='"+ Username +"' and password = '"+Password+"'");
    

    That’s a SQL injection hole. Read on about SQL injection attacks and prepared statements.

    Finally I miss the finally block wherein you properly close the JDBC resources in order to prevent them from leaking which may cause your application to crash on long term whenever the DB is running out of all resources.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

can i use hibernate to login jsp page using (session) if i'm not using
Basically, I have a login.jsp page and a filter that checks if a session
i have one login.jsp web page where i have two textboxes for username and
In the first line of my JSP page, I check for a session value
I have created a login page using a nice guide that I found about
I am trying to login a user using a jsp over https and i
I try to create login form in web application. in JSP page I can
im using hibernate with my jsp page and mySQL ,how can i do that
I'm just wondering, is it possible to exclude a page (say, the login.jsp) from
I am setting and getting the session attributes on login page, when i logout

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.