html请求的资源不适用于servlet

时间:2018-01-07 18:51:34

标签: java jsp servlets web

我正在尝试将html连接到servlet以获取用户输入,但是我收到一条错误消息,指出所请求的资源不可用。我甚至将servlet放在与html文件相同的文件夹中,但错误仍然存​​在。我在Java资源和html文件夹(web内容)中都有servlet。它应该是可见的。下面是我的html文件和servlet的代码。 html文件:

<!DOCTYPE html>
<html>
<!--This is the login page.-->
<head>
<meta charset="ISO-8859-1">
<title>Login Page</title>
<style>
body {
    background-color: lightblue;
    backroung-repeat: repeat-1 ; 
}

h1 {
    color: maroon;
    margin-left: 40px;
} 
</style>
</head>
<body background="https://cdn4.iconfinder.com/data/icons/gray-user-management/512/login-512.png" >
<h1>Autentification</h1>
<form action="ConnectionMaker" method="post">
User name:<Input type="text" name="user"><br/><br/>
Password:<Input type="password" name="pass"><br/><br/>
<input type="submit" value="Submit">

<a href="autentification.jsp">login</a>
</form>
</body>
</html>

和servlet:

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

/**
 * Servlet implementation class ConnectionMaker
 */
@WebServlet("/DBConnection")

public class ConnectionMaker extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public ConnectionMaker() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        response.getWriter().append("Served at: ").append(request.getContextPath());
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
         String user = request.getParameter("user");
            String pass= request.getParameter("pass");
            System.out.println(user+" "+pass);
    }

}

1 个答案:

答案 0 :(得分:0)

<form action="ConnectionMaker" method="post">更改为<form action="DBConnection" method="post">/DBConnection(使用斜杠)。您点击了URL,而不是类名。

另外,为什么在doGet内拨打doPost