Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Display hidden name after login in jsp  RSS feed

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to display the hidden field from the preferred name on the mainpage.jsp pages once I login. I created two user account in a database table named Users in order the user to log in.

Users table:

    Id | PreferredName |     Email                      |  Password
    1  | Michael             | michael13@gmail.com | passone
    2  | Lim                  | lim134@gmail.com    | passtwo


Every time the user submits a form, the form contains the hidden values from the preferred name that provide information about the user. It should like this:

Output:

    Welcome, Michael

When I run my browser, I input the user's email address and password in order to login. Although I successfully logging in but it did not display the name on the mainpage.jsp

My Output:

    Welcome,

Login.jsp

    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <title>Login Page</title>
        </head>
        <body>
            <h1>Login</h1>
            <form method="post" action="login">
                <input type="hidden" name="username" value="${request.username}" required/>
                Email:<input type="email" name="email" required/><br/><br/>
                Password:<input type="password" name="password" required/><br/><br/>
               
                <table>
                    <tr>
                        <td>
                            <input type="submit" value="Login">
                        </td>
                       
                        <td>
                            <a href="index.html">
                                <button>Main Page</button>
                            </a>
                        </td>
                    </tr>
                </table>
               
            </form>
   
        </body>
    </html>

LoginServlet.java

    import java.io.IOException;
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.ArrayList;
    import java.util.List;
    import javax.annotation.Resource;
    import javax.servlet.RequestDispatcher;
    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;
    import javax.sql.DataSource;
   
    @WebServlet("/login")
    public class LoginServlet extends HttpServlet{
      @Resource(name="jdbc/jed")
        private DataSource dsFoodDeliver;
        @Override
        protected void doPost(HttpServletRequest request,
                              HttpServletResponse response)
                              throws ServletException, IOException
        {
            String uname = request.getParameter("username");
            String q = "SELECT * FROM users WHERE preferredName ='" + uname + "'";
           
            if (uname != null) {
            //success!
                //save the user data in session scope
                HttpSession session = request.getSession();
                session.setAttribute("user", uname);
                //do your forward or redirect...
                RequestDispatcher rd = request.getRequestDispatcher("/mainpage.jsp");
                rd.forward(request, response);
            }
           
            String email = request.getParameter("email");
            String pw = request.getParameter("password");
           
            List<RegisterRecord> register= new ArrayList();
            Connection connection = null;
            PreparedStatement preparedStatement = null;      
            ResultSet resultset = null;
   
   
            try
            {
                connection = dsFoodDeliver.getConnection();
               
                connection.setAutoCommit(false);
               
                preparedStatement = connection.prepareStatement("SELECT * FROM users WHERE email = ? and password = ?");
                preparedStatement.setString(1, email);
                preparedStatement.setString(2, pw);
               
                resultset = preparedStatement.executeQuery();          
               
                while(resultset.next()){
                   
                     RegisterRecord record = new RegisterRecord();
                     email = resultset.getString("email");
                     pw = resultset.getString("password");
                    
                     record.setEmail(email);
                     record.setPassword(pw);
                    
                     register.add(record);               
                }
               
               
            }
            catch(SQLException ex)
            {
                request.getSession().setAttribute("message", "An error has occured in the insertion process, Please check with your DB administrator for more details");
                try
                {
                    connection.rollback();
                }
                catch(SQLException ex1)
                {
                    ex1.printStackTrace();
                    System.err.println(ex1.getMessage());
                }
                ex.printStackTrace();
                System.err.println(ex.getMessage());
            }
            finally
            {
                if(resultset != null)
                {
                    try
                    {
                        resultset.close();
                    }
                    catch(SQLException ex)
                    {
                        try
                        {
                            connection.rollback();
                        }
                        catch(SQLException ex1)
                        {
                            ex1.printStackTrace();
                            System.err.println(ex1.getMessage());
                        }
                        ex.printStackTrace();
                        System.err.println(ex.getMessage());
                      
                    }
                   
                }
                if(preparedStatement !=null)
                {
                    try
                    {
                        preparedStatement.close();
                    }
                    catch(SQLException ex)
                    {
                        try
                        {
                            connection.rollback();
                        }
                        catch(SQLException ex1)
                        {
                            ex1.printStackTrace();
                            System.err.println(ex1.getMessage());
                        }
                        ex.printStackTrace();
                        System.err.println(ex.getMessage());
                      
                    }
                }
                if(connection !=null)
                {
                    try
                    {
                        connection.setAutoCommit(false);
                        connection.close();
                    }
                    catch(SQLException ex)
                    {
                        try
                        {
                            connection.rollback();
                        }
                        catch(SQLException ex1)
                        {
                            ex1.printStackTrace();
                            System.err.println(ex1.getMessage());
                        }
                        ex.printStackTrace();
                        System.err.println(ex.getMessage());
                      
                    }
                }
            }
           
            response.sendRedirect(this.getServletContext().getContextPath() + "/mainpage.jsp");
        } 
    }

RegisterRecord.java

    public class RegisterRecord {
       private String preferredname;
       private String email;
       private String password;
   
       public RegisterRecord() {
        }
   
   
        public String getPreferredname() {
            return preferredname;
        }
   
        public void setPreferredname(String preferredname) {
            this.preferredname = preferredname;
        }
   
        public String getEmail() {
            return email;
        }
   
        public void setEmail(String email) {
            this.email = email;
        }
   
   
        public String getPassword() {
            return password;
        }
   
        public void setPassword(String password) {
            this.password = password;
        }
      
      
    }


Mainpage.jsp

    <html>
    <b>Welcome, <%=request.getAttribute("user") %></b>
    <html>
 
author & internet detective
Marshal
Posts: 38504
653
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lui,
In your servlet, you call session.setAttribute() but in your JSP, you call request.getAttribute(). That's like putting a book in the bathroom and wondering why you can't find it on your bed.

I imagine you want it in the servlet scope, so you'd change your JSP to session.getAttribute().

Note that putting scriptlets in JSPs is bad practice so you might want a more current book.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!