skip to Main Content

This is my first post. I hope someone can help me =)
Usually I’ll try to find solutions to this kind of problems on my own.
But this issue is causing me headache …

With the action tag in the html code below I want to start the java servlet.
Sadly I always get the HTTP 404 – Not found error.
I’ve already tried to write /Login, ../Login, Login (upper and lower case) and so on.

Here is my directory structure:

.
├── ps_18_gruppe09
│   └── src
│       ├── main
│       │   └── com.food42.servlet
│       │       └── LoginServelet.java
│       WebContent  
│       ├── html
│       │   └── login.jsp
│       └── WEB-INF
│           └── lib
│               └── web.xml

I’ve already tried the solutions answered on similar kind of posts but they sadly didn’t help.

In my Java Build Path I’ve the following Libraries installed:
Apache Tomcat v8.5
JRE System Library[jre1.8.0_201a] Web App Libraries
MySQL JDBC Driver
EAR Libraries
JUnit5
log4j-1.2.17.jar

Source Folder is ps_18_gruppe09/src/main

Full error message:

HTTP Status 404 – Not Found

Type Status Report

Message /ps_18_gruppe09/Login

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/8.5.35

Thanks in advance for your help =)

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>ServletDBLog4j</display-name>
  <welcome-file-list>
    <welcome-file>login.jsp</welcome-file>
  </welcome-file-list>
   <context-param>
    <param-name>dbUser</param-name>
    <param-value>admin</param-value>
  </context-param>
  <context-param>
    <param-name>dbPassword</param-name>
    <param-value>Food42</param-value>
  </context-param>
  <context-param>
    <param-name>dbURL</param-name>
    <param-value>jdbc:mysql://localhost/</param-value>
  </context-param>
  <context-param>
    <param-name>log4j-config</param-name>
    <param-value>WEB-INF/lib/log4j.xml</param-value>
  </context-param>

  <error-page>
    <error-code>404</error-code>
    <location>/AppErrorHandler</location>
  </error-page>
  <error-page>
    <exception-type>java.lang.Throwable</exception-type>
    <location>/AppErrorHandler</location>
  </error-page>

  <filter>
    <filter-name>AuthenticationFilter</filter-name>
    <filter-class>com.food42.servlet.AuthenticationFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>AuthenticationFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

</web-app>
<%@ page contentType="text/html; charset=UTF-8" language="java" %>

<!DOCTYPE html>
<html lang="en">

<head>

<meta charset="utf-8">
<meta name="viewport"
    content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="">
<meta name="author" content="">

<title>Food42 Login Page</title>

<!-- Bootstrap core CSS -->
<link href="../vendor/bootstrap/css/bootstrap.css" rel="stylesheet">

<!-- Custom fonts for this template -->
<link href="../vendor/fontawesome-free/css/all.min.css" rel="stylesheet"
    type="text/css">
<link href="https://fonts.googleapis.com/css?family=Montserrat:400,700"
    rel="stylesheet" type="text/css">
<link
    href="https://fonts.googleapis.com/css?family=Lato:400,700,400italic,700italic"
    rel="stylesheet" type="text/css">

<!-- Plugin CSS -->
<link href="../vendor/magnific-popup/magnific-popup.css"
    rel="stylesheet" type="text/css">

<!-- Custom styles for this template -->
<link href="../css/freelancer.css" rel="stylesheet">

</head>

