tomcat servlet初步了解

简介

tomcat是一个web的容器。很多时候可以通过这个来做php类似的工作。Java中的Servlet就是能运行在tomcat中。本文主要是通过实例来说明一下组件的关系。
tomcat可以完全理解成一个nginx+fpm的web服务器。他是集成了jsp相关的东西。

在网上找到的介绍资料:

Tomcat 服务器是一个免费的开放源代码的Web 应用服务器,属于轻量级应用服务器,在中小型系统和并发访问用户不是很多的场合下被普遍使用,是开发和调试JSP 程序的首选。

启动

这次做的一些测试是在windows下测试的,使用了xampp套件。
这里写图片描述
这是启动tomcat之后的控制台:
这里写图片描述
可以看到在启动过程中,他将会去读取java的运行环境,并且将会加载自己tomcat的jar包,并且将服务器启动起来。
这里写图片描述

实例

jsp

当访问这个地址的时候将会出现以下内容
http://localhost:8080/examples/jsp/jsp2/el/basic-arithmetic.jsp

这里写图片描述
这份代码被放在:
这里写图片描述

jsp的内容为:

<%--
 Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
--%>
<html>
  <head>
    <title>JSP 2.0 Expression Language - Basic Arithmetic</title>
  </head>
  <body>
    <h1>JSP 2.0 Expression Language - Basic Arithmetic</h1>
    <hr>
    This example illustrates basic Expression Language arithmetic.
    Addition (+), subtraction (-), multiplication (*), division (/ or div),
    and modulus (% or mod) are all supported.  Error conditions, like
    division by zero, are handled gracefully.
    <br>
    <blockquote>
      <code>
        <table border="1">
          <thead>
        <td><b>EL Expression</b></td>
        <td><b>Result</b></td>
      </thead>
      <tr>
        <td>\${1}</td>
        <td>${1}</td>
      </tr>
      <tr>
        <td>\${1 + 2}</td>
        <td>${1 + 2}</td>
      </tr>
      <tr>
        <td>\${1.2 + 2.3}</td>
        <td>${1.2 + 2.3}</td>
      </tr>
      <tr>
        <td>\${1.2E4 + 1.4}</td>
        <td>${1.2E4 + 1.4}</td>
      </tr>
      <tr>
        <td>\${-4 - 2}</td>
        <td>${-4 - 2}</td>
      </tr>
      <tr>
        <td>\${21 * 2}</td>
        <td>${21 * 2}</td>
      </tr>
      <tr>
        <td>\${3/4}</td>
        <td>${3/4}</td>
      </tr>
      <tr>
        <td>\${3 div 4}</td>
        <td>${3 div 4}</td>
      </tr>
      <tr>
        <td>\${3/0}</td>
        <td>${3/0}</td>
      </tr>
      <tr>
        <td>\${10%4}</td>
        <td>${10%4}</td>
      </tr>
      <tr>
        <td>\${10 mod 4}</td>
        <td>${10 mod 4}</td>
      </tr>
    <tr>
      <td>\${(1==2) ? 3 : 4}</td>
      <td>${(1==2) ? 3 : 4}</td>
    </tr>
    </table>
      </code>
    </blockquote>
  </body>
</html>

这个实例是用于演示jsp中的数学计算相关的功能。在他的jsp的实例里面还有

Expression Language
Basic Arithmetic    
Basic Comparisons   
Implicit Objects    
Functions   
Composite Expressions   

具体如果想学习的话可以自己去阅读jsp相关书籍。传送门

servlet

Servlet 为创建基于 web 的应用程序提供了基于组件、独立于平台的方法,可以不受 CGI 程序的性能限制。Servlet 有权限访问所有的 Java API,包括访问企业级数据库的 JDBC API。

手册传送门
效果:
这里写图片描述
源代码位置:
这里写图片描述

源码
RequestInfoExample.java

/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements.  See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License.  You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ResourceBundle;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import util.HTMLFilter;

/**
 * Example servlet showing request information.
 *
 * @author James Duncan Davidson <duncan@eng.sun.com>
 */

public class RequestInfoExample extends HttpServlet {

    private static final long serialVersionUID = 1L;

    private static final ResourceBundle RB = ResourceBundle.getBundle("LocalStrings");

    @Override
    public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
        throws IOException, ServletException
    {
        response.setContentType("text/html");

        PrintWriter out = response.getWriter();
        out.println("<html>");
        out.println("<head>");

        String title = RB.getString("requestinfo.title");
        out.println("<title>" + title + "</title>");
        out.println("</head>");
        out.println("<body bgcolor=\"white\">");

        // img stuff not req'd for source code html showing
        // all links relative!

        // XXX
        // making these absolute till we work out the
        // addition of a PathInfo issue

        out.println("<a href=\"../reqinfo.html\">");
        out.println("<img src=\"../images/code.gif\" height=24 " +
                    "width=24 align=right border=0 alt=\"view code\"></a>");
        out.println("<a href=\"../index.html\">");
        out.println("<img src=\"../images/return.gif\" height=24 " +
                    "width=24 align=right border=0 alt=\"return\"></a>");

        out.println("<h3>" + title + "</h3>");
        out.println("<table border=0><tr><td>");
        out.println(RB.getString("requestinfo.label.method"));
        out.println("</td><td>");
        out.println(HTMLFilter.filter(request.getMethod()));
        out.println("</td></tr><tr><td>");
        out.println(RB.getString("requestinfo.label.requesturi"));
        out.println("</td><td>");
        out.println(HTMLFilter.filter(request.getRequestURI()));
        out.println("</td></tr><tr><td>");
        out.println(RB.getString("requestinfo.label.protocol"));
        out.println("</td><td>");
        out.println(HTMLFilter.filter(request.getProtocol()));
        out.println("</td></tr><tr><td>");
        out.println(RB.getString("requestinfo.label.pathinfo"));
        out.println("</td><td>");
        out.println(HTMLFilter.filter(request.getPathInfo()));
        out.println("</td></tr><tr><td>");
        out.println(RB.getString("requestinfo.label.remoteaddr"));
        out.println("</td><td>");
        out.println(HTMLFilter.filter(request.getRemoteAddr()));
        out.println("</td></tr>");

        String cipherSuite=
                (String)request.getAttribute("javax.servlet.request.cipher_suite");
        if(cipherSuite!=null){
            out.println("<tr><td>");
            out.println("SSLCipherSuite:");
            out.println("</td><td>");
            out.println(HTMLFilter.filter(cipherSuite));
            out.println("</td></tr>");
        }

        out.println("</table>");
    }

    @Override
    public void doPost(HttpServletRequest request,
                      HttpServletResponse response)
        throws IOException, ServletException
    {
        doGet(request, response);
    }

}

这个代码写起来有些像php的写法,打印html语法,最后就能在浏览器中显示。
掌握这些知识之后,搭建一个web后台也是可以上路了。在他的官方实例里面还有关于WebSocket的实例,这个有空再来阅读了。web开发相当于扯上了java这个大爷了,所以做起事情了,也将会事半功倍的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值