使用Intellij IDEA新建Web项目

使用Intellij IDEA新建Web项目,第1张

使用Intellij IDEA新建Web项目 在学习Servlet的过程中,发现大多数的教程都是使用MyEclipse或者Eclipse来创建Web项目,这让一直使用高逼格的LZ很是不爽,于是自己配置了一下使用Intellij IDEA新建了Web项目。


LZ这里使用的Intellij IDEA 2017.1.4版本。


我们先来看看这个版本的IDEA骚气的启动界面吧~~

1.启动了IDEA后,在开始界面处点击Create New Project

2.选择Java——>勾选Web Application然后点击Next,在进行这些 *** 作时请确保你的Project SDK正确

3.根据自己要求设置Project name并选择Project location

4.建好工程后在WEB-INF中创建名为classes和lib的两个文件夹

5.然后点击File——>Project Structure——>Modules——>Paths,修改Output path和Test output path的路径,如下图所示:

6.在IDE的工具栏处的如下图的地方点击Edit Configurations

7.上述 *** 作完成后点击左上角的“+”号,往下翻,找到Tomcat Server,选择Tomcat Server——>Local

8.自定义设置Name,然后点击Deployment—>+—>Artifact…,设置Application context,建议和你建的project名字一样

9.在上述页面中点击Server到Server页面下,按照下图进行配置,你的Tomcat版本可以不必和我相同

10.选择File——>Project Structure——>Modules——>Dependencies——>+——>Libraries,选择Application Server Libraries下的Tomcat 9.0.1,点击Add Selected,这样就可以导入jsp和servlet的jar包了

11.新建一个Servlet,然后点击绿色按钮开启服务器

具体步骤: 
在src文件夹下新建名为“cn.jxs.servlet”package,并在package下新建名为HelloServlet.java的文件 

HelloServlet源代码如下:

package cn.jxs.servlet;

import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.IOException;
import java.io.OutputStream; /**
* Created by jiangxs on 2017/10/23.
*/
public class HelloServlet extends GenericServlet{ @Override
public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
OutputStream out = servletResponse.getOutputStream();
out.write("Hello Servlet!! \n--by Intellij IDEA".getBytes());
}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

在WEB-INF下更改web.xml文件: 

web.xml源代码如下:

<?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_3_1.xsd"
version="3.1"> <servlet>
<servlet-name>HelloServlet</servlet-name>
<servlet-class>cn.jxs.servlet.HelloServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet> <servlet-mapping>
<servlet-name>HelloServlet</servlet-name>
<url-pattern>/aa</url-pattern>
</servlet-mapping> </web-app>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

然后点击绿色按钮开启服务器: 

开启成功如下所示: 

浏览器d出: 

然后输入之前web.xml配置的servlet的路径http://localhost:8080/servletdemo/aa即可访问: 

好了,关于Intellij IDEA新建Web项目的步骤到此结束,希望能帮到大家,Enjoy Coding!

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/587001.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-12
下一篇 2022-04-12

发表评论

登录后才能评论

评论列表(0条)

保存