我有index.jsp页面:
</head>
<body>
<form action="HelloWorldServlet" method="post">
<input class="button" type="submit" value="load list" />
</form>
</body>
</head>
我想通过单击“加载列表”按钮运行HelloWorldServlet的位置。
public class HelloWorldServlet extends HttpServlet {
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
PrintWriter writer = response.getWriter();
writer.println("Hello, World!");
writer.close();
}
}
而不是“你好,世界!”消息,它尝试加载
http://localhost:8080/HelloWorld/HelloWorldServlet
页面..有什么意义?这是我的web.xml:
<!DOCTYPE web-app PUBLIC '-//Sun Microsystems, Inc.//DTD
Web Application 2.3//EN' 'http://java.sun.com/dtd/web-app_2_3.dtd'>
<web-app>
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>HelloWorldServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
</web-app>
最佳答案
如果您的servlet映射到/hello
,则您的<form>
应该发布到相同的URL。
<form action="hello" method="post">
关于java - 从jsp页运行servlet,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12585159/