简体   繁体   中英

JSP - submit a form to servlet

Maybe it is a simple issue but I am quite new to JAVA and I do not understand why my simple login form is not working. Here is a screenshot of my project structure:

在此处输入图片说明

Inside my index.jsp I have the following form:

<form action="/LoginServlet" method="post" enctype="multipart/form-data" class="form-horizontal">
    <div class="form-group">
        <label>E-Mail Adresse</label>
        <input class="au-input au-input--full" type="email" name="un" placeholder="E-Mail">
    </div>
    <div class="form-group">
        <label>Passwort</label>
        <input class="au-input au-input--full" type="password" name="pw" placeholder="Passwort">
    </div>
    <div class="login-checkbox">
        <label>
            <input type="checkbox" name="remember">Merken
        </label>
        <label>
            <a href="#">Passwort vergessen?</a>
        </label>
    </div>
    <button class="au-btn au-btn--block au-btn--green m-b-20" type="submit">Anmelden</button>
</form>

And my LoginServlet.java, inside the src-folder looks like this:

package controller;
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;

/** * Servlet implementation class LoginServlet */
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet { 
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    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("/index.jsp"); //logged-in page 

            } else 
                response.sendRedirect("/index.jsp"); //error page 
        } 
        catch (Throwable theException) { 
            System.out.println(theException);       
        } 
    } 
}

So as you can see, I have a form with action="/LoginServlet" . Inside my LoginServlet.java I am using @WebServlet("/LoginServlet") but as soon as I submit my form, I get a HTTP Status 404 – Not Found message.

I really do not understand why? Does anyone have an idea what I am doing wrong? What am I missing? Any help would be really appreciated.

<form action="LoginServlet" method="post" enctype="multipart/form-data" class="form-horizontal">

Just Update your action="LoginServlet" remove / and if not work try to remove enctype="multipart/form-data" from Form

Be Assure that your index.jsp in WebContent folder not in Web-INF

<form action="/LoginServlet" method="post" enctype="multipart/form-data" class="form-horizontal">

您的表单使用method =“ post”,因此您需要在LoginServlet上重写doPost方法

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM