记录使用IDEA 2020.2 创建Servlet项目

创建maven项目



创建项目,如下:

导入依赖

<dependencies>

        <!--jsp-->
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.2</version>
        </dependency>

        <!--servlet-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.1</version>
        </dependency>

        <!--使用EL表达式-->
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>

    </dependencies>

创建Servlet类处理信息

重载doGet和doPost方法
为这个Servlet类在web.xml文件中注册

package com.dj;

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;

public class demo1 extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1、读取视图传来的参数
        System.out.println("访问页面--------");
        String parameter = req.getParameter("method");
        //2、业务处理
        if (parameter.equals("add")) {
            req.getSession().setAttribute("msg", "执行了add方法");
        }
        if (parameter.equals("delete")) {
            req.getSession().setAttribute("msg", "执行了delete方法");
        } else {
            req.getSession().setAttribute("msg", "未找到执行方法");
        }
        //跳转
        req.getRequestDispatcher("/WEB-INF/jsp/helloServlet.jsp").forward(req, resp);
    }

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

对web.xml中添加下面的代码
在下面的代码中设置了欢迎页面和超时时长(可以都不要)

 <servlet>
        <servlet-name>hello</servlet-name>
        <servlet-class>com.zhl.demo01</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>hello</servlet-name>
        <url-pattern>/hello</url-pattern>
    </servlet-mapping>

<!--    超时时长-->
    <session-config>
        <session-timeout>5</session-timeout>
    </session-config>

<!--    欢迎页面-->
    <welcome-file-list>
        <welcome-file>/WEB-INF/jsp/welcome.jsp</welcome-file>
    </welcome-file-list>

jsp文件的创建


helloServlet.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
${msg}
</body>
</html>

welcome.jsp

<%--
  Created by IntelliJ IDEA.
  User: zycx.vm148
  Date: 2021/7/26
  Time: 14:06
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h1>欢迎访问页面</h1>
</body>
</html>

配置Tomcat


解决tomcat中文乱码

运行代码

参考链接:https://blog.csdn.net/qq_26558047/article/details/112968630

posted @ 2021-07-26 15:30  离人怎挽_wdj  阅读(118)  评论(0编辑  收藏  举报