mysql - HTTP 状态 404 servlet 请求

标签 mysql servlets authentication

我正在尝试使用JSP站点和Servlet制作登录表单。在 MySQL 数据库中,我有以下列:FirstNameLastNameusernamepassword。这里我补充了一些信息。当我运行 JSP 站点时,它应该检查 LoginServlet,但是当我在 JSP 站点上单击“提交”时,出现以下错误:

HTTP Status 404 - /ConnectionLoginForm/LoginServlet
type Status report
message /ConnectionLoginForm/LoginServlet
description The requested resource is not available.

我的Tomcat服务器正在运行等等。有人知道我为什么会收到此错误吗?我放置 JSP 和 servlet 的路径应该是正确的。我使用的是 Eclipse,我的 JSP 位于 WebContent 下,servlet 位于 src->package->servlets 下。我发布了所有代码,但一开始我可以想象这只是 JSP 和 LoginServlet 存在问题?我希望有一个人可以帮助我。最好的问候麦兹。 这是代码:

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

登录Servlet:

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;


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

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

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;

      } 
   }

连接 Servlet:

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:mysql://localhost/ConnectionLoginFormDB"; 
            // assuming "DataSource" is your DataSource name

            Class.forName("com.mysql.jdbc.Driver");

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

            // 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;
}
   }

编辑:

web.xml

会是这样吗:

<?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>ConnectionLoginForm</display-name>
    <servlet>
        <servlet-name>LoginServlet</servlet-name>
        <servlet-class>ExamplePackage.LoginServlet</servlet-class>
    </servlet>  
    <servlet-mapping>
        <servlet-name>LoginServlet</servlet-name>
        <url-pattern>/LoginServlet</url-pattern>
    </servlet-mapping>
</web-app>

最佳答案

试试这个: 在 web.xml 中

  <servlet>
    <servlet-name>Servlet</servlet-name>
    <servlet-class>Servlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Servlet</servlet-name>
    <url-pattern>/servlet</url-pattern>
  </servlet-mapping>

关于mysql - HTTP 状态 404 servlet 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21989892/

相关文章:

java - 使用 google API 在 jsp 页面上显示流程图的最佳方式

delphi - 如何使用 Delphi 创建视觉挑战/响应以恢复对应用程序的访问?

mysql - mysql 5.6安装文件夹中没有my.ini文件

php - 是否可以添加来自数据库的 jQuery slider ?

php - mysqli_affected_rows,mysql减去2列错误

java - Hello World 示例 struts 时序图

html - 如何使用 Servlet 流式传输 MP3、MP4、AVI 等音频/视频文件

java - 我在使用 @AuthenticationPrincipal 的 REST api 端点中显示了不必要的参数

java - 如何处理认证回调?

mysql - sql 显示基于计算列的分区与 mysql