409°

IntelliJ IDEA配置Tomcat

Tomcat安装及eclipse配置参见:https://my.oschina.net/gwlCode/blog/2996915#h1_11

创建工程

配置Configurations

添加Tomcat

配置Tomcat

配置完成,控制台显示如下

启动服务,浏览器出现如下,则成功

显示内容为index.jsp文件内容

工程添加到Tomcat

设置Output directory路径,在Tomcat/webapps文件夹下拼接上存放工程的文件名

Servlet测试

导入servlet-api.jar

\

配置文件web.xml

Schema参见 https://my.oschina.net/gwlCode/blog/2995818

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://www.example.org/web-app_2_5"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://www.example.org/web-app_2_5 web-app_2_5.xsd"
         version="2.5">
    
    <servlet>
        <servlet-name>Test</servlet-name>
        <servlet-class>Test</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Test</servlet-name>
        <url-pattern>/abc</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file></welcome-file>
    </welcome-file-list>
</web-app>

Test

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

public class Test implements Servlet {
    @Override
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {

        HttpServletResponse response = (HttpServletResponse) servletResponse;
        response.getWriter().write("My First IDEA Server");
    }

    @Override
    public void init(ServletConfig servletConfig) throws ServletException {

    }

    @Override
    public ServletConfig getServletConfig() {
        return null;
    }


    @Override
    public String getServletInfo() {
        return null;
    }

    @Override
    public void destroy() {

    }
}

运行服务或在浏览器输入 http://localhost:8080/untitled_war_exploded/abc,显示如下则成功

 

 

本文由【gwl_】发布于开源中国,原文链接:https://my.oschina.net/gwlCode/blog/2998680

全部评论: 0

    我有话说: