sprigmvc前奏-Servlet之idea学习
程序员文章站
2022-04-11 12:59:47
...
首先建立一个简单的meaven项目
导入依赖
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
建立一个Moudle:springmvc-01-servlet , 添加Web app的支持!
在子项目中继续导入依赖(可以省略)
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
</dependency>
编写一个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>
推荐阅读
-
JSP学习之Servlet用法分析
-
Servlet3.0学习总结之基于Servlet3.0的文件上传实例
-
JSP Servlet基础学习之JSP脚本中的9个内置对象
-
Jsp/Servlet 学习笔记之Tag JSPServletIBMJavaJ#
-
JSP学习之Servlet用法分析
-
J2EE学习笔记(五)之Servlet中的Filter过滤器
-
JSP Servlet基础学习之JSP脚本中的9个内置对象
-
Spring boot学习 (七) 之servlet、filter(过滤器)、listener(监听器)的学习
-
Servlet 知识详解(五)之 —— Listener对象 监听器 学习笔记
-
Servlet 知识详解(一)之 —— ServletContext对象 和 ServletConfig对象 学习笔记