第一:IntelliJ IDEA 2018.2.1
第二:JDK 1.8.0_77
第三:Tomcat 8.0.35
第一种: 通过Java工程创建,这个方法需要手动导入Tomcat的servlet包才能使用servlet(本案例使用这个方法)
第二种:通过Java Enterprise创建,这个方法会自动导入Tomcat的servlet包
在web/WEB-INF目录下新建两个文件夹lib(第三方jar包)和classes(编译后输出的class文件)
查看配置完毕:
(1)Project:无需设置 (也可点击Project complier output自定义编译目录)
(2)Modules:Sources
(3)Modules:Paths
注意:将 “Output path” 和 “Test output path” 都改为之前创建的classes目录。
(4)Modules:Dependencies
注意1:通过Java工程创建,这个方法需要手动导入Tomcat的servlet依赖包才能使用servlet;
注意2:前提是Tomcat已经配置完毕,在添加Tomcat依赖
界面显示内容:index.jsp
<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2020/1/6
Time: 10:38
To change this template use File | Settings | File Templates.
--%>
<%@ page import="java.util.*" language="java" pageEncoding="UTF-8" %>
小窗口
Hello,This is a JavaWeb Project!
启动项目:
查看运行结果:
(1)Servlet配置,有两个方法,第一种:在web.xml中配置servlet
import java.io.IOException;
import java.io.PrintWriter;
import javax.jws.WebService;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class HTMLServlet extends HttpServlet {
public HTMLServlet() {
super();
}
public void destroy() {
super.destroy();
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setCharacterEncoding("UTF-8"); //设置响应的字符集格式为UTF-8
response.setContentType("text/html"); //设置响应正文的MIME类型
PrintWriter out = response.getWriter(); //返回一个PrintWriter对象,Servlet使用它来输出字符串形式的正文数据
//以下为输出的HTML正文数据
out.println("");
out.println("");
out.println(" 动态生成的HTML文档 ");
out.println(" ");
out.println(" ");
out.println(" 动态生成HTML文档 ");
out.println("
");
out.println(" ");
out.println("");
out.flush();
out.close();
}
public void init() throws ServletException {
}
}
注意:在web.xml中配置Servlet,就是把访问路径与一个Servlet绑定到一起,例如:
配置是把访问路径:“/hello”与“com.wms.HTMLServlet”绑定到一起。
:指定Servlet的名称为HTMLServlet;
:指定/hello访问路径,所以访问的Servlet名为hello。
和 通过 这个元素关联在一起了
HTMLServlet
com.wms.HTMLServlet
HTMLServlet
/hello
index.jsp
<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2020/1/6
Time: 10:38
To change this template use File | Settings | File Templates.
--%>
<%@ page import="java.util.*" language="java" pageEncoding="UTF-8" %>
小窗口
Hello,This is a JavaWeb Project!
点击提交,跳转到/hello路径
(3)Servlet配置,有两个方法,第二种:利用注解配置servlet
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("/myServlet")
public class MyFirstServlet 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);
req.getRequestDispatcher("index.jsp").forward(req, resp);
}
}
<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2020/1/6
Time: 10:38
To change this template use File | Settings | File Templates.
--%>
<%@ page import="java.util.*" language="java" pageEncoding="UTF-8" %>
小窗口
Hello,This is a JavaWeb Project!
index.jsp