当前位置:坤哥网-kwan-JavaWeb学习笔记1:IDEA 中开发 Servlet 记录

JavaWeb学习笔记1:IDEA 中开发 Servlet 记录

2019/9/11 0:30:19 IT综合阅读(44) 评论(0)


在项目中添加一个普通类,继承 HttpServlet ,但是 HttpServlet 是属于 tomcat 下的,因此需要引入 tomcat 包。


导入 tomcat 包


File->Project Structure:

e0128f2d-ce0b-462f-a0ec-3117d45db11b.png

点击 Global Libraries,选择 Java:

a8661628-31d6-4f8f-a070-d50ab233a038.png

选择 tomcat 安装的目录下 lib 目录:

c418675d-3592-444a-a354-a0cf0a1d312e.png

选择 Modules:

e27c9198-198a-4fa5-9b5a-b39518212c90.png

4c67d4fd-7563-436d-bdb0-ee8aa1b20c47.png

确定之后 tomcat 包就加进项目里了:

f96bf3c8-9a5d-4115-aef6-4d4114c31592.png

在 MyServlet 类中导入 import javax.servlet.http.HttpServlet; 即可:

4b462de2-db82-44be-b7ff-b679ec0e0f6d.png

后面需要实现 HttpServlet 的方法了。


普通类继承 HttpServlet 


创建普通类 MyServlet ,继承 HttpServlet ,接下来我们要重写其方法,doGet、doPost:

package com.demo;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class MyServlet extends HttpServlet {

    public  MyServlet(){
        System.out.println("MyServlet 实例化");
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("开始调用父类 doGet 方法 req = [" + req + "], resp = [" + resp + "]");
        super.doGet(req, resp);
        System.out.println("调用父类 doGet 方法完成 req = [" + req + "], resp = [" + resp + "]");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("开始调用父类 doPost 方法 req = [" + req + "], resp = [" + resp + "]");
        super.doPost(req, resp);
        System.out.println("调用父类 doPost 方法完成 req = [" + req + "], resp = [" + resp + "]");
    }

    public static void main(String[] args) {
        System.out.println("MyServlet main");
    }

}

接下来要发布我们的 Web 项目,让其在 Web 容器中运行。


添加测试的Html


在 Web 文件夹下新建一个静态网页 index.html 用来测试写好的 MyServlet:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Servlet Test</title>
</head>
<body>
<fieldset>
    <legend>POST提交</legend>
    <form action="url1" method="post">
        <p><input type="submit" value="POST提交"></p>
    </form>
</fieldset>
<fieldset>
    <legend>GET提交</legend>
    <form action="url1" method="get">
        <p><input type="submit" value="GET提交"></p>
    </form>
</fieldset>
</body>
</html>


发布


右上角,【Edit Configurations】:

750b8313-1a11-4498-af74-cd9511d637fc.png

点击左上角的【+】:b34dbe7e-98dc-4ea9-bbcc-0125dadb66a0.png

选择 【Tomcat Server】【Local】:

63e4fdaf-361c-42ec-8f66-f05d42d6a6be.png

添加 Tomcat,选择 Tomcat 所在的文件夹:

46246b18-db27-4b8e-a3b6-a1ec22193240.png

e09000c7-6616-4526-962b-e9526d92fbb1.png4f987faa-2709-4552-b358-dae1f300695c.png

选择 Deployment,右边的【+】,选择 Artifact...:6192c1a5-53e1-428b-9394-95e74ee83af1.png

e2a928db-08f9-4cf8-9f7c-0fc8a9f04377.png

可以重命名下服务器的名称:

e59e8d17-563b-4e2d-8a4f-30214056fa20.png

Apply,然后 OK。



运行


点击右上角将项目运行起来:

d66764ee-0d48-49ac-b88a-a4393072b9e7.png

经测试 tomcat 9.0.16 与 jdk 1.8.0 不兼容,故本次添加了一个 tomcat 8.5.45。

2c996c38-c34b-4e6f-822d-17c0b2de104d.png

tomcat 启动:

d7f229df-112b-4981-a479-d754f518e553.png

