一、项目结构

二、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简单例子的更多相关文章

  1. servlet简单例子1

    servlet简单例子1 分类: servlet jsp xml2012-04-18 21:54 3646人阅读 评论(3) 收藏 举报 servletloginjspaction浏览器 LoginS ...

  2. spring mvc(注解)上传文件的简单例子

    spring mvc(注解)上传文件的简单例子,这有几个需要注意的地方1.form的enctype=”multipart/form-data” 这个是上传文件必须的2.applicationConte ...

  3. java 使用 comet4j 主动向客户端推送信息 简单例子

    [背景] 今天,一个前端的师弟问我怎样做实时聊天窗口,我毫不犹豫地说:在前台定时访问服务端呀!师弟默默地百度了一番,最后告诉我,有一种技术是后服务端动推送信息给客户端的,这种技术的名字叫comet,我 ...

  4. 使用 CXF 做 webservice 简单例子(转载)

    使用 CXF 做 webservice 简单例子     Apache CXF 是一个开放源代码框架,提供了用于方便地构建和开发 Web 服务的可靠基础架构.它允许创建高性能和可扩展的服务,您可以将这 ...

  5. SpringMvc(注解)上传文件的简单例子

    spring mvc(注解)上传文件的简单例子,这有几个需要注意的地方1.form的enctype=”multipart/form-data” 这个是上传文件必须的2.applicationConte ...

  6. Hibernate4.2.4入门(一)——环境搭建和简单例子

    一.前言 发下牢骚,这段时间要做项目,又要学框架,搞得都没时间写笔记,但是觉得这知识学过还是要记录下.进入主题了 1.1.Hibernate简介 什么是Hibernate?Hibernate有什么用? ...

  7. AgileEAS.NET SOA 中间件平台.Net Socket通信框架-简单例子-实现简单的服务端客户端消息应答

    一.AgileEAS.NET SOA中间件Socket/Tcp框架介绍 在文章AgileEAS.NET SOA 中间件平台Socket/Tcp通信框架介绍一文之中我们对AgileEAS.NET SOA ...

  8. ko 简单例子

    Knockout是在下面三个核心功能是建立起来的: 监控属性(Observables)和依赖跟踪(Dependency tracking) 声明式绑定(Declarative bindings) 模板 ...

  9. mysql定时任务简单例子

    mysql定时任务简单例子 ? 1 2 3 4 5 6 7 8 9     如果要每30秒执行以下语句:   [sql] update userinfo set endtime = now() WHE ...

随机推荐

  1. 【luoguP2158】 [SDOI2008]仪仗队

    题目描述 作为体育委员,C君负责这次运动会仪仗队的训练.仪仗队是由学生组成的N * N的方阵,为了保证队伍在行进中整齐划一,C君会跟在仪仗队的左后方,根据其视线所及的学生人数来判断队伍是否整齐(如下图 ...

  2. [bat]只更新svn部分文件夹

    游戏工程里的sdk文件夹,经常被svn认定为有毒文件. 后来关了权限之后,已拉取过的sdk文件夹还是会拉下来. 网上找了个方法,bat文件只更新部分文件. TortoiseProc /command: ...

  3. python3版 爬虫了解

    摘要:本文将使用Python3.4爬网页.爬图片.自动登录.并对HTTP协议做了一个简单的介绍.在进行爬虫之前,先简单来进行一个HTTP协议的讲解,这样下面再来进行爬虫就是理解更加清楚. 一.HTTP ...

  4. JVM GC之垃圾收集器

    简述 如果说收集算法时内存回收的方法论,那么垃圾收集器就是内存回收的具体实现.这里我们讨论的垃圾收集器是基于JKD1.7之后的Hotspot虚拟机,这个虚拟机包含的所有收集器如图: Serial 收集 ...

  5. 修改tomcat控制台的标题

    Tomcat的bin目录下,创建一个名为setenv.bat的文件. setenv.bat  编辑内容 : set TITLE = 想要命名的标题名称  保存修改.重新启动. 第二种. 修改tomca ...

  6. Linux系统下关闭与启动Oracle11g的顺序与命令

    关闭: 1.关EM:[oracle@localhost ~] emctl stop dbconsole 2.关监听:[oracle@localhost ~] lsnrctl stop 3.关数据库:S ...

  7. python 字符串(str)和列表(list)的互相转换

    1.str to list  str1 = "12345"list1 = list(str1)print list1 str2 = "123 sjhid dhi" ...

  8. linux中安装python

    1.首先切换目录 大型的软件一定要安装在/ opt中  规范 cd /opt 2.下载python3的源码 wget https://www.python.org/ftp/python/3.6.2/P ...

  9. LC 670. Maximum Swap

    Given a non-negative integer, you could swap two digits at most once to get the maximum valued numbe ...

  10. Python AES加密

    使用pycrypto模块https://pypi.python.org/pypi/pycrypto/ >>> from Crypto.Cipher import AES>> ...