### Web 应用服务器

安装在服务端上的一个服务产品,我们开业在 Web 应用服务器上放置一些允许客户端直接访问的资源,然后启动该服务,这样客户端就可以通过 IP+端口 的方式来访问 Web 应用服务器上的资源。

Web 应用服务器:Tomcat、Jboss、webloic、Jetty

Tomcat

bin:存放各种平台下启动和停止 Tomcat 服务的脚本文件

conf:存放 Tomcat 服务器配置文件

lib:存放 Tomcat 服务需要的 jar 文件

logs:存放 Tomcat 运行的日志文件

temp:存放 Tomcat 运行时的临时文件

webapps:存放允许客户端访问的资源

work:存放 JSP 生成的 Servlet



### Java Web

Servlet + JSP

Servlet 接收客户端请求,进行业务逻辑处理,通过 JDBC 完成持久层的操作。

JSP 负责与用户进行交互,将 Servlet 处理好的结果展示给用户。

Servlet 时 Java Web 开发的基石,与平台无关的服务器组件,它是运行在 Servlet 容器中(Tomcat),负责与客户端进行通信,完成数据交互。

Servlet 可以完成以下功能:

- 创建并返回基于客户端请求的动态 HTML 页面。
- 与数据库进行通信。

Servlet 就是一个 Java 类,一个普通的 Java 类,实现 Servlet 接口就可以接收客户端请求,成为一个服务端组件。

实际开发中,我们不需要直接实现 Servlet 接口,只需要继承 HttpServlet 类即可。

MyServlet.java

package com.southwind.test;

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

@WebServlet("/test.do")
public class MyServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        System.out.println("进入了Servlet");
        String value = req.getParameter("name");
        System.out.println("客户端传来的参数是"+value);
        resp.setContentType("text/html;charset=UTF-8");
        PrintWriter out = resp.getWriter();
        out.print("进入了Servlet");
    }

}

index.jsp

<%--
  Created by IntelliJ IDEA.
  User: southwind
  Date: 2019-06-30
  Time: 20:49
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>$Title$</title>
  </head>
  <body>
  $END$
  </body>
</html>

 

test.jsp

<%--
  Created by IntelliJ IDEA.
  User: southwind
  Date: 2019-06-30
  Time: 21:23
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <h1>Hello World</h1>
</body>
</html>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">

<!--    <servlet>-->
<!--        <servlet-name>test</servlet-name>-->
<!--        <servlet-class>com.southwind.test.MyServlet</servlet-class>-->
<!--    </servlet>-->

<!--    <servlet-mapping>-->
<!--        <servlet-name>test</servlet-name>-->
<!--        <url-pattern>/test.do</url-pattern>-->
<!--    </servlet-mapping>-->

</web-app>

 

posted on 2019-07-13 23:29  HiJackykun  阅读(188)  评论(0编辑  收藏  举报