一、通过File->new->others找到Dynamic Web project,创建一个动态的web项目
二、配置项目
再找个位置配置你下载的Tomcat的版本
点击next
Tomcat存放的位置
本地JRE
点击finish结束
新建完成
三、实现一个表单提交验证
1.在Java Resources下的src下通过new创建一个包,并在该包下创建一个servlet类
点击next
(输入描述后点击next)
(初学的话,不需要更改)
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class TextServlet
*/
@WebServlet(description = "chapter01 Servlet", urlPatterns = { "/TextServlet" })
public class TextServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public TextServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
}
/**
* @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);
}
}
(自动生成的代码)
- doGet()方法用来处理通过get提交的表单信息.
- doPost()方法用来处理通过Post提交的表单信息
- urlPatterns 表示访问该类的路径(当前处于根目录下)
2.在WebContent下创建jsp文件,命名denglu.jsp
测试一下
版权归原作者 小郭同学忒骚了 所有, 如有侵权,请联系我们删除。