Servlet响应发送重定向错误(Servlet response send redirect error) java

我刚刚得到错误404:请求的资源不可用。 我无法弄清楚这里有什么问题:

这是xml:

<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <servlet> <servlet-name>servlet</servlet-name> <servlet-class>controller.Servlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>servlet</servlet-name> <url-pattern>/login</url-pattern> </servlet-mapping> </web-app>

该servlet:

[@WebServlet(name = "controller.Servlet") public class Servlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String id = request.getParameter("id"); String password = request.getParameter("password"); if (LoginService.authentication(id, password)) { response.sendRedirect("succes.jsp"); } else { response.sendRedirect("index.jsp"); } return; }][1]

index.jsp文件:

<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Login Page</title> </head> <body> <form action="/login" method="post"> Username: <input type="text" name="id" > <br> Password: <input type="password" name="password"> <br> <input type="submit" value="submit"> </form> </body> </html>

succes.jsp:

<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Login Succesfully</title> </head> <body> <h1>Login succesfully</h1> </body> </html>

这是文件夹结构: http : //i.stack.imgur.com/ZGMH4.png

I just get the error 404: The requested resource is not available. I can't figure out what is wrong here:

This is the xml :

<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <servlet> <servlet-name>servlet</servlet-name> <servlet-class>controller.Servlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>servlet</servlet-name> <url-pattern>/login</url-pattern> </servlet-mapping> </web-app>

The servlet :

[@WebServlet(name = "controller.Servlet") public class Servlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String id = request.getParameter("id"); String password = request.getParameter("password"); if (LoginService.authentication(id, password)) { response.sendRedirect("succes.jsp"); } else { response.sendRedirect("index.jsp"); } return; }][1]

The index.jsp file :

<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Login Page</title> </head> <body> <form action="/login" method="post"> Username: <input type="text" name="id" > <br> Password: <input type="password" name="password"> <br> <input type="submit" value="submit"> </form> </body> </html>

succes.jsp :

<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Login Succesfully</title> </head> <body> <h1>Login succesfully</h1> </body> </html>

And this is the folder structure: http://i.stack.imgur.com/ZGMH4.png

最满意答案

更新web.xml:

<servlet> <servlet-name>controller.Servlet</servlet-name> <servlet-class>com..Servlet(replace with your servlet class namespace)</servlet-class> </servlet>

或者,您可以像这样在@WebServlet中定义urlPatterns,并从web.xml中移除servlet定义和url模式:

@WebServlet(name="controller.Servlet", urlPatterns={"/login"}) public class Servlet extends HttpServlet{ ....... }

web.xml中:

<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> </web-app>

Update web.xml:

<servlet> <servlet-name>controller.Servlet</servlet-name> <servlet-class>com..Servlet(replace with your servlet class namespace)</servlet-class> </servlet>

Alternatively you can define urlPatterns in @WebServlet like this and remove servlet definition and url patterns from web.xml:

@WebServlet(name="controller.Servlet", urlPatterns={"/login"}) public class Servlet extends HttpServlet{ ....... }

web.xml:

<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> </web-app>Servlet响应发送重定向错误(Servlet response send redirect error) java

我刚刚得到错误404:请求的资源不可用。 我无法弄清楚这里有什么问题:

这是xml:

<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <servlet> <servlet-name>servlet</servlet-name> <servlet-class>controller.Servlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>servlet</servlet-name> <url-pattern>/login</url-pattern> </servlet-mapping> </web-app>

该servlet:

[@WebServlet(name = "controller.Servlet") public class Servlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String id = request.getParameter("id"); String password = request.getParameter("password"); if (LoginService.authentication(id, password)) { response.sendRedirect("succes.jsp"); } else { response.sendRedirect("index.jsp"); } return; }][1]

index.jsp文件:

<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Login Page</title> </head> <body> <form action="/login" method="post"> Username: <input type="text" name="id" > <br> Password: <input type="password" name="password"> <br> <input type="submit" value="submit"> </form> </body> </html>

succes.jsp:

<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Login Succesfully</title> </head> <body> <h1>Login succesfully</h1> </body> </html>

这是文件夹结构: http : //i.stack.imgur.com/ZGMH4.png

I just get the error 404: The requested resource is not available. I can't figure out what is wrong here:

This is the xml :

<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <servlet> <servlet-name>servlet</servlet-name> <servlet-class>controller.Servlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>servlet</servlet-name> <url-pattern>/login</url-pattern> </servlet-mapping> </web-app>

The servlet :

[@WebServlet(name = "controller.Servlet") public class Servlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String id = request.getParameter("id"); String password = request.getParameter("password"); if (LoginService.authentication(id, password)) { response.sendRedirect("succes.jsp"); } else { response.sendRedirect("index.jsp"); } return; }][1]

The index.jsp file :

<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Login Page</title> </head> <body> <form action="/login" method="post"> Username: <input type="text" name="id" > <br> Password: <input type="password" name="password"> <br> <input type="submit" value="submit"> </form> </body> </html>

succes.jsp :

<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Login Succesfully</title> </head> <body> <h1>Login succesfully</h1> </body> </html>

And this is the folder structure: http://i.stack.imgur.com/ZGMH4.png

最满意答案

更新web.xml:

<servlet> <servlet-name>controller.Servlet</servlet-name> <servlet-class>com..Servlet(replace with your servlet class namespace)</servlet-class> </servlet>

或者,您可以像这样在@WebServlet中定义urlPatterns,并从web.xml中移除servlet定义和url模式:

@WebServlet(name="controller.Servlet", urlPatterns={"/login"}) public class Servlet extends HttpServlet{ ....... }

web.xml中:

<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> </web-app>

Update web.xml:

<servlet> <servlet-name>controller.Servlet</servlet-name> <servlet-class>com..Servlet(replace with your servlet class namespace)</servlet-class> </servlet>

Alternatively you can define urlPatterns in @WebServlet like this and remove servlet definition and url patterns from web.xml:

@WebServlet(name="controller.Servlet", urlPatterns={"/login"}) public class Servlet extends HttpServlet{ ....... }

web.xml:

<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> </web-app>