• Servlet笔记




    慕课网学习笔记-Servlet 加油吧

    1.软件结构发展史

      1.1单机时代

      

       1.2联机时代(Client-Server模式)

      

      1.3互联网时代(Broswer-Server模式)

      

    2.请问与响应、B/S模式执行流程

    从浏览器发出送给服务器的数据包称为“请求 Request ”

    从服务器返回给浏览器的结果称为“响应 Response ”

      

     3.J2EE 与 Apache Tomcat

      

     4.Servlet

      4.1

        Servlet(Server Applet)服务器小程序,主要功能用于生产动态Web内容

      4.2  

        Servlet是J2EE最主要的组成部分,也是我们学习的重点

      4.3Tomcat与Servlet的关系

      

    5.第一个Servlet

     1 package com.imooc.servlet;
     2 
     3 import java.io.IOException;
     4 import java.io.PrintWriter;
     5 
     6 import javax.servlet.ServletConfig;
     7 import javax.servlet.ServletException;
     8 import javax.servlet.http.HttpServlet;
     9 import javax.servlet.http.HttpServletRequest;
    10 import javax.servlet.http.HttpServletResponse;
    11 
    12 public class FirstServlet extends HttpServlet{
    13     
    14     public FirstServlet(){
    15         System.out.println("正在创建FirstServlet对象");
    16     }
    17     
    18     
    19     
    20     @Override
    21     public void init(ServletConfig config) throws ServletException {
    22         System.out.println("正在初始化FirstServlet对象");
    23     }
    24 
    25 
    26 
    27     @Override
    28     protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    29         //接收请求发来的参数
    30         String name = request.getParameter("name");
    31         String html = "<h1 style='color:red'>hi," + name +"!</h1><hr/>";
    32         System.out.println("返回给浏览器的响应数据为:" + html);
    33         PrintWriter out = response.getWriter();
    34         out.println(html);//将html发送回浏览器
    35     }
    36 
    37 
    38 
    39     @Override
    40     public void destroy() {
    41         System.out.println("正在销毁servlet对象");  
    42     }
    43 
    44     
    45 }
    FirstServlet
     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
     3   <display-name>FirstServlet</display-name>
     4   <welcome-file-list>
     5     <welcome-file>index.html</welcome-file>
     6     <welcome-file>index.htm</welcome-file>
     7     <welcome-file>index.jsp</welcome-file>
     8     <welcome-file>default.html</welcome-file>
     9     <welcome-file>default.htm</welcome-file>
    10     <welcome-file>default.jsp</welcome-file>
    11   </welcome-file-list>
    12   <!-- 声明Servlet -->
    13   <servlet>
    14       <!-- servlet的别名 -->
    15       <servlet-name>first</servlet-name>
    16       <servlet-class>com.imooc.servlet.FirstServlet</servlet-class>
    17   </servlet>
    18   <!-- 将Servlet与url绑定 -->
    19   <servlet-mapping>
    20       <servlet-name>first</servlet-name>
    21       <url-pattern>/hi</url-pattern>
    22   </servlet-mapping>
    23   
    24    <servlet>
    25       <!-- servlet的别名 -->
    26       <servlet-name>sample</servlet-name>
    27       <servlet-class>com.imooc.servlet.SampleServlet</servlet-class>
    28   </servlet>
    29   <servlet-mapping>
    30       <servlet-name>sample</servlet-name>
    31       <url-pattern>/sample</url-pattern>
    32   </servlet-mapping>
    33   
    34   <servlet>
    35       <!-- servlet的别名 -->
    36       <servlet-name>requestMethod</servlet-name>
    37       <servlet-class>com.imooc.servlet.RequestMethodServlet</servlet-class>
    38   </servlet>
    39   <servlet-mapping>
    40       <servlet-name>requestMethod</servlet-name>
    41       <url-pattern>/request_method</url-pattern>
    42   </servlet-mapping>
    43 </web-app>
    web.xml

    6.标准Java Web工程、Servlet开发步骤

    7.Servlet

    7.1Servlet访问方法

    如:
    http://localhost:8080/FirstServlet/anno

    7.2请求参数

    如:
    http://localhost:8080/FirstServlet/sample?name=10086
    &mobile=158&sex=male&spec=English&spec=Program&spec=Speech&spec=Swimming

    7.3Servlet接收请求参数

     如:

    String sex=request.getParameter("sex");
    String[] spec=request.getParameterValues("spec");

    7.4Get与Post请求方法

    Get请求提交的数据,可以在浏览器调试页面的Query String Parameters中查看

    Post请求提交的数据,可以在浏览器调试页面的Form Data中查看

    Post请求可以用于安全性较高的功能

     

    7.5Get与Post处理方式

     1 package com.imooc.servlet;
     2 
     3 
     4 import java.io.IOException;
     5 
     6 import javax.servlet.http.HttpServlet;
     7 import javax.servlet.http.HttpServletRequest;
     8 import javax.servlet.http.HttpServletResponse;
     9 
    10 
    11 public class RequestMethodServlet extends HttpServlet{
    12     public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException {
    13         String name=request.getParameter("name");
    14         response.getWriter().println("<h1 style='color:green'>"+name+"</h1>");
    15     }
    16     
    17     public void doPost(HttpServletRequest request,HttpServletResponse response) throws IOException {
    18         String name=request.getParameter("name");
    19         response.getWriter().println("<h1 style='color:red'>"+name+"</h1>");
    20     }
    21 }
    get、post

    7.6应用场景

     8.Servlet生命周期

     

     1 package com.imooc.servlet;
     2 
     3 import java.io.IOException;
     4 import java.io.PrintWriter;
     5 
     6 import javax.servlet.ServletConfig;
     7 import javax.servlet.ServletException;
     8 import javax.servlet.http.HttpServlet;
     9 import javax.servlet.http.HttpServletRequest;
    10 import javax.servlet.http.HttpServletResponse;
    11 
    12 public class FirstServlet extends HttpServlet{
    13     
    14     public FirstServlet(){
    15         System.out.println("正在创建FirstServlet对象");
    16     }
    17     
    18     
    19     
    20     @Override
    21     public void init(ServletConfig config) throws ServletException {
    22         System.out.println("正在初始化FirstServlet对象");
    23     }
    24 
    25 
    26 
    27     @Override
    28     protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    29         //接收请求发来的参数
    30         String name = request.getParameter("name");
    31         String html = "<h1 style='color:red'>hi," + name +"!</h1><hr/>";
    32         System.out.println("返回给浏览器的响应数据为:" + html);
    33         PrintWriter out = response.getWriter();
    34         out.println(html);//将html发送回浏览器
    35     }
    36 
    37 
    38 
    39     @Override
    40     public void destroy() {
    41         System.out.println("正在销毁servlet对象");  
    42     }
    43 
    44     
    45 }
    生命周期

    9.使用注解配置Servlet

     

     1 package com.imooc.servlet;
     2 
     3 import java.io.IOException;
     4 
     5 import javax.servlet.ServletException;
     6 import javax.servlet.annotation.WebServlet;
     7 import javax.servlet.http.HttpServlet;
     8 import javax.servlet.http.HttpServletRequest;
     9 import javax.servlet.http.HttpServletResponse;
    10 
    11 @WebServlet("/anno")
    12 
    13 public class AnnotationServlet extends HttpServlet{
    14 
    15     @Override
    16     protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    17         // TODO Auto-generated method stub
    18         resp.getWriter().println("I,m annotation servlet!");
    19     }
    20     
    21 }

     10.启动时加载Servlet

     1 package com.imooc.servlet;
     2 
     3 import javax.servlet.ServletException;
     4 import javax.servlet.annotation.WebServlet;
     5 import javax.servlet.http.HttpServlet;
     6 
     7 
     8 public class CreateServlet extends HttpServlet{
     9 
    10     @Override
    11     public void init() throws ServletException {
    12         System.out.println("正在创建数据库");
    13     }
    14     
    15 }
     1 package com.imooc.servlet;
     2 
     3 import javax.servlet.ServletException;
     4 import javax.servlet.annotation.WebServlet;
     5 import javax.servlet.http.HttpServlet;
     6 
     7 @WebServlet(urlPatterns="/unused",loadOnStartup=12)
     8 public class ImportServlet extends HttpServlet{
     9 
    10     @Override
    11     public void init() throws ServletException {
    12         System.out.println("正在导入数据");
    13     }
    14     
    15 }
     注解必须,两个参数
    @WebServlet(urlPatterns="/unused",loadOnStartup=12)

  • 相关阅读:
    决策树算法
    数据挖掘建模-Logistic回归
    Python数据结构与算法-栈和递归函数
    使用python自动化生成软件著作权的源代码
    一起玩转图论算法(1)图的基本表示
    全网最最最详细的软件著作权申请步骤
    时间序列模式(ARIMA)---Python实现
    宝塔面板怎样部署SSL证书?
    Linux基础概念入门
    转载:VMware workstation创建虚拟机,安装Ubuntu系统
  • 原文地址:https://www.cnblogs.com/qqfff/p/12275197.html
Copyright © 2020-2023  润新知