栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringMvc之回顾Servlet

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringMvc之回顾Servlet

首先创建完项目把需要的Jar包进行导入

      junit
      junit
      4.13.2
      test
    

    
      org.springframework
      spring-webmvc
      5.1.9.RELEASE
    

    
      javax.servlet
      servlet-api
      2.5
    

    
      javax.servlet.jsp
      jsp-api
      2.2.1-b03
    

    
      javax.servlet
      jstl
      1.2
    

  
创建一个Servlet用来接受前端消息
package com.lxy.servlet;

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

public class HellowServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取前端数据
        String msg = req.getParameter("message");
        if ("add".equals(msg)){
            req.getSession().setAttribute("msg","执行了add方法");
        }
        if ("delete".equals(msg)){
            req.getSession().setAttribute("msg","执行了delete方法");
        }
        //调用业务层

        //视图转发或重定向
        req.getRequestDispatcher("/WEB-INF/jsp/test.jsp").forward(req,resp);

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
写完Servlet就要立马去web.xml里面配置Servlet



    
        hello
        com.lxy.servlet.HellowServlet
    
    
        hello
        /hello
    


最后配置Tomcat运行

url填写配置的Servlet地址和参数数值 

 

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1021617.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号