sprigmvc前奏-Servlet之idea学习

首先建立一个简单的meaven项目

导入依赖


    
        junit
        junit
        4.12
    
    
        org.springframework
        spring-webmvc
        5.1.9.RELEASE
    
    
        javax.servlet
        servlet-api
        2.5
    
    
        javax.servlet.jsp
        jsp-api
        2.2
    
    
        javax.servlet
        jstl
        1.2
    

建立一个Moudle:springmvc-01-servlet , 添加Web app的支持!

sprigmvc前奏-Servlet之idea学习_第1张图片

在子项目中继续导入依赖(可以省略)


    javax.servlet
    servlet-api
    2.5


    javax.servlet.jsp
    jsp-api
    2.2

编写一个Servlet类,用来处理用户的请求

package com.kuang.servlet;

//实现Servlet接口
@WebServlet('/user')
public class HelloServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //取得参数
        String method = req.getParameter("method");
        if (method.equals("add")){
            req.getSession().setAttribute("msg","执行了add方法");
        }
        if (method.equals("delete")){
            req.getSession().setAttribute("msg","执行了delete方法");
        }
        //业务逻辑
        //视图跳转
        req.getRequestDispatcher("/WEB-INF/jsp/hello.jsp").forward(req,resp);
    }

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

编写Hello.jsp,在WEB-INF目录下新建一个jsp的文件夹,新建hello.jsp

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

你可能感兴趣的:(ssm,servlet,jsp,java,spring)