zoukankan      html  css  js  c++  java
  • Servlet笔记2--模拟Servlet本质、第一个Servlet程序、将响应结果输出到浏览器中

    以下代码均非IDE开发,所以都不规范,仅供参考

    模拟Servlet本质:

      模拟Servlet接口:

     1 /*
     2     SUN公司制定的JavaEE规范:Servlet规范
     3     Servlet接口是Servlet规范中核心接口
     4     接口注意:调用者谁?实现者谁?
     5 */
     6 
     7 public interface Servlet //服务器端小java程序不能随意编写,必须实现Servlet接口
     8 {
     9     /*
    10         服务器端的小java程序必须将service方法实现
    11     */
    12     void service();
    13 }

      服务器端小java程序(即Servlet接口的实现类):

    1 /*
    2     JavaWeb程序员编写服务器端小java程序的时候,不能随意编写,必须实现Servlert接口
    3 */
    4 public class LoginServlet implements Servlet
    5 {
    6     public void service(){
    7         System.out.println("连接数据库,正在验证用户名和密码。。。。");
    8     }
    9 }
    1 /*
    2     JavaWeb程序员编写服务器端小java程序的时候,不能随意编写,必须实现Servlert接口
    3 */
    4 public class SaveServlet implements Servlet
    5 {
    6     public void service(){
    7         System.out.println("连接数据库,正在保存数据请稍后。。。。");
    8     }
    9 }
    1 /*
    2     JavaWeb程序员编写服务器端小java程序的时候,不能随意编写,必须实现Servlert接口
    3 */
    4 public class DeleteServlet implements Servlet
    5 {
    6     public void service(){
    7         System.out.println("连接数据库,正在删除数据请稍后。。。。");
    8     }
    9 }

      模拟Tomcat容器:

     1 /*
     2     Tomcat
     3         WebServer
     4         Web服务器
     5         Web Container
     6         Web 容器
     7 
     8     Web容器面向Servlert接口调用
     9 */
    10 import java.util.Scanner;
    11 import java.util.Properties;
    12 import java.io.FileReader;
    13 
    14 public class Tomcat{
    15     public static void main(String[] args) throws Exception{
    16         Scanner s = new Scanner(System.in);
    17         System.out.println("服务器启动成功");
    18 
    19         while(true){
    20             System.out.print("请打开浏览器,在浏览器地址栏上请输入请求路径:");
    21             //程序执行到这里,等待用户输入
    22             String requestPath = s.next();
    23             //System.out.println("您访问的资源路径是:" + requestPath);
    24         
    25             //Tomcat读取web.xml文件
    26             FileReader reader = new FileReader("web.xml");
    27             Properties pro = new Properties();
    28             pro.load(reader);
    29             reader.close();
    30 
    31             //通过key获取value
    32             String servletClassName = pro.getProperty(requestPath);
    33 
    34             //通过反射机制创建对象
    35             Class c = Class.forName(servletClassName);
    36             Servlet servlet = (Servlet)c.newInstance();
    37 
    38             //面向Servlet接口调用方法即可
    39             servlet.service();
    40         }
    41     }
    42 }

      模拟web.xml:

    1 /login=LoginServlet
    2 /delete=DeleteServlet
    3 /save=SaveServlet

      总结:Tomcat容器根据web.xml配置文件,调用Servlet接口的方法,自己编写的服务器小程序则具体实现Servlet接口的方法。

    第一个Servlet程序:

      Servlet程序:

     1 import javax.servlet.Servlet;
     2 import javax.servlet.ServletConfig;
     3 import javax.servlet.ServletRequest;
     4 import javax.servlet.ServletResponse;
     5 import javax.servlet.ServletException;
     6 import java.io.IOException;
     7 
     8 public class HelloServlet implements Servlet{
     9 
    10     public void init(ServletConfig config) throws ServletException{
    11         //将信息打印到控制台(而不是浏览器)
    12         System.out.println("Hello world!");
    13     }
    14 
    15     public void service(ServletRequest request,ServletResponse response) 
    16         throws IOException,ServletException{
    17     
    18     }
    19     
    20     public void destroy(){
    21         
    22     }
    23 
    24     public String getServletInfo(){
    25         return null;
    26     }
    27 
    28     public ServletConfig getServletConfig(){
    29         return null;
    30     }
    31 
    32 }

      前端页面:

     1 <html>
     2     <head>
     3         <title>welcome page</title>
     4     </head>
     5     <body>
     6         <!--为了更加的通用,URL路径中的IP地址,端口号可以省略-->
     7         <!--以下网络资源路径还是一个绝对路径,目前必须以"/"开始-->
     8         <a href="/FirstServletWebApp/hello">HelloServlet</a>
     9     </body>
    10 </html>

      web.xml:

     1 <?xml version="1.0" encoding="ISO-8859-1"?>
     2 <web-app xmlns="http://java.sun.com/xml/ns/javaee"
     3   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
     5                       http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
     6   version="3.0">
     7     <!--这是一个合法的web.xml文件-->
     8     <!--一个webapp只有一个web.xml文件-->
     9     <!--web.xml文件主要配置请求路径和Servlet类名之间的绑定关系-->
    10     <!--web.xml文件在Tomcat服务器启动阶段被解析-->
    11     <!--web.xml文件解析失败,会导致webapp启动失败-->
    12     <!--web.xml文件中的标签不能随意编写,因为Tomcat服务器早就知道文件中编写了哪些标签-->
    13     <!--web.xml文件中的标签也是SUN公司制定的Servlet规范-->
    14     <servlet>
    15         <servlet-name>thisIsServletName</servlet-name>
    16         <servlet-class>HelloServlet</servlet-class>
    17     </servlet>
    18     <servlet-mapping>
    19         <servlet-name>thisIsServletName</servlet-name>
    20         <!--路径随意编写,但是必须以"/"开始-->
    21         <!--这是一个虚拟路径,只是代表一个资源的名称-->
    22         <url-pattern>/df/s/fs/f/sdf/s/f</url-pattern>
    23         <!--可以编写多个url-pattern,但是路径需要以"/"开始-->
    24         <url-pattern>/hello</url-pattern>
    25     </servlet-mapping>
    26 
    27 </web-app>

      

    将响应结果输出到浏览器中:

      Servlet程序:

     1 import javax.servlet.Servlet;
     2 import javax.servlet.ServletConfig;
     3 import javax.servlet.ServletException;
     4 import javax.servlet.ServletRequest;
     5 import javax.servlet.ServletResponse;
     6 import java.io.IOException;
     7 import java.io.PrintWriter;  //标准输出流,不需要关闭
     8 
     9 public class WelcomeServlet implements Servlet{
    10     public void init(ServletConfig config) throws ServletException{
    11         
    12     }
    13     
    14     public void service(ServletRequest request,ServletResponse response)
    15         throws ServletException,IOException{
    16         //解决响应的时候中文乱码问题
    17         //设置响应的内容类型以及字符编码方式
    18         //在获取响应流之前设置有效果
    19         response.setContentType("text/html;charset=UTF-8");
    20 
    21         //将信息输出到浏览器上
    22         //将HTML字符串输出到浏览器上,浏览器解释执行
    23         //获取输出流对象,流直接指向特定的浏览器客户端
    24         PrintWriter out = response.getWriter();
    25         
    26         //响应HTML代码到浏览器,并且在网页上体现换行
    27         out.print("<html>");
    28         out.print("<head>");
    29         out.print("<title>welcome servlet</title>");
    30         out.print("</head>");
    31         out.print("<body>");
    32         out.print("<h1 align="center">welcome study servlet!</h1>");
    33         out.print("hello");
    34         out.print("<br>");
    35         out.print("你好中国!");
    36         out.print("</body>");
    37         out.print("</html>");
    38     }
    39 
    40     public void destroy(){
    41     
    42     }
    43 
    44     public String getServletInfo(){
    45         return null;
    46     }
    47 
    48     public ServletConfig getServletConfig(){
    49         return null;
    50     }
    51 }

      web.xml:

     1 <?xml version="1.0" encoding="ISO-8859-1"?>
     2 <web-app xmlns="http://java.sun.com/xml/ns/javaee"
     3   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
     5                       http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
     6   version="3.0">
     7 
     8     <servlet>
     9         <servlet-name>helloServlet</servlet-name>
    10         <servlet-class>WelcomeServlet</servlet-class>
    11     </servlet>
    12     <servlet-mapping>
    13         <servlet-name>helloServlet</servlet-name>
    14         <url-pattern>/hello</url-pattern>
    15     </servlet-mapping>
    16 
    17 </web-app>
  • 相关阅读:
    linux常用命令
    linux简介
    vmware+CentOs 6.9的安装步骤
    Android layer type与WebView白屏-第一次加载webview很慢
    Android View框架的measure机制
    maven的facet错误解决方法
    oracle jdbc驱动发布nexus
    dubbox2.8.4编译发布到私服(nexus)
    一致性哈希 与 普通哈希对比
    1对多业务,数据库水平切分架构一次搞定 | 架构师之路
  • 原文地址:https://www.cnblogs.com/qjjazry/p/6368336.html
Copyright © 2011-2022 走看看