具体代码如下:
jsp代码:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
Demo Servlet代码:
public class DemoServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String name = request.getParameter("name");
System.out.println(name);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
其中DemoServlet是一个继承了HttpServlet类的Servlet类,当表单提交(点击提交按钮)时,回去调用这个类的doPost()方法,执行对应的代码,通过request.getParameter("name")来获取表单的值。