我已经使用Eclipse Luna IDE和一个简单的jsp页面创建了一个简单的servlet。当我部署它时,它不起作用。我正在使用tomcat 8。
错误是
HTTP状态404-/ ServletLearning / DynamicPage / welcome
类型:状态报告
消息:/ ServletLearning / DynamicPage / welcome
描述:请求的资源不可用。
我的代码在这里。
package atulrai;
import java.io.IOException;
import java.io.PrintWriter;
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 DynamicServlet
*/
@WebServlet("/DynamicServlet")
public class DynamicServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setContentType("text/html");
PrintWriter pw = res.getWriter();
String name = req.getParameter("name");
pw.println("Welcome " + name);
}
}
Jsp页面是
<html>
<head>
<meta charset="ISO-8859-1">
<title>dynamic page using Servlet</title>
</head>
<body>
<form action="welcome" method="get">
Enter your name:<input type="text" name="name"><br>
<input type="submit" value="login">
</form>
</body>
</html>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>ServletLearning</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>atulrai</servlet-name>
<servlet-class>DynamicServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>atulrai</servlet-name>
<url-pattern>/welcome</url-pattern>
</servlet-mapping>
最佳答案
尝试/ ServletLearning / welcome或将index.jsp文件放入WEB-INF。