伙计们!我是 servlet 的新手。我尝试按照书的步骤创建一个servlet。它只是一个登录表单,用户在其中输入用户ID和密码单击登录名,然后它应该在网页中显示输入值。但是,当我输入用户ID和密码时,我收到Http404错误。
我想知道上下文可能有问题.xml但我不确定。我也尝试在 xml 中映射 servlet,但仍然收到错误。
这是我的 HTML
<!DOCTYPE html>
<html>
<head>
<title>USER LOGIN</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial- scale=1.0">
</head>
<body>
<form action="UserServlet" method="get">
<!-- Name text Field -->
<p>
<label>User ID</label>
<input type ="text" name="userId" size="30"/>
</p>
<p>
<label>User Password</label>
<input type ="text" name="userPassword" size="30"/>
</p>
<!--Button for submit -->
<input type ="submit" name="Login" value="LogIn"/>
<input type ="button" value="LogOut" onclick="self.close()"/>
</form>
</body>
这是我的仆人.java
public class UserServlet extends HttpServlet
{
//process the HTTP GET REQUEST//
@Override
public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException
{
response.setContentType("text/html");
PrintWriter out=response.getWriter();
//get the data
String userId=request.getParameter("userId");
String passWord=request.getParameter("userPassWord");
//determine the input if user missing these two send back the message
if (userId.isEmpty()&&passWord.isEmpty())
{
out.println("UserId and passWord can not be empty.");
}
else
{
out.println("<p>your id is "+userId);
out.println("<br>your password is"+passWord);
out.println("<br>You entered the data successfully </p>");
}
}
}
这是我的背景.xml
<?xml version="1.0" encoding="UTF-8"?>
<Context path="/userLogin"/>
我没有在上下文中改变任何事情.xml
当我运行项目时它可以工作,但是一旦我单击按钮,它就会给我
类型状态报告
Message/userLogin/UserServlet
说明 源服务器未找到目标资源的当前表示形式,或者不愿意透露目标资源存在。
你能检查一下web.xml中提供的servlet映射是什么吗?我认为映射与请求不匹配。或者您可以发布网络.xml