<body id="page-top">
    <!-- Navigation -->
    <nav
        class="navbar navbar-expand-lg bg-secondary fixed-top text-uppercase"
        id="mainNav">
        <div class="container">
            <a class="navbar-brand js-scroll-trigger" href="startseite.html">Food42</a>
            <button
                class="navbar-toggler navbar-toggler-right text-uppercase bg-primary text-white rounded"
                type="button" data-toggle="collapse" data-target="#navbarResponsive"
                aria-controls="navbarResponsive" aria-expanded="false"
                aria-label="Toggle navigation">
                Menu <i class="fas fa-bars"></i>
            </button>
            <div class="topnav">
                <input type="text" placeholder="Suchen...">
            </div>
            <form action="suchresultate.html">
                <input type="submit" value="Suchen" class="searchButton" />
            </form>
            <div class="collapse navbar-collapse" id="navbarResponsive">
                <ul class="navbar-nav ml-auto">
                    <li class="nav-item mx-0 mx-lg-1"><a
                        class="nav-link py-3 px-0 px-lg-3 rounded js-scroll-trigger"
                        href="register.html">Registrieren</a></li>
                </ul>
            </div>
        </div>
    </nav>

    <!-- Header -->
    <header class="masthead bg-primary text-white text-center"> </header>

    <!-- Portfolio Grid Section -->
    <section class="portfolio" id="portfolio">
        <div class="container">
            <h2 class="text-center text-uppercase text-secondary mb-0">Benutzer-Login</h2>
            <hr class="star-dark mb-5">
            <form action="${pageContext.request.contextPath}/Login" method="post">
                <h3 class="text-center">Bitte geben Sie Ihre Login-Daten ein:</h3>
                <fieldset class="container" style="width: 400px">
                    <table style="width: 400px">
                        <tr>
                            <th><label class="mt-2 mb-2" for="username">Username:</label></th>
                            <td><input type="text" id="username" name="username"
                                required autofocus /></td>
                        </tr>
                        <tr>
                            <th><label class="mt-2 mb-2" for="password">Password:</label></th>
                            <td><input type="password" id="password" name="password"
                                required /></td>
                        </tr>
                        <tr>
                            <th><label class="mt-2 mb-2" for="pwdconfirm">Best&auml;tigen:</label></th>
                            <td><input type="password" id="pwdconfirm" name="pwdconfirm"
                                required /></td>
                        </tr>
                        <tr>
                            <th><label class="mt-4 mb-3 " for="submit"></label> <input
                                type="submit" id="submit" name="submit" value="Login" /></th>
                        </tr>
                    </table>
                </fieldset>
            </form>
        </div>
    </section>

    <!-- Footer -->
    <footer class="footer text-center">
        <div class="container">
            <div class="row">
                <div class="col-md-4 mb-5 mb-lg-0">
                    <h4 class="text-uppercase mb-4">Location</h4>
                    <p class="lead mb-0">
                        ZHAW Winterthur <br>Technikum
                    </p>
                </div>
                <div class="col-md-4 mb-5 mb-lg-0">
                    <h4 class="text-uppercase mb-4">Around the Web</h4>
                    <ul class="list-inline mb-0">
                        <li class="list-inline-item"><a
                            class="btn btn-outline-light btn-social text-center rounded-circle"
                            href="https://www.facebook.com/Food42ch-954018978130563/?modal=admin_todo_tour">
                                <i class="fab fa-fw fa-facebook-f"></i>
                        </a></li>
                        <li class="list-inline-item"><a
                            class="btn btn-outline-light btn-social text-center rounded-circle"
                            href="https://twitter.com/Food42C?edit=true"> <i
                                class="fab fa-fw fa-twitter"></i>
                        </a></li>
                        <li class="list-inline-item"><a
                            class="btn btn-outline-light btn-social text-center rounded-circle"
                            href="https://www.linkedin.com/company/food42-ch/"> <i
                                class="fab fa-fw fa-linkedin-in"></i>
                        </a></li>
                    </ul>
                </div>
                <div class="col-md-4">
                    <h4 class="text-uppercase mb-4">Food42</h4>
                    <p class="lead mb-0">Share your food
                </div>
            </div>
        </div>
    </footer>

    <div class="copyright py-4 text-center text-white">
        <div class="container">
            <small>Copyright &copy; Food42 2018</small>
        </div>
    </div>

    <!-- Scroll to Top Button (Only visible on small and extra-small screen sizes) -->
    <div class="scroll-to-top d-lg-none position-fixed ">
        <a class="js-scroll-trigger d-block text-center text-white rounded"
            href="#page-top"> <i class="fa fa-chevron-up"></i>
        </a>
    </div>

    <!-- Bootstrap core JavaScript -->
    <script src="../vendor/jquery/jquery.min.js"></script>
    <script src="../vendor/bootstrap/js/bootstrap.bundle.min.js"></script>

    <!-- Plugin JavaScript -->
    <script src="../vendor/jquery-easing/jquery.easing.min.js"></script>
    <script src="../vendor/magnific-popup/jquery.magnific-popup.min.js"></script>

    <!-- Contact Form JavaScript -->
    <script src="../js/jqBootstrapValidation.js"></script>
    <script src="../js/contact_me.js"></script>

    <!-- Custom scripts for this template -->
    <script src="../js/freelancer.min.js"></script>

</body>

</html>
package com.food42.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

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 org.apache.log4j.Logger;
import com.food42.util.User;

