Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Login script not working, help please

843789Apr 17 2010 — edited Apr 17 2010
Hi, Im having problems with a login script. I have got a login form that sucessfully passes the data to the login process form but the login process form, but then nothing happens after the SQL statement is run apart from a white screen being shown with my two test out statements for the username and password.

The SQL works in a accss sql query but doens't seem to do much in the java servlet.

I would greatly appreciate it if you could take a look at the code, the database is being connected from a db conenction page.
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package servlets;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import dbclasses.DBConnector;


public class Loginprc extends HttpServlet {
    private Connection conn;


   DBConnector db= new DBConnector();
    /** 
     * 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();

        HttpSession session = null;
        ResultSet rsdoLogin = null;
    PreparedStatement psdoLogin=null;
    


    String sUserID=request.getParameter("usersusername");
    String sPassword=request.getParameter("userspassword");
    out.println(sUserID);
    out.println(sPassword);
    String message="User login successfully ";

    try{
          db.createConnection();

   // String sqlOption="SELECT * FROM useraccount WHERE Username = ? and Password = ?";
String sqlOption = "SELECT * FROM useraccount WHERE Username = 'andy' AND Password = 'andy'";


    psdoLogin=conn.prepareStatement(sqlOption);
        //psdoLogin.setString(1,sUserID);
   // psdoLogin.setString(2,sPassword);

    rsdoLogin=psdoLogin.executeQuery();

  if (rsdoLogin.next())
    {
        out.println("Blah");
     /* String sUserName=rsdoLogin.getString("sFirstName")+" "+rsdoLogin.getString("sLastName");

      session.setAttribute("sUserID",rsdoLogin.getString("sUserID"));
      session.setAttribute("iUserType",rsdoLogin.getString("iUserType"));
      session.setAttribute("iUserLevel",rsdoLogin.getString("iUserLevel"));
      session.setAttribute("sUserName",sUserName);
*/
      response.sendRedirect("success.jsp?error="+message);
      
    }
    else
    {
            out.println("Blah1111");
      message="No user or password matched" ;
      response.sendRedirect("login.jsp?error="+message);
    }
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }


    /// close object and connection
    try{
         if(psdoLogin!=null){
             psdoLogin.close();
         }
         if(rsdoLogin!=null){
             rsdoLogin.close();
         }

         if(conn!=null){
          conn.close();
         }
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }

      /*  String usersusername1, userspassword1;
        usersusername1 = request.getParameter("usersusername");
            userspassword1 = request.getParameter("userspassword");
        try {
            
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet Loginprc</title>");  
            out.println("</head>");
            out.println("<body>");
            session = request.getSession(true);

            out.println("<h1>Servlet Loginprc at " + request.getContextPath () + "</h1>");
            out.println("</body>");
            out.println("</html>");

             session.setAttribute("username1",usersusername1);

        } */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 {
        processRequest(request, response);
    } 

    /** 
     * 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 {
        processRequest(request, response);
    }

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

}
Cheers
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 15 2010
Added on Apr 17 2010
1 comment
186 views