重定向到Servlet

时间:2017-05-23 15:08:15

标签: java jsp tomcat servlets

我一直在努力寻找解决问题的方法。 我正在尝试使用Tomcat在Java中开发Web应用程序。

这是我的项目结构:

Project Structure

根据我的阅读,我知道.jsp文件应该放在web目录中,但是当我这样做时,我得到了一个:

  

HTTP状态[404] - [未找到]

如果所有我的.jsp文件都直接放在“simpleLogin”中,那么从服务器发生

此外,这是我的 index.jsp

<html>
<head>
    <title>Title</title>
</head>
<body>
<form method="post" action="LoginCheck"> <%--Action shows you where to go, we want to go from jsp to a servlet--%>
    <table>
        <tr>
            <td>
                UserName:
            </td>
            <td>
                <input type="text" name="username"/>
            </td>
        </tr>
        <tr>
            <td>
                Password:
            </td>
            <td>
                <input type="text" name="password"/>
            </td>
        </tr>
        <tr>
            <td></td>
            <td><input type="submit" value="login"/></td>
        </tr>
    </table>
</form>
</body>
</html>

这是我的 LoginCheck servlet类:

package example;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import java.io.IOException;

/**
 * Created by AGDS on 5/23/2017.
 */
@WebServlet("/LoginCheck")
public class LoginCheck extends HttpServlet {
    protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {

        //It will check weither username or password is correct

        String username = request.getParameter("username");
        String password = request.getParameter("password");

        System.out.println(System.getProperty("user.dir"));
        try {
            if (username.equals("java") && password.equals("1234")) {
                //Go to member's page
                response.sendRedirect("member.jsp");

            } else {
                //Go to error page
                response.sendRedirect("error.jsp");
            }
        } catch (Exception e) {
        }


    }

    protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {

    }
}

这是我的 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">
    <listener>
        <listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>LoginCheck</servlet-name>
        <servlet-class>example.LoginCheck</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>LoginCheck</servlet-name>
        <url-pattern>/LoginCheck</url-pattern>
    </servlet-mapping>
</web-app>

现在,如果我尝试按下再次导致的提交按钮:

  

HTTP状态[404] - [未找到]

我想我的路径和我的.jsp文件所在的位置有问题,或者我在web.xml中的映射有问题。

任何建议/建议都将不胜感激!

2 个答案:

答案 0 :(得分:0)

没有必要将html / jsp内容放入名为web的目录中。为了方便起见,这是IDE为您所做的事情。例如,eclipse将为同一目的创建一个名为WebContent的目录。

因此,如果您想使用Intelij管理您的项目,请确保正确配置InteliJ以将其作为Java Web应用程序处理。

查看详细信息: https://www.jetbrains.com/help/idea/2017.1/creating-and-running-your-first-web-application.html

答案 1 :(得分:0)

404错误表示文件不存在或者您没有正确提供文件路径 点击提交按钮后,网址应为localhost:8085/simpleLogin/LoginCheck而不是localhost:/LoginCheck