JSP Servlet HTTP状态404 –找不到

时间:2019-03-08 07:18:52

标签: java xml jsp servlets java-ee

这是我的第一篇文章。我希望有人可以帮助我=) 通常,我会尝试自己寻找解决此类问题的方法。 但是这个问题让我头疼...

在下面的html代码中有action标签,我想启动Java servlet。 可悲的是,我总是收到HTTP 404-Not found错误。 我已经尝试写过/ Login,.. / Login,Login(大写和小写)等等。

这是我的目录结构:

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

我已经尝试过在类似帖子中回答的解决方案,但可惜它们没有帮助。

在我的Java构建路径中,我安装了以下库: Apache Tomcat v8.5 JRE系统库[jre1.8.0_201a] Web应用程序库 MySQL JDBC驱动程序 EAR图书馆 JUnit5 log4j-1.2.17.jar

源文件夹为ps_18_gruppe09 / src / main

完整错误消息:

  

HTTP状态404 –找不到

     

类型状态报告

     

消息/ ps_18_gruppe09 /登录

     

说明原始服务器未找到当前表示   目标资源或不愿意透露其存在。

     

Apache Tomcat / 8.5.35

预先感谢您的帮助=)

<?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 个答案:

答案 0 :(得分:0)

在我拥有的类似项目中,我是这样做的,

1 -我没有在Servlet类中声明任何内容,只是有了它,

public class Margin extends HttpServlet { ... }

&我在我的web.xml中进行所有Servlet声明,

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

这可能与您的问题无关,但是您可以通过类似的方式尝试。

2 -可能引起问题的不同之处在于,您可以尝试将login.jsp页面放置在与WEB-INF相同的级别上,即直接位于/ WebContent下,可能有用。

希望有帮助!

答案 1 :(得分:0)

我设法解决了这个问题。 我将发布解决方案,也许它可以帮助遇到类似问题的人。

我必须更改项目目录结构。 我的问题是,我将Java软件包命名为com.example.test(在我的项目com.food42.servlet中),而不是为com创建目录,而在com目录中创建了名为example等的目录。

之前:

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

之后:

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

感谢我的教授,他帮助我找出了问题所在=)

一些图片可以更好地显示我的更改:

  

Changing the text color   titlePositionAdjustment   Directory structure   Deployment_Assembly