美文网首页
servlet入门

servlet入门

作者: 陈柴盐 | 来源:发表于2019-04-28 10:05 被阅读0次

    使用工具:IDEA

    创建Web工程

    image.png
    image.png
    image.png

    文件结构

    image.png

    HelloServlet

    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 {
            resp.setContentType("text/html;charset=utf-8");
            PrintWriter out = resp.getWriter();
            out.println("<h1>Hello Servlet</h1>");
        }
    }
    

    web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns="http://java.sun.com/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
             version="3.0">
    
        <servlet>
            <servlet-name>hello</servlet-name>
            <servlet-class>com.chen.web.HelloServlet</servlet-class>
        </servlet>
        <servlet-mapping>
            <servlet-name>hello</servlet-name>
            <url-pattern>/hello</url-pattern>
        </servlet-mapping>
    
    </web-app>
    

    启动项目

    image.png

    访问

    image.png

    相关文章

      网友评论

          本文标题:servlet入门

          本文链接:https://www.haomeiwen.com/subject/lmirnqtx.html