• hello1和hello2代码分析


    1.hello1代码分析

    hello.java

    package javaeetutorial.hello1;


    import javax.enterprise.context.RequestScoped;#导入了一个java包,注解类型为RequestScoped((1)指定bean是请求范围的 (2) 请求范围处于活动状态   (3)请求上下文被销毁)
    import javax.inject.Named;#导入了一个java包,注解类型为Named(常用于命名注解javax.inject.Name并 javax.enterprise.context.RequestScoped使用请求范围将类标识为托管bean。范围定义应用程序数据如何保持和共享)

    @Named
    @RequestScoped

    #使用两个注解
    public class Hello {#定义一个公共类

        private String name;#一个私有字符串name

        public Hello() { #定义自身类对象
        }

        public String getName() {定义对象getName,返回name
            return name;
        }

        public void setName(String user_name) {#将setNaame设置为返回值对象,把user_name赋值给this.name
            this.name = user_name;
        }
    }

    hello.1.web.xml

    <?xml version="1.0" encoding="UTF-8"?>#xml版本号和编码
    <web-app version="3.1" #web-app版本号
             xmlns="http://xmlns.jcp.org/xml/ns/javaee" #表示web.xml命名空间
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" #类url
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">#指定web.xml约束文件xsd所在位置
        <context-param>#表明应用范围内的初始化参数
            <param-name>javax.faces.PROJECT_STAGE</param-name>#将参数名为faces的包导入project_stage
            <param-value>Development</param-value>#参数值
        </context-param>
        <servlet>#在向servlet或JSP页面制定初始化参数或定制URL时,必须首先命名servlet或JSP页面。
            <servlet-name>Faces Servlet</servlet-name>#servlet命名
            <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>#指定servlet类包
            <load-on-startup>1</load-on-startup>#启动负载为1
        </servlet>
        <servlet-mapping>#更改缺省url
            <servlet-name>Faces Servlet</servlet-name>
            <url-pattern>*.xhtml</url-pattern>#指定url参数类型
        </servlet-mapping>
        <session-config>#指定缺省超时值
            <session-timeout>#会话超时为30
                30
            </session-timeout>
        </session-config>
        <welcome-file-list>#指示服务器在收到引用一个目录名而不是文件名的URL时,使用的文件
            <welcome-file>index.xhtml</welcome-file>#应用index.xhtml文件
        </welcome-file-list>
    </web-app>

    2.hello2代码分析

    Responseservlet.java:
    package javaeetutorial.hello2;  #软件包名为javaeetutorial.hello2


    import java.io.IOException;
    import java.io.PrintWriter;
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

      #导入io 和 serverlet相关的各类包
    @WebServlet("/response")

      #注解WebServerlet 为response
    public class ResponseServlet extends HttpServlet {

      #定义类ReponseServerlet从HttpServerlet拓展而来

    @Override

      #注解Override
    public void doGet(HttpServletRequest request,

      #定义对象doGet ,定义HttpServerletRequest 为request

    HttpServletResponse response)

      #定义HttpServerResponse 为response
    throws ServletException, IOException {

      #抛出异常
    try (PrintWriter out = response.getWriter()) {

      #将response的getWeite方法赋值给PrintWrite的out,response的getWrite方法来自于HttpServletResponse接口方法
    String username = request.getParameter("username");

      #将request的getParameter方法获得的username赋值给字符串username,requset来自于HttpServerletRequest接口方法
    if (username != null && username.length() > 0) {

      #当username字符串不为空且username的长度大于0时
    out.println("<h2>Hello, " + username + "!</h2>");

      #调用out的println打印Hello,和字符串
    }
    }
    }

    @Override
    public String getServletInfo() {
    return "The Response servlet says hello.";

      #定义字符串getServerletinfo,返回内容

    }
    }

     

    GreetingServlet.java:

    package javaeetutorial.hello2;  

      #包名hello2

    import java.io.IOException;
    import java.io.PrintWriter;
    import javax.servlet.RequestDispatcher;
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

      #导入各类需要的资源包

    @WebServlet("/greeting")  

      #注解名greeting
    public class GreetingServlet extends HttpServlet {

      #拓展类GreetingServlet从HttpServlet接口

    @Override

      #注解Override
    public void doGet(HttpServletRequest request,
    HttpServletResponse response)

      #同上定义对象 HttpServletRequest名为request,HttpServletResponse名为response
    throws ServletException, IOException {

      #抛出异常

    response.setContentType("text/html");

      #定义文本类型为text,html
    response.setBufferSize(8192);

      #定义缓冲池大小8192
    try (PrintWriter out = response.getWriter()) {
    out.println("<html lang="en">"
    + "<head><title>Servlet Hello</title></head>");

    // then write the data of the response
    out.println("<body bgcolor="#ffffff">"
    + "<img src="resources/images/duke.waving.gif" "
    + "alt="Duke waving his hand">"
    + "<form method="get">"
    + "<h2>Hello, my name is Duke. What's yours?</h2>"
    + "<input title="My name is: " type="text" "
    + "name="username" size="25"/>"
    + "<p></p>"
    + "<input type="submit" value="Submit"/>"
    + "<input type="reset" value="Reset"/>"
    + "</form>");

      #以上是打印网页页面相关内容

    String username = request.getParameter("username");
    if (username != null && username.length() > 0) {
    RequestDispatcher dispatcher =
    getServletContext().getRequestDispatcher("/response");
      #将response响应赋给dispatcher分发调度
    if (dispatcher != null) {
    dispatcher.include(request, response);
    }

      #当匹配到处理请求响应的处理服务时,就返回请求和response.java的响应内容结果
    }
    out.println("</body></html>");
    }
    }

    @Override
    public String getServletInfo() {
    return "The Hello servlet says hello.";

      #在页面返回响应的文字

    }
    }

    引用:https://www.cnblogs.com/lijianxuan/p/10606299.html

  • 相关阅读:
    Spring的历史和哲学
    CORS简介
    LDAP概念了解
    Restful levels&HATEOAS
    python/mysql connector
    Session&Cookie 简介及使用
    XML简介
    Json简介
    《魅族官网》
    期末设计部分代码截图
  • 原文地址:https://www.cnblogs.com/zrj-cdu/p/10631701.html
Copyright © 2020-2023  润新知