Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Login / Logout Session | why it isn't working?

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Posts
    11
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Question Login / Logout Session | why it isn't working?

    Why my login session is not working? it should run out.println("Welcome "+ session.getAttribute("username") +" <a href=\"logout.jsp\" >Logout</a>"); when i logged in

    Login.jsp
            <%
     
                if(session.getAttribute("username")!=null)
                    {
                     out.println("Welcome  "+ session.getAttribute("username") +"  <a href=\"logout.jsp\" >Logout</a>");
                    }
                else {
              %>
            <a href="#login-box" class="login-window">Login</a>
               <div id="login-box" class="login-popup">
                <a href="#" class="close"><img src="images/close_pop.png" class="btn_close" title="Close Window" alt="Close" /></a>
                  <form method="post" class="signin" action="doLogin.jsp">
                        <fieldset class="textbox">
                       <label class="username">
                        <span>Username or email</span>
                        <input id="username" name="username" value="" type="text" autocomplete="on" placeholder="Username">
                        </label>
                        <label class="password">
                        <span>Password</span>
                        <input id="password" name="password" value="" type="password" placeholder="Password">
                        </label>
                        <button class="submit button" type="submit">Sign in</button>
                        <!-- <input  class="button" name="login" type="submit" value="Sign In"> -->
                        <p>
                        <a class="forgot" href="#">Forgot your password?</a>
                        </p>       
                        </fieldset>
                  </form>

    doLogin.jsp
        <%@ page import="java.sql.*,java.util.*,java.text.*,java.text.SimpleDateFormat" %>
        <%
     
            String username = request.getParameter("username");
            String password = request.getParameter("password");
     
            Connection conn = null;
     
            try {
     
                conn = DriverManager.getConnection("jdbc:mysql://localhost/database", "root", "");
                Statement st = conn.createStatement();
                //System.out.println("User name [" + username + "] Password [" + password +"]");
                ResultSet rs = st.executeQuery("select nama from user where nama = '" + username + "' AND password = '" + password + "'");
                if (rs.next())
                 {
                 response.sendRedirect("login.jsp");
                 }
              else {
                  out.println("Login Failed,Please try Again");
        %>
     
        <%
     
                    //response.sendRedirect("login.jsp");
                    conn.close();
                }
     
            } catch (Exception e) {
                e.printStackTrace();
            }
        %>


  2. #2
    Junior Member
    Join Date
    May 2012
    Location
    Bali, ID
    Posts
    6
    My Mood
    Happy
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default Re: Login / Logout Session | why it isn't working?

    From your code I can see that there is no session attribute named "username" was placed in the session scope. So the the session.getAttribute("username") will give you null that's why it doesn't work.

  3. The Following User Says Thank You to wsaryada For This Useful Post:

    sadaharu (June 20th, 2012)

  4. #3
    Junior Member
    Join Date
    Jun 2012
    Posts
    11
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Login / Logout Session | why it isn't working?

    i just try adding session in my doLogin.jsp
    it will be like this

    Login.jsp
        <%
     
            if(session.getAttribute("sesusername")!=null)
                {
                 out.println("Welcome  "+ session.getAttribute("sesusername") +"  <a href=\"logout.jsp\" >Logout</a>");
                }
            else {
    		%>

    doLogin.jsp
    <%@ page import="java.sql.*,java.util.*,java.text.*,java.text.SimpleDateFormat" %>
    <%
     
        String username = request.getParameter("username");
        String password = request.getParameter("password");
     
     
     
        Connection conn = null;
     
        try {
     
            conn = DriverManager.getConnection("jdbc:mysql://localhost/jspproblog", "root", "");
            Statement st = conn.createStatement();
            System.out.println("User name [" + username + "] Password [" + password +"]");
            ResultSet rs = st.executeQuery("select nama from user where nama = '" + username + "' AND password = '" + password + "'");
            if (rs.next()) 
    			{
    			session.setAttribute("sesusername", rs.getString("username"));
    			session.setAttribute("sespassword", rs.getString("password"));
    			response.sendRedirect("index.jsp");
    			} 
    		else {
        		out.println("Login Failed,Please try Again");
    %>
     
    <%
     
                //response.sendRedirect("login.jsp");
                conn.close();
            }
     
        } catch (Exception e) {
            e.printStackTrace();
        }
    %>

    it give me blank page and won't redirect right now it just stuck in doLogin.jsp
    can u help me to show me how good way to use session?
    Last edited by sadaharu; June 20th, 2012 at 05:05 AM.

  5. #4
    Junior Member
    Join Date
    May 2012
    Location
    Bali, ID
    Posts
    6
    My Mood
    Happy
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default Re: Login / Logout Session | why it isn't working?

    There must be an exception thrown in you program. In your query you select a column named "nama", but you tried to read "username" and "password" from the result set. This throws an SQLException because the column name is not found.

  6. The Following User Says Thank You to wsaryada For This Useful Post:

    sadaharu (June 20th, 2012)

  7. #5
    Junior Member
    Join Date
    Jun 2012
    Posts
    11
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Login / Logout Session | why it isn't working?

    it's working man! thanks!
    wait a minute i found something wrong, after i try logged in, it still stuck in doLogin.jsp with blank page, that won't directing to index.jsp
    this line

            if (rs.next()) 
    			{
    			session.setAttribute("sesusername", rs.getString("username"));
    			session.setAttribute("sespassword", rs.getString("password"));
    			response.sendRedirect("index.jsp");
    			} 
    		else {
        		out.println("Login Failed,Please try Again");

    it should redirect user after logged in into index.jsp, but it stuck
    then i try push back button in my browser, i already logged in
    what happend now with that send response.sendRedirect("index.jsp");
    i already clear the cache, is still the same

  8. #6
    Junior Member
    Join Date
    Jun 2012
    Posts
    11
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Login / Logout Session | why it isn't working?

    bump
    dunno why my response.sendRedirect("index.jsp");
    won't working

Similar Threads

  1. session in jsp
    By casperl90 in forum JavaServer Pages: JSP & JSTL
    Replies: 3
    Last Post: November 2nd, 2011, 12:53 PM
  2. Passing session ID over HttpURLConnection
    By zuzacat in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 20th, 2011, 10:18 AM
  3. Java session problem
    By Padmaja in forum JavaServer Pages: JSP & JSTL
    Replies: 2
    Last Post: August 5th, 2009, 10:06 PM
  4. Session Timeout in LDAP
    By retail_deepa in forum Java Servlet
    Replies: 0
    Last Post: August 4th, 2009, 04:26 AM
  5. How can i put session in Javascript?
    By rajani in forum JavaServer Pages: JSP & JSTL
    Replies: 2
    Last Post: July 8th, 2009, 01:46 PM