首先声明,配置tomcat项目可能有多种方法,但是却特别容易出错,下面的是我常用的配置流程,在这里推给大家,当然如果你有自己的一套无误的配置方法也是可以的。

一、 打开Project Stucture -> Project Setting -> project

配置jdk

二、 Modules

添加web模块

三、 Artifacts

  1. 先添加Web Application:Exploded

  1. 再添加Web Application:Archive

Exploded和Archive的区别:

web application exploded: 这个是以文件夹形式(War Exploded)发布项目,选择这个,发布项目时就会自动生成文件夹在指定的output directory

web application archive:就是war包形式,每次都会重新将全部的项目打包成一个war包

四、 配置tomcat

  1. 点击Add Configuration

  1. 点击加号 -> Tomcat Server -> Local

  1. 配置tomcat的位置(你解压在电脑里的路径)

  1. 配置Deployment

若想要改变请求的URL,需要先配置Deployment下的Application context

这里,将Application context/shop_archive 改为/shop,则URL变为http://localhost:8080/shop

五、导入JAR包

  • 首先在群里下载jar包
  • 之后进入File -> Project Structure

六、编写一个简单的servlet类

  • 创建一个名为HelloServlet的普通java类

  • 并令其继承自HttpServlet

  • Ctrl+O重写两个方法 doGetdoPost

    • GET请求会进入doGet方法,浏览器默认Get方法
    • Post请求会进入doPost方法
  • 在类上方添加一个注解@WebServlet("/hello"),表示我们可以通过/hello这个路径来访问这个Servlet

    若前面你配置的URL为http://localhost:8080/shop/,那么你只需要将/hello添加到其后变为

    http://localhost:8080/shop/hello即可访问

样例:

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/hello")  
public class HelloServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        System.out.println("success!!");

        PrintWriter out = null;
        try {
            out = resp.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }

        if (out != null) {
            out.write("HELLO!!SUCCESS!!");  //将信息传给浏览器
            out.flush();
            out.close();  //关闭输出流
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}

七、 启动你的项目吧!

  • 选中tomcat,并点击绿色三角运行

  • 若下方services模块若下图所示,那么启动成功

  • 之后通过浏览器访问网址http://localhost:8080/shop/hello

success!!

八、 接下来,带参数的请求

参数在URL中的表现形式就是http://localhost:8080/shop/hello?name=tom&age=14

问号?之后的就是参数,其中各个参数用&连接

注意是英文字符

这里我们有两个参数 name 和 age

  • 接下来,我们需要在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;
import java.io.PrintWriter;

@WebServlet("/hello")
public class HelloServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("application/json;charset=utf-8");  //设置request和response的编码方式为UTF-8,防止可能的乱码

        System.out.println("success!!");

        //这里最重要,接收参数的方法
        String name = req.getParameter("name");  //这里获取request里的请求参数name
        String age = req.getParameter("age");  //这里获取request里的请求参数age

        if (name == null || age == null) {  //如果参数为空,直接终止方法
            System.out.println("没有name参数或者没有age参数");
            return;
        }

        PrintWriter out = null;
        try {
            out = resp.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }

        if (out != null) {
            out.write("HELLO!!SUCCESS!!");  //将信息传给浏览器
            out.write("名字是: " + name + "    年龄是: " + age);
            out.flush();
            out.close();  //关闭输出流
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}
  • 访问网址http://localhost:8080/shop/hello?name=sky&age=16,可以得到下面的结果,那么你就成功了


hhhhh