a标签及表单跳转Servlet的实现

不用加载servlet-api.jar到Build-path

目录结构如下:


web.xml配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
  <servlet>
    <servlet-name>MyServlet</servlet-name>
    <servlet-class>servlet.MyServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>MyServlet</servlet-name>
    <url-pattern>/MyServlet</url-pattern>
  </servlet-mapping>

</web-app>

index.jsp中代码如下:

<body>
    <a href="MyServlet">点我跳转</a>
    <form action="MyServlet" method="post"><button type="submit">点我跳转</button></form>
</body>

success.jsp中代码如下:

 <body>
    This is my JSP page. <br>
    <a href="index.jsp">点我返回</a>
  </body>

MyServlet中代码如下:

package servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MyServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * Constructor of the object.
	 */
	public MyServlet() {
		super();
	}
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		request.getRequestDispatcher("success.jsp").forward(request, response); 
	}

}


点击后跳转到success.jsp




展开阅读全文

没有更多推荐了,返回首页