Servlet学习:(一)使用IDEA手动编写一个Servlet
程序员文章站
2022-04-10 17:10:57
...
目录
(1)在WEB-INFO目录下创建classes和lib目录
(2)打开Project Structure(默认快捷键Ctrl+Alt+Shift+S 或者 File 菜单 -> Project Structure...),配置compiler output
(3)打开Project Structure(默认快捷键Ctrl+Alt+Shift+S 或者 File 菜单 -> Project Structure...),配置依赖包
1、新建项目
2、填写项目名称(MyFirstServlet)
3、项目创建成功后的目录初始结构
4、配置
(1)在WEB-INFO目录下创建classes和lib目录
classes:用于存放编译后的class
lib:用于存放依赖包
(2)打开Project Structure(默认快捷键Ctrl+Alt+Shift+S 或者 File 菜单 -> Project Structure...),配置compiler output
(3)打开Project Structure(默认快捷键Ctrl+Alt+Shift+S 或者 File 菜单 -> Project Structure...),配置依赖包
5、第一个Servlet类
(1)创建Servlet类
(2)编写代码
package com.idark;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet(name = "DemoServlet",urlPatterns = "/myFirstServlet")
public class DemoServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter write = response.getWriter();
write.print("Hello , This is my first servlet");
}
}
(3)启动tomcat(SHIFT+F10)
(4)访问http://localhost:8080/myFirstServlet
下一篇: mongdb添加索引