当前位置: 首页 > news >正文

hello_servlet

环境:java 1.8

0.创建web项目

mvn archetype:generate \-DgroupId=com.yawei \-DartifactId=hello_servlet \-Dversion=1.0-SNAPSHOT \-DarchetypeGroupId=org.apache.maven.archetypes \-DarchetypeArtifactId=maven-archetype-quickstart \-DarchetypeVersion=1.4 \-DinteractiveMode=false

1.添加依赖 pom.xml

<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><packaging>pom</packaging><groupId>com.yawei</groupId><artifactId>tmp</artifactId><version>1.0-SNAPSHOT</version><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.13.2</version><scope>test</scope></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>5.3.25</version></dependency><dependency><groupId>javax.servlet</groupId><artifactId>servlet-api</artifactId><version>2.5</version></dependency><dependency><groupId>javax.servlet</groupId><artifactId>jsp-api</artifactId><version>2.0</version></dependency><dependency><groupId>javax.servlet</groupId><artifactId>jstl</artifactId><version>1.2</version></dependency></dependencies>
</project>

2.HelleServlet

package com.yawei.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 HelleServlet extends HttpServlet {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/text.jsp").forward(req, resp);}protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doGet(req, resp);}
}

3.text.jsp

<%--Created by IntelliJ IDEA.User: zhangyaweiDate: 2025/9/14Time: 21:11To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>Title</title>
</head>
<body>${msg}</body>
</html>

4.web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"version="4.0"><servlet><servlet-name>hello</servlet-name><servlet-class>com.yawei.servlet.HelleServlet</servlet-class></servlet><servlet-mapping><servlet-name>hello</servlet-name><url-pattern>/hello</url-pattern></servlet-mapping><session-config><session-timeout>15</session-timeout></session-config><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list></web-app>
  1. from.jsp
<%--Created by IntelliJ IDEA.User: zhangyaweiDate: 2025/9/14Time: 21:17To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>Title</title>
</head>
<body><form action="/hello" method="post"><input type="text" name="method"><input type="submit">
</form>
</body>
</html>
  1. index.jsp
<%--Created by IntelliJ IDEA.User: zhangyaweiDate: 2025/9/14Time: 16:13To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html><head><title>$Title$</title></head><body>$END$</body>
</html>

7.目录
在这里插入图片描述
8.测试:
http://localhost:8080/tmp_war_exploded/hello?method=add

http://www.dtcms.com/a/462339.html

相关文章:

  • 易拉罐和瓶子分类数据集 6059张图片,支持yolo,coco json,paschal voc xml格式,识别率可达92.5%,文末提供下载地址
  • 1 玩转Linux命令行:基础文件操作实战教程
  • 图观 流渲染打包服务器
  • 为什么只有中国做网站需要备案建站技术博客
  • 最好的微网站建设价格搜索引擎排名国内
  • 自动化测试中元素定位失败的解决策略
  • 心痛之窗:滑动窗口算法解爱与愁的心痛(洛谷P1614)
  • 鸿蒙HTTP请求老是发不出去?一文带你彻底排查所有坑!
  • c# 开发网站开发做市级网站需要什么
  • 购物网站建设的必要性电商设计年终总结
  • 元宵节html+css+js 5页
  • 基于单片机的温度烟雾与漏电综合火灾报警系统设计
  • NX565NX578美光SSD固态闪存NX579NX580
  • 永久解决ubuntu网络连接问题
  • Linux timeout 命令详解:精准控制程序运行时长,避免资源浪费
  • 免费php域名网站专业品牌网站设计公司
  • 选择海外代理IP的方法
  • C语言中整数与浮点数的内存存储详解
  • 旅游建设投资公司网站建设局的全称
  • 网站建设的公司哪家强前端开发工程师是什么专业
  • 手机上自己如何做网站做视频网站用什么语言
  • 【图像处理基石】GIS图像处理入门:4个核心算法与Python实现(附完整代码)
  • 青岛+网站建设wordpress制作百度地图xml
  • 建站多语言方案
  • 网站开发时间计划智慧旅游网站开发与设计
  • 生成与无监督学习 —— 奶茶店的 “新品研发与原料优化体系”
  • 网站开发第三方支付用什么工具建设网站
  • [xboard] 27kernel内核中的kconfig工作原理及完整示例
  • Arduino 与 Raspberry Pi 的区别
  • 仿门户网站多功能js相册画廊源码divi wordpress