1
(1)File->new->project 创建一个新的project
选中Web Appplication
然后点击next
(2)输入项目名称 例如 FirstServlet
(3)可以看到project的结构
(4)然后右键src->new->Servlet
(5)填写Servlet名称
(6)点击放大镜左边的选项进行导包
(7)找到如图两个包
开始配置tomcat
(1)点击run->Edit configurations
(2)选择好
(3)添加artifical
然后完成可以编写第一个servlet
@WebServlet("/Servlet1")
public class Servlet1 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");//设置相应内容类型器
PrintWriter out = response.getWriter();//取得响应输出对象
String name = request.getParameter("name");
out.println("<html>");
out.println("<head>");
out.println("<title> Hello </title>");
out.println("</head>");
out.println("<body>");
out.println("<h1> Hello "+name+" ~~</h1>");
out.println("</body>");
out.println("</html>");
out.close();
}
}
运行成功