1. 第一步:打开idea工具,创建一个新的工程;
2. 第二步:file-->new -->project
3. 在选项卡中选择web工程基本的选项-->Next,然后给工程命名为webDemo,最后点击Finish,完美收工。
4. 然后右键项目,选择add Framework Support...,在选项卡中选择maven,使工程中的jar包可以通过maven来统一管理。
5. Pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>groupId</groupId>
<artifactId>webDemo</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
</dependency>
</dependencies>
</project>
6. 书写controller文件
package com.wql;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* java类简单作用描述
*
* @ProjectName: webDemo
* @Package: com.wql
* @ClassName: Controller
* @Description: java类作用描述
* @Author: liupeng
* @CreateDate: 2018/3/1/001 15:28
* @UpdateRemark: The modified content
* @Version: 1.0
*/
public class Controller extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("UTF-8");
String name = req.getParameter("name");
req.setAttribute("name", name);
System.out.println("-sss-");
req.getRequestDispatcher("index.jsp").forward(req, resp);
}
}
7. 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">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
8. Index.jsp文件
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>index</title>
</head>
<body>
<form action="mytest" method="post">
<input name="name">
return:${name}
<input value="提交" type="submit">
</form>
</body>
</html>
9. 附加:目录结构如下
10、将项目部署到tomcat中
11、启动tomcat,在浏览器中访问http://localhost:8080地址得到返回结果页面