D:\noinstall\apache-tomcat-8.5.45\bin\catalina.bat run
[2019-09-13 04:33:17,130] Artifact WebModuleTest:war exploded: Waiting for server connection to start artifact deployment...
Using CATALINA_BASE:   "C:\Users\kungge\.IntelliJIdea2018.3\system\tomcat\Unnamed_IDEAProjTest_2"
Using CATALINA_HOME:   "D:\noinstall\apache-tomcat-8.5.45"
Using CATALINA_TMPDIR: "D:\noinstall\apache-tomcat-8.5.45\temp"
Using JRE_HOME:        "D:\install\Java\jdk1.8.0_181"
Using CLASSPATH:       "D:\noinstall\apache-tomcat-8.5.45\bin\bootstrap.jar;D:\noinstall\apache-tomcat-8.5.45\bin\tomcat-juli.jar"
13-Sep-2019 16:33:18.338 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log Server version:        Apache Tomcat/8.5.45
13-Sep-2019 16:33:18.340 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log Server built:          Aug 14 2019 22:21:25 UTC
13-Sep-2019 16:33:18.340 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log Server number:         8.5.45.0
13-Sep-2019 16:33:18.340 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log OS Name:               Windows 10
13-Sep-2019 16:33:18.340 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log OS Version:            10.0
13-Sep-2019 16:33:18.340 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log Architecture:          amd64
13-Sep-2019 16:33:18.340 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log Java Home:             D:\install\Java\jdk1.8.0_181\jre
13-Sep-2019 16:33:18.340 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Version:           1.8.0_181-b13
13-Sep-2019 16:33:18.340 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Vendor:            Oracle Corporation
13-Sep-2019 16:33:18.340 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_BASE:         C:\Users\kungge\.IntelliJIdea2018.3\system\tomcat\Unnamed_IDEAProjTest_2
13-Sep-2019 16:33:18.340 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_HOME:         D:\noinstall\apache-tomcat-8.5.45
13-Sep-2019 16:33:18.341 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.config.file=C:\Users\kungge\.IntelliJIdea2018.3\system\tomcat\Unnamed_IDEAProjTest_2\conf\logging.properties
13-Sep-2019 16:33:18.341 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
13-Sep-2019 16:33:18.341 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcom.sun.management.jmxremote=
13-Sep-2019 16:33:18.341 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcom.sun.management.jmxremote.port=1099
13-Sep-2019 16:33:18.341 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcom.sun.management.jmxremote.ssl=false
13-Sep-2019 16:33:18.341 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcom.sun.management.jmxremote.password.file=C:\Users\kungge\.IntelliJIdea2018.3\system\tomcat\Unnamed_IDEAProjTest_2\jmxremote.password
13-Sep-2019 16:33:18.341 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcom.sun.management.jmxremote.access.file=C:\Users\kungge\.IntelliJIdea2018.3\system\tomcat\Unnamed_IDEAProjTest_2\jmxremote.access
13-Sep-2019 16:33:18.342 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.rmi.server.hostname=127.0.0.1
13-Sep-2019 16:33:18.342 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djdk.tls.ephemeralDHKeySize=2048
13-Sep-2019 16:33:18.342 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.protocol.handler.pkgs=org.apache.catalina.webresources
13-Sep-2019 16:33:18.342 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dignore.endorsed.dirs=
13-Sep-2019 16:33:18.342 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.base=C:\Users\kungge\.IntelliJIdea2018.3\system\tomcat\Unnamed_IDEAProjTest_2
13-Sep-2019 16:33:18.342 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.home=D:\noinstall\apache-tomcat-8.5.45
13-Sep-2019 16:33:18.342 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.io.tmpdir=D:\noinstall\apache-tomcat-8.5.45\temp
13-Sep-2019 16:33:18.342 信息 [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [D:\install\Java\jdk1.8.0_181\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files\dotnet\;C:\Program Files\Microsoft SQL Server\130\Tools\Binn\;D:\Program Files\Git\cmd;D:\portable\mysql-5.7.26\bin;C:\Users\kungge\AppData\Local\Microsoft\WindowsApps;D:\install\Java\jdk1.8.0_181\bin;;D:\Users\kungge\AppData\Local\Programs\Microsoft VS Code\bin;.]
13-Sep-2019 16:33:18.594 信息 [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["http-nio-8080"]
13-Sep-2019 16:33:18.609 信息 [main] org.apache.tomcat.util.net.NioSelectorPool.getSharedSelector Using a shared selector for servlet write/read
13-Sep-2019 16:33:18.618 信息 [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["ajp-nio-8009"]
13-Sep-2019 16:33:18.625 信息 [main] org.apache.tomcat.util.net.NioSelectorPool.getSharedSelector Using a shared selector for servlet write/read
13-Sep-2019 16:33:18.626 信息 [main] org.apache.catalina.startup.Catalina.load Initialization processed in 538 ms
13-Sep-2019 16:33:18.654 信息 [main] org.apache.catalina.core.StandardService.startInternal Starting service [Catalina]
13-Sep-2019 16:33:18.655 信息 [main] org.apache.catalina.core.StandardEngine.startInternal Starting Servlet Engine: Apache Tomcat/8.5.45
13-Sep-2019 16:33:18.667 信息 [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-nio-8080"]
13-Sep-2019 16:33:18.678 信息 [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["ajp-nio-8009"]
13-Sep-2019 16:33:18.683 信息 [main] org.apache.catalina.startup.Catalina.start Server startup in 56 ms
Connected to server
[2019-09-13 04:33:18,715] Artifact WebModuleTest:war exploded: Artifact is being deployed, please wait...
13-Sep-2019 16:33:18.959 警告 [RMI TCP Connection(3)-127.0.0.1] org.apache.tomcat.util.descriptor.web.WebXml.setVersion Unknown version string [4.0]. Default version will be used.
[2019-09-13 04:33:19,096] Artifact WebModuleTest:war exploded: Artifact is deployed successfully
[2019-09-13 04:33:19,097] Artifact WebModuleTest:war exploded: Deploy took 382 milliseconds
13-Sep-2019 16:33:28.678 信息 [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [D:\noinstall\apache-tomcat-8.5.45\webapps\manager]
13-Sep-2019 16:33:28.745 信息 [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [D:\noinstall\apache-tomcat-8.5.45\webapps\manager] has finished in [67] ms

浏览器页面:

7fd05f2e-a448-4c06-a23a-b8108c7844a1.png

点击【post提交】按钮,输出了如下:8a54ac38-bac3-416b-a7f5-06e824583bab.png

MyServlet ʵ����
��ʼ���ø��� doPost ���� req = [org.apache.catalina.connector.RequestFacade@da69797], resp = [org.apache.catalina.connector.ResponseFacade@3617853]
���ø��� doPost ������� req = [org.apache.catalina.connector.RequestFacade@da69797], resp = [org.apache.catalina.connector.ResponseFacade@3617853]

是乱码,解决乱码可参考本站另一篇文章:http://www.kungge.com/tool/3909.html

这个先不用管,已经进入了我们配置的 MyServlet 类中了。


ServletConfig


通过 ServletConfig 对象我们可以获取 Servlet 的初始化参数。

在配置文件 web.xml 中给 MyServlet 添加初始化参数:

<?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-name>ServletName1</servlet-name>
        <servlet-class>com.demo.MyServlet</servlet-class>
        <!--给MyServlet添加初始化参数-->
        <init-param>
            <param-name>paramName1</param-name>
            <param-value>paramName1Value</param-value>
        </init-param>
        <init-param>
            <param-name>paramName2</param-name>
            <param-value>paramName2Value</param-value>
        </init-param>
    </servlet>

    <servlet-mapping>
        <servlet-name>ServletName1</servlet-name>
        <url-pattern>/url1</url-pattern>
    </servlet-mapping>
    
</web-app>

web容器会在创建 Servlet 对象时,检查如果配置了初始化参数,则会将这些参数封装到 ServletConfig 对象中,在调用 Servlet 的 init 方法时,会将 ServletConfig 对象传递给 Servlet。

将 MyServlet 类修改如下:

package com.demo;

import javax.servlet.ServletConfig;
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.util.Enumeration;

public class MyServlet extends HttpServlet {

    public  MyServlet(){
        System.out.println("MyServlet 实例化");
    }

    public ServletConfig servletConfig;

    @Override
    public void init(ServletConfig config) throws ServletException {
        System.out.println("MyServlet init start");
        this.servletConfig=config;
        System.out.println("MyServlet init end");
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("MyServlet doGet 开始调用父类 doGet 方法 req = [" + req + "], resp = [" + resp + "]");
        super.doGet(req, resp);
        System.out.println("MyServlet doGet 调用父类 doGet 方法完成 req = [" + req + "], resp = [" + resp + "]");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("MyServlet doPost 开始调用父类 doPost 方法 req = [" + req + "], resp = [" + resp + "]");

        String para1=this.servletConfig.getInitParameter("paramName1");

        System.out.println("para1="+para1);

        Enumeration<String> paramEnums=servletConfig.getInitParameterNames();
        while (paramEnums.hasMoreElements()){
           String name= paramEnums.nextElement();
           String value=servletConfig.getInitParameter(name);
           System.out.println("name="+name+" value="+value);
        }
        
        super.doPost(req, resp);
        System.out.println("MyServlet doPost 调用父类 doPost 方法完成 req = [" + req + "], resp = [" + resp + "]");

    }

    public static void main(String[] args) {
        System.out.println("MyServlet main");
    }

}

运行,初始化参数已取到,如下:91ccd295-122c-4dcb-9f47-9f5ae128c448.png


创建 Servlet 类


IDEA支持直接创建 Servlet:74c5153b-afb9-4725-aebb-0a5739fa095d.png

定义 Servlet 的名称:

f5926848-14f4-4ec4-a936-cd0955c8d43c.png

确定默认创建 doPost 和 doGet 方法:

e85aed46-2a8b-4e79-8ce1-8e087f056bbb.png

使用的是注解的方式,name 是 servlet 的名称,还可以添加 urlPatterns 来匹配地址:

package com.demo;

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;

@WebServlet(name = "MyServlet2",urlPatterns = "/url2")
public class MyServlet2 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("MyServlet2 doPost 开始调用父类 doPost 方法 req = [" + request + "], resp = [" + response + "]");
        super.doPost(request, response);
        System.out.println("MyServlet2 doPost 调用父类 doPost 方法完成 req = [" + request + "], resp = [" + response + "]");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("MyServlet2 doGet 开始调用父类 doGet 方法 req = [" + request + "], resp = [" + response + "]");
        super.doGet(request, response);
        System.out.println("MyServlet2 doGet 调用父类 doGet 方法完成 req = [" + request + "], resp = [" + response + "]");
    }
}


urlPatterns 是一个数组,可以匹配多个地址。

执行一把:900dec8d-0f7b-4849-95ef-55d80f367eb8.png


ServletContext


1.通过 ServletContext 实现数据共享。

新建 MyServlet3:

package com.demo;

import javax.servlet.ServletContext;
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;

@WebServlet(name = "MyServlet3",urlPatterns = "/url3")
public class MyServlet3 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("MyServlet3 doPost 开始获取 ServletContext");
        ServletContext context= this.getServletConfig().getServletContext();

        String myServlet3SetKey1Val="this is ServletContext set";
        context.setAttribute("MyServlet3SetKey1",myServlet3SetKey1Val);
        System.out.println("MyServlet3 doPost 调用setAttribute方法完成");

        response.getWriter().println("MyServlet3 设置了 MyServlet3SetKey1="+myServlet3SetKey1Val);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}

新建 MyServlet4:

package com.demo;

import javax.servlet.ServletContext;
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;

@WebServlet(name = "MyServlet4",urlPatterns = "/url4")
public class MyServlet4 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ServletContext context= this.getServletConfig().getServletContext();

        System.out.println("MyServlet4 doPost 开始调用 getAttribute 方法开始");

        String myServlet3SetKey1Val = (String)context.getAttribute("MyServlet3SetKey1");

        System.out.println("MyServlet4 doPost 开始调用 getAttribute 方法结束");

        System.out.println(myServlet3SetKey1Val);

        response.getWriter().println("MyServlet4 获取 myServlet3SetKey1Val="+myServlet3SetKey1Val);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}


运行,访问 url3:

bf9313ad-2c8e-4873-8bf7-b2bca9f9a8b3.png

访问 url4:

dea53d90-b146-4f8b-998d-3b910f0f63d6.png

乱码问题先忽略,后面会讲处理乱码问题。

后台打印:6fde4bfd-0b2f-44bb-a92d-faef00f042f0.png


2.通过 ServletContext 获取 Web 应用的初始化参数。

在 web.xml 中添加:

<!--web应用初始化参数-->
<context-param>
    <param-name>contextParam1</param-name>
    <param-value>contextParam1Val</param-value>
</context-param>


新建 MyServlet5:

package com.demo;

import javax.servlet.ServletContext;
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;

@WebServlet(name = "MyServlet5",urlPatterns = "/url5")
public class MyServlet5 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ServletContext context=this.getServletContext();
        String theVal=context.getInitParameter("contextParam1");
        System.out.println("theVal="+theVal);
        response.getWriter().println("theVal="+theVal);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}

浏览器访问 url5:

854d6a6b-561a-4144-8a2b-f6955a9304ee.png

后台打印:2c3894cb-cfc6-4460-a920-df3c2e868ba2.png


3.通过 ServletContext 实现请求转发。

新建 MyServlet6:

package com.demo;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
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;

@WebServlet(name = "MyServlet6",urlPatterns = "/url6")
public class MyServlet6 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        System.out.println("this is MyServlet6");

        String str="MyServlet6 Content";
        response.getOutputStream().write(str.getBytes());

        System.out.println(str);

        ServletContext context=this.getServletContext();
        RequestDispatcher rd=context.getRequestDispatcher("/url7");
        rd.forward(request,response);//请求转发
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}

新建 MyServlet7:

package com.demo;

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;

@WebServlet(name = "MyServlet7",urlPatterns = "/url7")
public class MyServlet7 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("this is MyServlet7");

        String str="MyServlet7 Content";
        response.getOutputStream().write(str.getBytes());
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}

浏览器访问 url6:

0adebbdc-bf66-45e2-9098-188c3c967988.png

还可以通过 ServletContext 读取资源文件。





分类: java JavaWeb

发表评论 没有账号,注册评论