我正在使用Eclipse和Tomcat 9.0(动态Web模块4.0,而不生成web.xml部署。)>
项目目录如下:
其中reverse.java是一个servlet,它获取输入参数,将其发送回输出页面。出于某种原因,在我单击“提交”按钮后,无法访问“ /反向” servlet,并且tomcat提示“ HTTP状态404-找不到”页面。我做错了什么?
源代码:
inputForm.jsp
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Take Input</title> </head> <body> <form action="../reverse"> <table cellspacing="4"> <tr> <td>Enter your input string:</td> <td><input name="input" type="text" size="20"></td> <td style="color: red"><%=error_message%></td> </tr> <tr> <td></td> <td><input type="submit" value="Submit"></td> <td></td> </tr> </table> </form> </body> </html>
outputForm.jsp
<html> <body> <% String output = "?"; String reservedInput = request.getParameter("input"); if (reservedInput != null) output = reservedInput; %> Output: <%=output%>! </body> </html>
reverse.java
@WebServlet("/reverse") public class reverse extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public reverse() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String url = "/OutputForm.jsp"; String user = request.getParameter("input"); if (user == null || user.length() == 0) { url = "/inputForm.jsp"; request.setAttribute("error", "Input must not be empty"); } ServletContext context = getServletContext(); RequestDispatcher dispatcher = context.getRequestDispatcher(url); dispatcher.forward(request, response); } /** * @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); } }
点击inputForm.jsp页面上的Submit按钮后,它在/ reverse上显示HTTP状态404 –在Tomcat服务器上未找到错误。输入输入=“ abcd”时我在提交时调用的URL:http://localhost:8080/reverse?input=abcd
我做错了什么?谢谢。
我正在使用Eclipse和Tomcat 9.0(动态Web模块4.0)进行示例,而没有生成web.xml部署。项目目录如下:其中reverse.java是一个servlet,它得到...
如果未显式设置set context root,默认情况下它会设置为与项目相同的名称。如果您的项目名称为“ bla”,则用于调用servlet的URL应该如下:http://localhost:8080/bla/reverse