@WebServlet(name = "Login", urlPatterns = {"/Login"})
public class LoginServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    static Logger logger = Logger.getLogger(LoginServlet.class);

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String errorMsg = null;

        if(username == null || username.equals("")){
            errorMsg ="Username can't be null or empty";
        }

        if(password == null || password.equals("")){
            errorMsg = "Password can't be null or empty";
        }

        if(errorMsg != null){
            RequestDispatcher rd = getServletContext().getRequestDispatcher("/login.jsp");
            PrintWriter out= response.getWriter();
            out.println("<font color=red>"+errorMsg+"</font>");
            rd.include(request, response);
        } else {
        Connection con = (Connection) getServletContext().getAttribute("DBConnection");
        PreparedStatement ps = null;
        ResultSet rs = null;

        try {
            ps = con.prepareStatement("select id, title, prename, lastname, dateofbirth, mail, signupdate from user where username=? and password=? limit 1");
            ps.setString(1, username);
            ps.setString(2, password);
            rs = ps.executeQuery();

            if(rs != null && rs.next()){

                User user = new User(rs.getString("lastName"), rs.getString("preName"), rs.getInt("id"), rs.getString("birthDate"), rs.getString("zip"), rs.getString("cityName"), rs.getString("street"), rs.getString("mail"), rs.getString("username"));
                logger.info("User found with details = " + user);
                HttpSession session = request.getSession();
                session.setAttribute("User", user);
                response.sendRedirect("/index.jsp");;
            } else {
                RequestDispatcher rd = getServletContext().getRequestDispatcher("/login.jsp");
                PrintWriter out= response.getWriter();
                logger.error("User not found with username = " + username);
                out.println("<font color=red>No user found with given email id, please register first.</font>");
                rd.include(request, response);
            }
        } catch (SQLException e) {
            e.printStackTrace();
            logger.error("Database connection problem");
            throw new ServletException("Database Connection problem.");
        } 
            finally{
                try {
                    rs.close();
                    ps.close();
                } catch (SQLException e) {
                    logger.error("SQLException in closing PreparedStatement or ResultSet");;
                }
            }
        }
    }
}

2

Answers


  1. Chosen as BEST ANSWER

    I managed to solve the problem. I'll post the solution maybe it helps someone with a similar kind of problem.

    I had to change my project directory structure. My problem was that i named my java-packages com.example.test (in my project com.food42.servlet) instead of creating a directory for com and in the com directory a directory named example and so on.

    Before:

    src
    ├── main
    │   ├── com.food42.database
    │   │   └── database.sql
    │   ├── com.food42.servlet
    │   │   ├── AuthenticationFilter.java
    │   │   ├── LoginServlet.java
    │   │   ├── LogoutServlet.java
    │   │   └── RegisterServlet.java
    │   ├── com.food42.servlet.errorhandler
    │   │   └── AppErrorHandler.java
    │   ├── com.food42.servlet.listeners
    │   │   └── AppContextListener.java
    │   └── com.food42.util
    │       ├── DBConnectionManager.java
    │       └── User.java
    └── test
        └── TestLoginFilter.java
    

    Afterwards:

    src
    ├── main
    │   └── java
    │       └── com
    │           └── food42
    │               ├── database
    │               │   └── database.sql
    │               ├── servlet
    │               │   ├── AuthenticationFilter.java
    │               │   ├── LoginServlet.java
    │               │   ├── LogoutServlet.java
    │               │   ├── RegisterServlet.java
    │               │   ├── errorhandler
    │               │   │   └── AppErrorHandler.java
    │               │   └── listeners
    │               │       └── AppContextListener.java
    │               └── util
    │                   ├── DBConnectionManager.java
    │                   └── User.java
    └── test
        └── TestLoginFilter.java
    

    Credit to my professor who helped me find out what the problem was =)

    Some images to better show my changes:

    Directory structure Deployment_Assembly Build_path_source Build_path_libraries


  2. In a similar project I have, I do it this way,

    1 – I don’t declare anything in the Servlet class, just have it like that,

    public class Margin extends HttpServlet { ... }
    

    & I make all the Servlet declarations inside my web.xml,

    <servlet>
        <servlet-name>Margin</servlet-name>
        <servlet-class>gr.thebexperts.servlets.Margin</servlet-class>
    </servlet>
    <servlet-mapping>
         <servlet-name>Margin</servlet-name>
         <url-pattern>/Margin</url-pattern>
    </servlet-mapping>
    

    This is probably not related to your issue but you can try it in a similar way.

    2 – What is different though that maybe causes the issue is that you can try to place your login.jsp page at the same level as WEB-INF, meaning directly under /WebContent, it will probably work.

    Hope it helps!

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search