Servlet简单例子
一、项目结构

二、index.jsp
<%@ page contentType="text/html; charset=utf-8" %>
<html>
<body>
<h2>Hello World!</h2><hr/>
<a href="aaa/bbb">Get方式请求</a><br/>
<!-- 会到web.xml中交给url-pattern为/aaa/bbb的Servlet来处理,根据get、post的提交方式来执行doGet()、doPost()方法-->
<form action="aaa/bbb" method="post">
<input type="submit" value="Post方式请求"/>
</form>
</body>
</html>
三、MyServlet.java
package com.yanguobin; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter; public class MyServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("处理Get()请求...");
//使servlet页面中文不会乱码,一定要放在getWriter()方法前面
resp.setContentType("text/html; charset=utf-8");
//添加上面这行才会解析html代码,显示Get()请求成功!的加粗模式,否则不会解析html代码,直接显示html标签
PrintWriter out = resp.getWriter();
out.println("<strong>Get()请求成功!</strong><br/>");
} @Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("处理Post()请求...");
//使servlet页面中文不会乱码,一定要放在getWriter()方法前面
resp.setContentType("text/html; charset=utf-8");
//添加上面这行才会解析html代码,显示Get()请求成功!的加粗模式,否则不会解析html代码,直接显示html标签
PrintWriter out = resp.getWriter();
out.println("<strong>Post()请求成功!</strong><br/>");
}
}
四、web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app>
<display-name>Archetype Created Web Application</display-name> <servlet>
<servlet-name>myservlet</servlet-name>
<servlet-class>com.yanguobin.MyServlet</servlet-class> <!-- 包名类名写全 -->
</servlet>
<servlet-mapping>
<servlet-name>myservlet</servlet-name>
<url-pattern>/aaa/bbb</url-pattern>
<!-- 第一个/表示项目的根目录,或者说使当前Web工程的根目录,不能省略 -->
<!-- myget/aaa应与前端页面中的请求地址一致,即访问地址 -->
</servlet-mapping>
</web-app>
五、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> <groupId>com.yanguobin</groupId>
<artifactId>servletDemo</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging> <name>servletDemo Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties> <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
</dependencies> <build>
<finalName>servletDemo</finalName>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
六、运行结果




Servlet简单例子的更多相关文章
- servlet简单例子1
servlet简单例子1 分类: servlet jsp xml2012-04-18 21:54 3646人阅读 评论(3) 收藏 举报 servletloginjspaction浏览器 LoginS ...
- spring mvc(注解)上传文件的简单例子
spring mvc(注解)上传文件的简单例子,这有几个需要注意的地方1.form的enctype=”multipart/form-data” 这个是上传文件必须的2.applicationConte ...
- java 使用 comet4j 主动向客户端推送信息 简单例子
[背景] 今天,一个前端的师弟问我怎样做实时聊天窗口,我毫不犹豫地说:在前台定时访问服务端呀!师弟默默地百度了一番,最后告诉我,有一种技术是后服务端动推送信息给客户端的,这种技术的名字叫comet,我 ...
- 使用 CXF 做 webservice 简单例子(转载)
使用 CXF 做 webservice 简单例子 Apache CXF 是一个开放源代码框架,提供了用于方便地构建和开发 Web 服务的可靠基础架构.它允许创建高性能和可扩展的服务,您可以将这 ...
- SpringMvc(注解)上传文件的简单例子
spring mvc(注解)上传文件的简单例子,这有几个需要注意的地方1.form的enctype=”multipart/form-data” 这个是上传文件必须的2.applicationConte ...
- Hibernate4.2.4入门(一)——环境搭建和简单例子
一.前言 发下牢骚,这段时间要做项目,又要学框架,搞得都没时间写笔记,但是觉得这知识学过还是要记录下.进入主题了 1.1.Hibernate简介 什么是Hibernate?Hibernate有什么用? ...
- AgileEAS.NET SOA 中间件平台.Net Socket通信框架-简单例子-实现简单的服务端客户端消息应答
一.AgileEAS.NET SOA中间件Socket/Tcp框架介绍 在文章AgileEAS.NET SOA 中间件平台Socket/Tcp通信框架介绍一文之中我们对AgileEAS.NET SOA ...
- ko 简单例子
Knockout是在下面三个核心功能是建立起来的: 监控属性(Observables)和依赖跟踪(Dependency tracking) 声明式绑定(Declarative bindings) 模板 ...
- mysql定时任务简单例子
mysql定时任务简单例子 ? 1 2 3 4 5 6 7 8 9 如果要每30秒执行以下语句: [sql] update userinfo set endtime = now() WHE ...
随机推荐
- 使用WinDbg内核调试[转]
Technorati 标签: windbg,内核调试 WINDOWS调试工具很强大,但是学习使用它们并不容易.特别对于驱动开发者使用的WinDbg和KD这两个内核调试器(CDB和NTSD是用户态调试器 ...
- [python之ipython] jupyter notebook在云端服务器上开启,本地访问
本地ssh到云端: ssh username@xxx.xxx.xxx.xxx -L127.0.0.1:7777:127.0.0.1:8888 把云端的8888端口映射到本地的7777端口 云端运行指令 ...
- neo4j︱与python结合的py2neo使用教程
—- 目前的几篇相关:—– neo4j︱图数据库基本概念.操作罗列与整理(一) neo4j︱Cypher 查询语言简单案例(二) neo4j︱Cypher完整案例csv导入.关系联通.高级查询(三) ...
- @Value和@PropertySource实现*.properties配置文件读取过程和实现原理
@Value和@PropertySource实现*.properties 配置文件读取过程和实现原理 1 配置使用步骤 (1)右击resource目录添加*.prooerties配置文件
- PySpider的安装
使用 Pip 安装,命令如下 pip install pyspider 命令执行完毕即可安装成功. 常见错误: Windows 下可能会出现这样的错误提示:Command "python s ...
- XXE_payload
<?php $xmlfile = file_get_contents('php://input'); $creds=simplexml_load_string($xmlfile); echo $ ...
- LeetCode 122. 买卖股票的最佳时机 II(Best Time to Buy and Sell Stock II)
题目描述 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. 设计一个算法来计算你所能获取的最大利润.你可以尽可能地完成更多的交易(多次买卖一支股票). 注意:你不能同时参与多笔交易(你 ...
- php中的<?= ?>和<?php ?>有什么区别么?
<? ?>是短标签<?php ?>是长标签在php的配置文件(php.ini)中有一个short_open_tag的值,开启以后可以使用PHP的短标签:<? ?>同 ...
- MySQL Cluster 集群部署
前言 此篇博客用以介绍 MySQL Cluster 集群部署方法 一.节点规划 序号 IP地址 节点名称 1 172.16.1.201 mysql-manage 2 172.16.1.202 mysq ...
- 数据分析 - matpltlib 模块
matplotlib 模块 引入模块 import matplotlib.pyplot as plt 设置图片大小 - figure 展示图片 - show 画图 - 实例化后指定类型画图 plot ...