idea 开发 java 项目非常方便,但是收费版需要各种破解还不稳定,社区版其实用起来也不错,基本功能虽然少,但是通过各种插件可以实现各种功能,比如最近复习 Servlet 开发,就需要“Smart Tomcat”插件。
另外缺乏各种模板是比较影响初期使用;不过一番折腾,习惯后使用还是很方便的。
idea 社区版默认不支持 java web 项目模板,手动创建也很方便,基于 java 项目或者 maven 项目稍微修改即可。
1、运行环境
JDK1.8、 idea 社区版、 tomcat 9 这些安装配置就不用说了,基本依赖环境。
2、创建项目
创建 maven 项目,这个为以后添加各种依赖类库提供方便;
3、环境支持
java web 项目需要依赖 servlet-api.jar,可以通过 maven 引入,或者直接引用 jar 包,在这强烈推荐使用 jar 包;原因是在开发 javaweb 项目时需要写各种 xml 文件,而 idea 对 xml 文件的自动提示需要 xsd 文件,maven 引用很奇怪,没有,所以折腾了好久。jar 包在 tomcat 9 的 lib 目录下就有。
4、创建 java web 项目文件目录:
java web 项目目录结构有指定的"WEB-INF"目录,以及 "web.xml"文件,可以参考 tomcat webapp 目录下的项目的目录结构,如下图:
编写 servlet:
编写 servlet:
package org.example.servlet;
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 HelloServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html");
resp.getWriter().println("Hello servlet!");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
配置 web.xml,文档的结构如果没有自动提示还是去 tomcat 的 webapp 下的项目中找,然后为了以后方便可以添加 idea 的 setting > Editor > Lite Templates > xml 下添加提示模板。
配置 servlet 路径以及 servlet mapping:
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<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"
metadata-complete="true">
<display-name>Servlet Demo</display-name>
<description>
Welcome to Tomcat
</description>
<servlet>
<servlet-name>HelloServlet</servlet-name>
<servlet-class>org.example.servlet.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloServlet</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
</web-app>
5、运行项目
安装插件“Smart Tomcat”插件,并配置 tomcat 路径:
添加启动项就可以运行了:
注意配置项:Deployment Directory、server Port、context path:
项目就可以访问servlet服务了:
注意最后访问路劲:ip + {server Port} + {Context path} + {servlet mapping}。
总结:
一路折腾,第一次使用确实有点麻烦,不过也锻炼人不是;当让土豪还是建议买收费版的吧,不建议破解哈,各种问题折腾。