idea中web项目的创建
作者:互联网
在idea中创建web项目
1)创建一个普通的Java项目
2)右键项目选择ADD Framework Support
3)勾选JavaEE
4)添加jar包
点击Project Structure
5)部署Tomcat
配置项目访问路径
在Web工程中编写Servlet
在src下创建HelloServlet.java
package www.servlet; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; public class HelloServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { PrintWriter pw=resp.getWriter(); pw.println("HelloServlet"); pw.flush(); pw.close(); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doGet(req,resp); } }
在web.xml对该servlet进行配置
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <!--为servlet取名--> <servlet> <servlet-name>servlet1</servlet-name> <servlet-class>www.servlet.HelloServlet</servlet-class> </servlet> <!--做URL映射--> <servlet-mapping> <servlet-name>servlet1</servlet-name> <url-pattern>/helloservlet</url-pattern> </servlet-mapping> </web-app>
启动Tomcat
标签:web,pw,创建,resp,idea,import,servlet,javax,HelloServlet 来源: https://www.cnblogs.com/chaplu/p/16063133.html