Share on Google+Share on Google+

Mumbai
loginDAO and Loginservlet
2 Answer(s)      3 years and 10 months ago
Posted in : JDBC

how to make loginDAO and loginServlet?

Ads

View Answers

May 22, 2013 at 9:22 AM


userDAO
===================
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tcs.ignite.app.dao;

import com.tcs.ignite.app.beans.User;
import com.tcs.ignite.app.utils.ConnectionManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 *
 * @author 495944
 */
public class UserDAO {

    public void logUserIn(User user) {
        try {
            Connection con = new ConnectionManager().createConnection();
            String queryString = "select Username,password from register where Email='" + user.getEmail() + "'";
            PreparedStatement ps = con.prepareStatement(queryString);
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
                user.setRegistered(true);
                String password = rs.getString("password");
                if (password.equals(user.getPassword())) {
                    user.setName(rs.getString("Username"));
                }
            } else {
                user.setRegistered(false);
            }
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }

         public boolean forgetpassword(User user){

        try {
            Connection con = new ConnectionManager().createConnection();

            String queryString ="update test.register set password=("+"'"+user.getNewpassword()+"'"+") where Email=("+"'"+user.getEmail()+"'"+")";
            System.out.println(queryString);
            PreparedStatement ps = con.prepareStatement(queryString);
            int rs = ps.executeUpdate();
            if(rs==1){
                return true;
            }else{
                return false;
            } 
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
            return false;
        } catch (SQLException ex) {
            ex.printStackTrace();
            return false;
        }
    }  
}


May 22, 2013 at 9:23 AM


Login servlet
=======================
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tcs.ignite.app.servlets;

import com.tcs.ignite.app.beans.User;
import com.tcs.ignite.app.dao.UserDAO;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author 495944
 */
@WebServlet(name = "Login1", urlPatterns = {"/Login1"})
public class Login1 extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /*
             * TODO output your page here. You may use following sample code.
             */
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet Login</title>");            
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet Login at " + request.getContextPath() + "</h1>");
            out.println("</body>");
            out.println("</html>");
        } finally {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        request.getSession().invalidate();
       response.sendRedirect("home.jsp");
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        User user = new User();
      user.setEmail(request.getParameter("inputEmail").trim());
        user.setPassword(request.getParameter("inputPassword1").trim());
        new UserDAO().logUserIn(user);
        if(user.isRegistered()){
            if(user.getName()!=null){
                request.getSession().setAttribute("user", user);
                request.getRequestDispatcher("home.jsp").forward(request, response);
                }
            else{
                request.setAttribute("msg", "Wrong Password");
                request.getRequestDispatcher("home.jsp").forward(request, response);
//                response.sendRedirect("index.jsp");
            }
        }else{
            request.setAttribute("msg", "User does not exist.");
            request.getRequestDispatcher("home.jsp").forward(request, response);
//            response.sendRedirect("index.jsp");
        }

    }
    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}









