2014-02-24 209 views
0

我正在嘗試使用JSP站點和Servlet進行登錄表單。在MySQL數據庫中,我有以下列:FirstNameLastName,usernamepassword。我在這裏添加了一些信息。當我運行我的JSP網站應該檢查LoginServlet,但是當我點擊提交的JSP網站,我收到以下錯誤:HTTP狀態404 servlet請求

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存在問題?我希望有一個人可以幫助我。最好的問候Mads。 下面是代碼:

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

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的小服務程序:

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

Editeded:

的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> 
+0

有你正確設置'sevlet-mappings'? –

+0

操作編號: - /好點... :-)這樣的註釋將如何顯示? – McDuck4

+0

或者我怎麼能在web.xml中做到這一點? – McDuck4

回答

0

試試這個: 在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> 
+0

我剛剛編輯了我的問題並放入了web.xml。它看起來正確嗎? – McDuck4

+0

當我運行代碼時,我進入頁面「Invaliduserlogin.jsp」。所以現在發生了一些事情,但我只是得到了錯誤的頁面: - /我再次檢查了我根據您發佈的內容輸入了正確的信息 – McDuck4

+0

,這是正確的。 –