jsp 转向servelet
我想通过JSP 页面转到 一个SERVLET 文件下面
以下是创建的web项目结构 src 目录下面有个pack包 pack包下面有个SimpleHello.java servlet类
内容如下
package pack;
import
java.io.IOException;
import java.io.PrintWriter;
import
javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class SimpleHello extends HttpServlet {
public SimpleHello() {
super();
}
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws
ServletException,
IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the GET method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out
.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the POST method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
public void init() throws ServletException {
// Put your code here
}
}
然后 有个 index.jsp文件内容如下
body>
This is selevelet test.
<form method="post" action="/sayHello">
<input type="submit" ></input>
</form>
<br>
</body>
web。xml 文件内容如下
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>SimpleHello</servlet-name>
<servlet-class>pack.SimpleHello</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SimpleHello</servlet-name>
<url-pattern>/sayHello</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
结果 输入 http://localhost:8080/sevelet/index.jsp 能正常显示。但是。点击按钮却不能转过去。显示
HTTP Status 404 - /sayHello
--------------------------------------------
type Status report
message /sayHello
description The requested resource (/sayHello) is not available.
不知道哪错了哪位朋友帮忙解决下问题谢谢。
------解决方案--------------------
在你的 <form method="post" action="/sayHello"> 的action="/sayHello" 那个 / 去掉。