Related Tutorials/Questions & Answers:
loginDAO and Loginservlet
loginDAO and Loginservlet  how to make loginDAO and loginServlet?   userDAO /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package com.tcs.ignite.app.dao; import
Loginservlet not being called
Loginservlet not being called  Hi, I have created a simple login page using jsp,on submit it calls the loginservlet,Everything was working fine when I run the jsp in eclipse,on clickin the submit button login servlet was called
Advertisements
Error with LogIn with mysql database
'loginDAO': Injection of resource dependencies failed; nested exception... com.pizza.model.Users; public interface LoginDAO{ public boolean checkLogin(String... org.hibernate.Query; import java.util.List; @Repository("loginDAO") public class
Java client posting to a servlet - Development process
javax.servlet.http.*; public class LoginServlet extends HttpServlet{ public void...(""); } } web.xml file for this program: Hello LoginServlet Hello /LoginServlet For more information on servlets visit to : http
Spring 4: Login Form using Spring MVC and Hibernate Example
net.roseindia.model.*; public interface LoginDAO{ public boolean checkLogin... org.hibernate.Query; import java.util.List; @Repository("loginDAO") public class LoginDAOImpl implements LoginDAO{ @Resource(name="sessionFactory
http status 500 error in tomcat 6 while using eclipse helios
*; import java.io.*; public class LoginServlet extends HttpServlet{ public void init... org.apache.catalina.core.StandardWrapperValve invoke SEVERE: Servlet.service() for servlet LoginServlet threw
http status 500 error in tomcat 6 while using eclipse helios
*; import java.io.*; public class LoginServlet extends HttpServlet{ public void init... org.apache.catalina.core.StandardWrapperValve invoke SEVERE: Servlet.service() for servlet LoginServlet threw
http status 500 error in tomcat 6 while using eclipse helios
*; import java.io.*; public class LoginServlet extends HttpServlet{ public void init... org.apache.catalina.core.StandardWrapperValve invoke SEVERE: Servlet.service() for servlet LoginServlet threw
http status 500 error in tomcat 6 while using eclipse helios
*; import java.io.*; public class LoginServlet extends HttpServlet{ public void init... org.apache.catalina.core.StandardWrapperValve invoke SEVERE: Servlet.service() for servlet LoginServlet threw
http status 500 error in tomcat 6 while using eclipse helios
*; import java.io.*; public class LoginServlet extends HttpServlet{ public void init... org.apache.catalina.core.StandardWrapperValve invoke SEVERE: Servlet.service() for servlet LoginServlet threw
File size limit - JSP-Servlet
LoginServlet .auth.LoginServlet UploadServlet.../* LoginServlet /login
Need Help-How to store input parameter in DB through Java Bean - JSP-Servlet
javax.servlet.*; import javax.servlet.http.*; public class LoginServlet extends...: Password: web.xml: one LoginServlet one /tmp
Using Servlets, JSP for Online Shopping
? JSP <form action="LoginServlet "method="post" > <table>..., response); } } }   Login.jsp: <form action="LoginServlet... javax.servlet.http.*; public class LoginServlet extends HttpServlet{ public void doPost
login example
Page</title> </head> <body> <form action="LoginServlet">... javax.servlet.http.HttpSession; /** * Servlet implementation class LoginServlet */ public class LoginServlet extends HttpServlet { public void doGet(HttpServletRequest
login example
Page</title> </head> <body> <form action="LoginServlet">... javax.servlet.http.HttpSession; /** * Servlet implementation class LoginServlet */ public class LoginServlet extends HttpServlet { public void doGet(HttpServletRequest
How does this example work even if no oracle to be used and only access is available?
; /** * Servlet implementation class LoginServlet */ public class LoginServlet extends
How Map the First_Servlet to Second_Servlet - JSP-Servlet
class LoginServlet extends HttpServlet{ public void doGet(HttpServletRequest... Page web.xml: one LoginServlet one /tmp submit.jsp
Also HTTP Status 404 - /Login/userdetail.java
LoginServlet extends HttpServlet{ public void doPost(HttpServletRequest
unable to retrive the data from mysql using servlet using jdbc driver
unable to retrive the data from mysql using servlet using jdbc driver  Hi, i have a servlet program..to verify the login and redirect to correct html page on verification. public class LoginServlet extends HttpServlet implements
Servlets - JSP-Servlet
javax.servlet.http.*; import java.io.*; import java.sql.*; public class loginservlet
Struts - Struts
javax.servlet.http.*; import java.io.*; import java.sql.*; public class loginservlet
Struts Login Validation. In This code setter of login page is called only one time again it doesnt call it. Why..?
implementationStatus; private String subSystem; private LoginDAOImpl loginDAO = new
Servlet-JSP Mapping - JSP-Servlet
class LoginServlet extends HttpServlet{ public void doGet(HttpServletRequest... Page web.xml: one LoginServlet one /tmp submit.jsp issue.jsp
Servlet-JSP Mapping - JSP-Servlet
class LoginServlet extends HttpServlet{ public void doGet(HttpServletRequest... Page web.xml: one LoginServlet one /tmp submit.jsp issue.jsp
javax. servlet.Servlet Exception: Initialization failed, Unable to get DB connection - JSP-Servlet
*; /** * @author Santosh */ public class LoginServlet extends GenericServlet
java server programming : servlet - JSP-Servlet
(""); out.println("Servlet LoginServlet"); out.println
HTTP Status 404 - /Login/userdetail.java
java.sql.*; public class LoginServlet extends HttpServlet{ public void
Servlets
.*; public class LoginServlet extends HttpServlet { public void Service
java multiple users with single connection - JSP-Servlet
*; public class LoginServlet extends HttpServlet{ public void doPost
Passing Parameter Using Html Form
;/htmlform/LoginServlet">   <p> Username  <
Tutorials   
Java Spring Hibernate Struts Training how to retrieve text and images from mysql database and show on html page using jsp servlet How to install Neo4j on Ubuntu 15.10? please help me in these prog MYSQL - mysql copy table to another table example by creating new table solution for mapping hibernate and pojos from net beans through database Connect GWT application with Oracle inventory System of Medical Store inventory System of Medical Store inventory System of Medical Store inventory System of Medical Store This code have created jTable that connected with a database.every cell of last column have a button.But this button doesn't work properly.Where is the error java program Java Database Program jdbc Database connection class in a live project ..?? What is JDBC architecture? Unable to Create DSN for excel to ms access importing data and edit that data Database Scrolling Buttons how to use foreign key in java How to delete content of a log file from linux terminal? JSP-Oracle connectivity Student Management System Java code for Saving Marathi (Indian local language ) in Mysql and retrieving the same crud operations with foreign key constraint. I really need help with this assignment question Please help me out Please Backup Failed Mysqldump Got Errno 28 On Write Upgrade MySQL 5.1 to 5.5 in cpanel mysqldump all databases com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'jpa.sequence' doesn't exist persistence xml class element persistence.xml for MySQL MySQL JDBC Driver pom dependency code Blob column jdk6 and oracle 8i connexion oracle 8i et jdk6 java [ cannot retrive date from sql ] why?? Order Entry System How to threow timeout exception when DB is not responding How to threow timeout exception when DB is not responding This is the employee form to add data to mysql database but I got error of NullPointerException, Is this write code please give me feedback as soon as possible make a method which takes user name & pwd as parameter & validate them against the database. If the corresponding user exist return true otherwise false. prabhakar validating credentials and displaying error message in login .jsp if not valid JDBC jdbc jdbc code register servlet vibhu connectiondetails from vibhu

Ads

 
Advertisement null

Ads