0

I'm trying to create a login page for a site (I'm using a tutorial right now - never done this before). When I go to use the "login" page, it pops a 404 error. Here is the code.

LoginPage.jsp

<%@ page language="java" 
    contentType="text/html; charset=windows-1256"
    pageEncoding="windows-1256"
%>

<!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=windows-1256">
        <title>Login Page</title>
    </head>

    <body>
        <form action="LoginServlet">

            Please enter your username      
            <input type="text" name="un"/><br>      

            Please enter your password
            <input type="text" name="pw"/>

            <input type="submit" value="submit">            

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

LoginServlet.java

package ExamplePackage;


import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class LoginServlet
 */
public class LoginServlet extends HttpServlet {


public void doGet(HttpServletRequest request, HttpServletResponse response) 
                       throws ServletException, java.io.IOException {

try
{       

     UserBean user = new UserBean();
     user.setUserName(request.getParameter("un"));
     user.setPassword(request.getParameter("pw"));

     user = UserDAO.login(user);

     if (user.isValid())
     {

          HttpSession session = request.getSession(true);       
          session.setAttribute("currentSessionUser",user); 
          response.sendRedirect("userLogged.jsp"); //logged-in page             
     }

     else 
          response.sendRedirect("invalidLogin.jsp"); //error page 
} 


catch (Throwable theException)      
{
     System.out.println(theException); 
}
       }
    }

UserBean.Java

package ExamplePackage;

public class UserBean {

      private String username;
      private String password;
      private String firstName;
      private String lastName;
      public boolean valid;


      public String getFirstName() {
         return firstName;
    }

      public void setFirstName(String newFirstName) {
         firstName = newFirstName;
    }


      public String getLastName() {
         return lastName;
            }

      public void setLastName(String newLastName) {
         lastName = newLastName;
            }


      public String getPassword() {
         return password;
    }

      public void setPassword(String newPassword) {
         password = newPassword;
    }


      public String getUsername() {
         return username;
            }

      public void setUserName(String newUsername) {
         username = newUsername;
            }


      public boolean isValid() {
         return valid;
    }

      public void setValid(boolean newValid) {
         valid = newValid;
    }   
}

UserDAO.java

package ExamplePackage;

   import java.text.*;
   import java.util.*;
   import java.sql.*;

   public class UserDAO     
   {
      static Connection currentCon = null;
      static ResultSet rs = null;  



      public static UserBean login(UserBean bean) {

         //preparing some objects for connection 
         Statement stmt = null;    

         String username = bean.getUsername();    
         String password = bean.getPassword();   

         String searchQuery =
               "select * from users where username='"
                        + username
                        + "' AND password='"
                        + password
                        + "'";

      // "System.out.println" prints in the console; Normally used to trace the process
      System.out.println("Your user name is " + username);          
      System.out.println("Your password is " + password);
      System.out.println("Query: "+searchQuery);

      try 
      {
         //connect to DB 
         currentCon = ConnectionManager.getConnection();
         stmt=currentCon.createStatement();
         rs = stmt.executeQuery(searchQuery);           
         boolean more = rs.next();

         // if user does not exist set the isValid variable to false
         if (!more) 
         {
            System.out.println("Sorry, you are not a registered user! Please sign up first");
            bean.setValid(false);
         } 

         //if user exists set the isValid variable to true
         else if (more) 
         {
            String firstName = rs.getString("FirstName");
            String lastName = rs.getString("LastName");

            System.out.println("Welcome " + firstName);
            bean.setFirstName(firstName);
            bean.setLastName(lastName);
            bean.setValid(true);
         }
      } 

      catch (Exception ex) 
      {
         System.out.println("Log In failed: An Exception has occurred! " + ex);
      } 

      //some exception handling
      finally 
      {
         if (rs != null)    {
            try {
               rs.close();
            } catch (Exception e) {}
               rs = null;
            }

         if (stmt != null) {
            try {
               stmt.close();
            } catch (Exception e) {}
               stmt = null;
            }

         if (currentCon != null) {
            try {
               currentCon.close();
            } catch (Exception e) {
            }

            currentCon = null;
         }
      }

return bean;

      } 
   }

ConnectionManager.java

package ExamplePackage;

import java.sql.*;
import java.util.*;


   public class ConnectionManager {

      static Connection con;
      static String url;

      public static Connection getConnection()
      {

         try
         {
            String url = "jdbc:odbc:" + "logindb"; 
            // assuming "DataSource" is your DataSource name

            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

            try
            {               
               con = DriverManager.getConnection(url,"root","root"); 

            // assuming your SQL Server's   username is "username"               
            // and password is "password"

            }

            catch (SQLException ex)
            {
               ex.printStackTrace();
            }
         }

         catch(ClassNotFoundException e)
         {
            System.out.println(e);
         }

      return con;
}
   }

Error:

HTTP Status [404] – [Not Found]

Type Status Report

Message /LoginExample/LoginServlet

Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.

Apache Tomcat/9.0.0.M21

VNT
  • 934
  • 2
  • 8
  • 19
dragos_kai
  • 163
  • 4
  • 18
  • Go and check the servlets hello world program in the web and try to practice on it and come back to this application...It is a good way for beginners... – VNT Jun 02 '17 at 15:24

2 Answers2

1

In the form in JSP, do not specify the java file name. Try using the URL-pattern of the servlet.

Sai Kishore
  • 326
  • 1
  • 7
  • 16
  • Meaning what? I'm sorry, complete newbie at programming. Got very spoiled by things like WordPress. – dragos_kai Jun 02 '17 at 14:45
  • When you create a servlet, a url-pattern will be specified, which is nothing but a name to the servlet. whis is then attached to the url for e.g. action="localhost/web-app/testservlet" where testservlet is the url-pattern. I use NetBeans. – Sai Kishore Jun 02 '17 at 15:14
0

in your jsp page, the form is mapped on the url-pattern "LoginServlet" servlet:

<form action="LoginServlet">

The error is because in your web.xml file in WEB-INF, the LoginServlet is mapped onto something else. If you want it to work, you need to specify this in the web.xml:

  <servlet>
    <description>LoginServlet</description>
    <display-name>LoginServlet</display-name>
    <servlet-name>LoginServlet</servlet-name>
    <servlet-class>yourpackagename.LoginServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>LoginServlet</servlet-name>
    <url-pattern>/LoginServlet</url-pattern>
  </servlet-mapping>
Jonathan Laliberte
  • 2,672
  • 4
  • 19
  • 44