zoukankan      html  css  js  c++  java
  • JavaWeb-----实现第一个Servlet程序

       1.Servlet简介

         Servlet是在服务器端运行的一个小程序,实际上一个Servlet就是一个Java类,并且可以通过“请求-响应”编程模型来访问的这个驻留在服务器内

    存里的servlet程序。主要用来处理客户端请求、响应给浏览器的动态资源

       2.Servlet原理

         Web服务器在与客户端交互时.Servlet的工作原理是:

    • 在客户端对web服务器发出请求
    • web服务器接收到请求后将其发送给Servlet
    • Servlet容器为此产生一个实例对象并调用ServletAPI中相应的方法来对客户端HTTP请求进行处理,然后将处理的响应结果返回给WEB服务器
    • web服务器将从Servlet实例对象中收到的响应结构发送回客户端

       3.Servlet生命周期

    • 装载Servlet
    • Server创建一个Servlet实例
    • Server调用Servlet的init方法,只初始化一次
    • 一个客户端请求到达Server
    • Server创建一个请求对象
    • Server创建一个响应对象
    • Server激活Servlet的service方法,传递请求和响应对象作为参数
    • service方法获得关于请求对象的信息,处理请求,访问其他资源,获得需要的信息
    • service方法使用响应对象的方法。将响应传回Server,最终到达客户端
    • 对于更多的客户端请求,Server创建新的请求和响应对象,仍然激活此servlet的service方法,将这两个对象作为参数传递给它
    • 当Server不再需要Servlet时,比如当Server要关闭时,Server调用Servlet的destroy

       4.第一个Servlet实例

         配置Web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <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">
        <display-name>Servlet_HelloWorld</display-name>
        <welcome-file-list>
            <welcome-file>index.jsp</welcome-file>
        </welcome-file-list>
        <servlet>
            <servlet-name>HelloWorldServlet</servlet-name>
            <servlet-class>com.servlets.HelloWorldServlet</servlet-class>
            <!-- 当值为0或者大于0时,表示容器在启动时就加载并初始化这个servlet -->
            <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>HelloWorldServlet</servlet-name>
            <url-pattern>*.do</url-pattern>
        </servlet-mapping>
    </web-app>

         Servlet处理类

    package com.servlets;
    
    import java.io.IOException;
    import javax.servlet.ServletConfig;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;
    
    //@WebServlet("/helloworldservlet") 可以在Web.xml中配置servlet代替
    public class HelloWorldServlet extends HttpServlet {
        private static final long serialVersionUID = 1L;
    
        public HelloWorldServlet() {
            super();
            System.out.println("创建Servlet实例");
        }
    
        public void init(ServletConfig config) throws ServletException {
            System.out.println("初始化");
        }
    
        public void destroy() {
            System.out.println("销毁");
        }
    
        protected void service(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            System.out.println("调用service()方法");
            response.setCharacterEncoding("UTF-8");
            //获得参数
            String userName = request.getParameter("userName");
            String userPwd = request.getParameter("userPwd");
            //将数据存入Session
            HttpSession session = request.getSession();
            session.setAttribute("userName", userName);
            session.setAttribute("userPwd", userPwd);
            //转发
            request.getRequestDispatcher("/success.jsp").forward(request, response);
        }
    }

         JSP页面

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>First Servlet</title>
    </head>
    <body>
        <a
            href="${pageContext.request.contextPath}/helloworldservlet.do?userName=张三&userPwd=123456">HelloWorld!</a>
    </body>
    </html>
    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
        <h1>${userName}</h1>
        <h1>${userPwd}</h1>
    </body>
    </html>

       注意:若是service()、doPost()和doGet()这三个方法都在存在的情况下,java只会执行service()方法,而其他的两种方法不会被执行。若是没有service() 方法,则是根据jsp传入方式选择对应的方法

  • 相关阅读:
    ubuntu 14.04 安装python包psycopg2
    vmare 往 virtualbox迁移
    docker-compose & docker 镜像/加速
    nodejs & npm & gulp 安装和配置
    airflow 优化
    airflow 部署
    windows 上vmare超卡的问题解决方案
    HDU 6781 Solo (贪心 + 优先队列)
    HDU 6779 Drink (最小费用流)
    HDU 6778 Car (状压DP)
  • 原文地址:https://www.cnblogs.com/fengfuwanliu/p/10512689.html
Copyright © 2011-2022 走看看