《Oracle Java SE编程指南》26-08:Java E

2020-05-19  本文已影响0人  AT阿宝哥
课程封面-JavaSE-AT阿宝哥

内容导航


1、Servlet


package com.company.project.demo02;

//导入所需的包

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

//继承HttpServlet类
public class HelloServlet extends HttpServlet {

    // 处理请求的方法
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        // 将数据发送给客户端
        response.setContentType("text/html;charset=GBK");
        PrintWriter out = response.getWriter();

        // Servlet输出HTML标签和内容
        out.println("<html>");
        out.println("  <head><title>Servlet</title></head>");
        out.println("  <body>");
        out.println("你好,欢迎来到Servlet世界");
        out.println("  </body>");
        out.println("</html>");
        out.close();
    }
}


2、Web.xml


    <servlet>
        <description>我们的第一个Servlet程序!</description>

        <display-name>This is the display name of my tomcat component</display-name>

        <servlet-name>helloWorld</servlet-name>
        
        <servlet-class>com.company.project.demo01.HelloWorld</servlet-class>
    </servlet>

    <servlet-mapping>
    
        <servlet-name>helloWorld</servlet-name>
        
        <url-pattern>/helloWorld</url-pattern>
        
    </servlet-mapping>


持续更新,欢迎留言提议!
码字很累,多点赞多赞赏!


扫描二维码,关注AT阿宝哥
上一篇下一篇

猜你喜欢

热点阅读