Servlet笔记2--模拟Servlet本质、第一个Servlet程序、将响应结果输出到浏览器中
以下代码均非IDE开发,所以都不规范,仅供参考
模拟Servlet本质:
模拟Servlet接口:
/*
SUN公司制定的JavaEE规范:Servlet规范
Servlet接口是Servlet规范中核心接口
接口注意:调用者谁?实现者谁?
*/ public interface Servlet //服务器端小java程序不能随意编写,必须实现Servlet接口
{
/*
服务器端的小java程序必须将service方法实现
*/
void service();
}
服务器端小java程序(即Servlet接口的实现类):
/*
JavaWeb程序员编写服务器端小java程序的时候,不能随意编写,必须实现Servlert接口
*/
public class LoginServlet implements Servlet
{
public void service(){
System.out.println("连接数据库,正在验证用户名和密码。。。。");
}
}
/*
JavaWeb程序员编写服务器端小java程序的时候,不能随意编写,必须实现Servlert接口
*/
public class SaveServlet implements Servlet
{
public void service(){
System.out.println("连接数据库,正在保存数据请稍后。。。。");
}
}
/*
JavaWeb程序员编写服务器端小java程序的时候,不能随意编写,必须实现Servlert接口
*/
public class DeleteServlet implements Servlet
{
public void service(){
System.out.println("连接数据库,正在删除数据请稍后。。。。");
}
}
模拟Tomcat容器:
/*
Tomcat
WebServer
Web服务器
Web Container
Web 容器 Web容器面向Servlert接口调用
*/
import java.util.Scanner;
import java.util.Properties;
import java.io.FileReader; public class Tomcat{
public static void main(String[] args) throws Exception{
Scanner s = new Scanner(System.in);
System.out.println("服务器启动成功"); while(true){
System.out.print("请打开浏览器,在浏览器地址栏上请输入请求路径:");
//程序执行到这里,等待用户输入
String requestPath = s.next();
//System.out.println("您访问的资源路径是:" + requestPath); //Tomcat读取web.xml文件
FileReader reader = new FileReader("web.xml");
Properties pro = new Properties();
pro.load(reader);
reader.close(); //通过key获取value
String servletClassName = pro.getProperty(requestPath); //通过反射机制创建对象
Class c = Class.forName(servletClassName);
Servlet servlet = (Servlet)c.newInstance(); //面向Servlet接口调用方法即可
servlet.service();
}
}
}
模拟web.xml:
/login=LoginServlet
/delete=DeleteServlet
/save=SaveServlet
总结:Tomcat容器根据web.xml配置文件,调用Servlet接口的方法,自己编写的服务器小程序则具体实现Servlet接口的方法。
第一个Servlet程序:
Servlet程序:
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.ServletException;
import java.io.IOException; public class HelloServlet implements Servlet{ public void init(ServletConfig config) throws ServletException{
//将信息打印到控制台(而不是浏览器)
System.out.println("Hello world!");
} public void service(ServletRequest request,ServletResponse response)
throws IOException,ServletException{ } public void destroy(){ } public String getServletInfo(){
return null;
} public ServletConfig getServletConfig(){
return null;
} }
前端页面:
<html>
<head>
<title>welcome page</title>
</head>
<body>
<!--为了更加的通用,URL路径中的IP地址,端口号可以省略-->
<!--以下网络资源路径还是一个绝对路径,目前必须以"/"开始-->
<a href="/FirstServletWebApp/hello">HelloServlet</a>
</body>
</html>
web.xml:
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<!--这是一个合法的web.xml文件-->
<!--一个webapp只有一个web.xml文件-->
<!--web.xml文件主要配置请求路径和Servlet类名之间的绑定关系-->
<!--web.xml文件在Tomcat服务器启动阶段被解析-->
<!--web.xml文件解析失败,会导致webapp启动失败-->
<!--web.xml文件中的标签不能随意编写,因为Tomcat服务器早就知道文件中编写了哪些标签-->
<!--web.xml文件中的标签也是SUN公司制定的Servlet规范-->
<servlet>
<servlet-name>thisIsServletName</servlet-name>
<servlet-class>HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>thisIsServletName</servlet-name>
<!--路径随意编写,但是必须以"/"开始-->
<!--这是一个虚拟路径,只是代表一个资源的名称-->
<url-pattern>/df/s/fs/f/sdf/s/f</url-pattern>
<!--可以编写多个url-pattern,但是路径需要以"/"开始-->
<url-pattern>/hello</url-pattern>
</servlet-mapping> </web-app>
将响应结果输出到浏览器中:
Servlet程序:
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.IOException;
import java.io.PrintWriter; //标准输出流,不需要关闭 public class WelcomeServlet implements Servlet{
public void init(ServletConfig config) throws ServletException{ } public void service(ServletRequest request,ServletResponse response)
throws ServletException,IOException{
//解决响应的时候中文乱码问题
//设置响应的内容类型以及字符编码方式
//在获取响应流之前设置有效果
response.setContentType("text/html;charset=UTF-8"); //将信息输出到浏览器上
//将HTML字符串输出到浏览器上,浏览器解释执行
//获取输出流对象,流直接指向特定的浏览器客户端
PrintWriter out = response.getWriter(); //响应HTML代码到浏览器,并且在网页上体现换行
out.print("<html>");
out.print("<head>");
out.print("<title>welcome servlet</title>");
out.print("</head>");
out.print("<body>");
out.print("<h1 align=\"center\">welcome study servlet!</h1>");
out.print("hello");
out.print("<br>");
out.print("你好中国!");
out.print("</body>");
out.print("</html>");
} public void destroy(){ } public String getServletInfo(){
return null;
} public ServletConfig getServletConfig(){
return null;
}
}
web.xml:
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0"> <servlet>
<servlet-name>helloServlet</servlet-name>
<servlet-class>WelcomeServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>helloServlet</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping> </web-app>
Servlet笔记2--模拟Servlet本质、第一个Servlet程序、将响应结果输出到浏览器中的更多相关文章
- Servlet+JSP(三):第一个Web程序
Servlet+JSP(三):第一个Web程序在学习了服务器并成功安装后,我们知道当浏览器发送请求给服务器后,服务器会调用并执行对应的逻辑代码进行请求处理.逻辑代 码是由程序员自己编写然后放进服务器进 ...
- Java连接MySQL数据库。编写一个应用程序,在主类Test_4类中,通过JDBC访问stu数据库,显示t_student表中的内容(表结构见表1),显示效果自己设计。
题目2:编写一个应用程序,在主类Test_4类中,通过JDBC访问stu数据库,显示t_student表中的内容(表结构见表1),显示效果自己设计.之后,可根据显示的内容进行某条记录的删除(以id为条 ...
- 编写一个应用程序,在主类Test1类中,创建两个链表List<E>对象,分别存储通过键盘输入的字符串内容
题目1:编写一个应用程序,在主类Test1类中,创建两个链表List<E>对象,分别存储通过键盘输入的字符串内容--"chen","wang",&q ...
- 【Spring学习笔记-2】Myeclipse下第一个Spring程序-通过ClassPathXmlApplicationContext加载配置文件
*.hl_mark_KMSmartTagPinkImg{background-color:#ffaaff;}*.hl_mark_KMSmartTagBlueImg{background-color:# ...
- Java笔记2:Eclipse编写第一个Java程序
1 下载并安装jdk 2 下载较新版本的eclipse,eclipse都是非安装版的,解压缩即可. 3 双击eclipse.exe,打开elipse软件 4 FileàNewàProject 5 选择 ...
- 在cmd启动一个win32程序,printf把信息输出到启运它的那个CMD窗口
#define ProcessBasicInformation 0 typedef struct { DWORD ExitStatus; DWORD PebBaseAddress; DWORD Aff ...
- [原创]java WEB学习笔记04:Servlet 简介及第一个Servlet程序(配置注册servlet,生命周期)
本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...
- 对于Servlet、Servlet容器以及一个Servlet容器-Tomcat
Servlet.Servlet容器等内容讲解 转载自http://blog.csdn.net/iAm333 对于Servlet.Servlet容器以及一个Servlet容器-Tomcat这些概念讲解的 ...
- Servlet入门总结及第一个Servlet程序
目录 一了解Servlet的概念 二Servlet技术功能 三 Servlet技术特点 四 Servlet生命周期 五servlet工作过程 六 Servlet与JSP区别 七Servlet代码结构 ...
随机推荐
- Window系统 安装TFLearn
1. 确保成功安装了tensorflow 2. 查看当前tensorflow下的库文件,判断是否已经安装了h5py,scipy:conda list 3. 若没有安装,安装h5py,scipy.我的电 ...
- CF992C Nastya and a Wardrobe
我是题面 题意很清晰,这种题,我们当然还是有两种方法来做啦 方法一:找规律 读完题我们来看样例,通过样例一已我们大概可以看出,答案或许是\(n*2^{k+1}\) 肯定不能这么简单对吧,那就来看样例二 ...
- MySQL总结小妙招
mysql5.7版本,免登陆修改管理员密码: vim /etc/my.cnf 加入skip-grant-tables,重启MySQL 终端输入 mysql ,直接登录MySQL数据库,然后use my ...
- Luogu1309 瑞士轮(分治,归并排序)
Luogu1309 瑞士轮(分治,归并排序) Description 在双人对决的竞技性比赛,如乒乓球.羽毛球.国际象棋中,最常见的赛制是淘汰赛和循环赛.前者的特点是比赛场数少,每场都紧张刺激,但偶然 ...
- UESTC--1548
题目:Easy math 原题链接:http://acm.uestc.edu.cn/problem.php?pid=1548 分析:费马小定理的应用. #include<cstdio> # ...
- laravel 实用扩展包
1.beyondcode / laravel-self-diagnosis 环境检测.检测 php 版本.扩展 是否正常,数据库连接是否正常等 2.nunomaduro/larastan larave ...
- golang json 编码解码
json 编码 package main import ( "encoding/json" "fmt" ) type Person struct { Name ...
- ElasticStack系列之八 & _source 字段
有很多人会有这样的一个疑问: _source字段存储的是索引的原始内容,那 store 属性的设置是为何呢?elasticsearch 为什么要把 store 的默认取值设置为 no?设置为 yes ...
- CF916E Jamie and Tree
CF916E Jamie and Tree 题意翻译 有一棵n个节点的有根树,标号为1-n,你需要维护以下三种操作 1.给定一个点v,将整颗树的根变为v 2.给定两个点u, v,将lca(u, v)所 ...
- IOS取消performSelector警告
#pragma clang diagnostic push #pragma clang diagnostic ignored "-Warc-performSelector-leaks&quo ...