0

Here is the Login.jsp:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Login</title>
        <script type="text/javascript">

        </script>
    </head>

    <body bgcolor="#999966">
<p>&nbsp;</p>
<form method="get" action="http://localhost:8080/WebApplication1/Login">
<%
  if (request.getAttribute("error")!=null) {
%>
Login failed. Please try again
<BR><HR>
<%
  }
%>

<p> &nbsp;&nbsp;&nbsp;&nbsp;Username: <input type="text" name="uname" size="20"></p>
<p> &nbsp;&nbsp;&nbsp;&nbsp;Password: <input type="password"  name="pass" size="20"></p>
<p><BR>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  <input type="submit" VALUE="login" >
  <input type="reset" value="reset">
  <input type="hidden" value="" name="url">
 </p>
<p>If logging in for the first time, please register <a href="/WebApplication1/SignUp.jsp">here</a>.
</p>
<%

String username=request.getParameter("uname");
session.putValue("uname",username);
%>



</form>
</body>
</html>

Here is the Login.java

import java.sql.*;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.*;
import javax.servlet.http.*;
//import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
//import javax.servlet.http.HttpServlet;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;


@WebServlet(name="Login", urlPatterns={"/Login"})
public class Login extends HttpServlet {

   
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
    response.setContentType("text/html");
    PrintWriter pw = response.getWriter();
    String name = request.getParameter("uname");
    String password = request.getParameter("pass");


		String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
		Class.forName(driver).newInstance();

		Connection con=null;
		ResultSet rst=null;
		Statement stmt=null;

		String url="jdbc:odbc:c";
		con=DriverManager.getConnection(url);
		stmt=con.createStatement();
		String query = "select uname from Customer where uname='"+name+"' and pass='"+password+"'";
		out.println(query);
		ResultSet rs = stmt.executeQuery(query);

		if(rs.next()){

                     HttpSession session = request.getSession(true);
                     session.setAttribute("user", name);
                     String prev= (String) request.getSession().getValue("previousPage");
                     response.sendRedirect(prev);
                }
		else{
                    request.setAttribute("error", "yes");
                    RequestDispatcher rd = request.getRequestDispatcher("/Login.jsp");
                    rd.forward(request, response);
		    pw.println("Please insert valid Username and Password!");
		}

        }
        catch(Exception e){
		out.println(e);
	}
        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>

}
4
Contributors
3
Replies
5
Views
5 Years
Discussion Span
Last Post by vtm
0

:yawn:What is the name of your database?

and please add comments to the part were you get errors...

Edited by syrellNardo: n/a

0

Also if you use JDBC:ODBC you need to pass username and password to database

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.