Tomcat Servlet and learning the basics (a)

Tomcat is the author for beginners, the main purpose of writing this article is to facilitate their understanding of the content of Tomcat, some not so professional and comprehensive description, please bear with me.

A, Tomcat Overview

Tomcat server is a free open source Web application server, are lightweight application server, it is not widely used in many occasions small and medium systems and concurrent user access is the preferred development and debugging JSP program. For a beginner, it can be considered that when the Apache server configured on a single machine, it responds to the access request can use HTML (under application of a Standard Generalized Markup Language) pages. Apache Tomcat server is actually an extension, but when it is run independently run, so when you run tomcat, it is in fact as a separate process with Apache running alone.
Source: Tomcat

Tomcat is aweb serverWe in the development of web projects, the project needs to be deployed to the web server, Tomcat is a very friendly server, very suitable for development use. For why should the web projects deployed to the server, not discussed here.

Two, Tomcat installation and configuration

See in particular blog win10 IDEA tomcat configuration problems encountered and solutions .

Third, the use Tomcat in the web project inside

In the above installation and configuration which actually generates a web project, project structure as follows:
Here Insert Picture Description
and the general project compared to a more largelyweb folder. There is another in the web directoryindex.jspFile, the complete code is as follows:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>

  <head>
    <title>$Title$</title>
  </head>
  
  <body>
  $END$
  </body>

</html>

Here comes to the jsp, jsp Notes are as follows:

JSP (full name of the Java Server Pages) is advocated by Sun Microsystems, and many of the companies involved in the creation of a common software developers can respond to client requests, and dynamically generate Web pages of technical standards for HTML, XML or other format of the document.
JSP technology is based on Java language as a scripting language, JSP pages it provides an interface to serve for the entire HTTP application server-side Java library unit.
JSP file suffix * .jsp.
JSP development of WEB applications cross-platform, both run on Linux can also run on Windows.

We understand directly into a format just fine. Turning now to the code above, it is clear that the first row, such that some configurations, without careful study. If you previously had to know HTML, it is easy to find the following code is a complete HTML program. Here we focusbodysection:

  <body>
  $END$
  </body>

We all know that inside HTML, which is the content of the main body content of the page, this time to start the program, the program will automatically open a browser pop-up web
Here Insert Picture Description
page's content is the body just inside the content. At this point look at IDEA's log
Here Insert Picture Description
indicating that Tomcat has been successfully configured, and this web project successfully deployed to Tomcat went.

For Tomcat, just a server only, so the focus is to know how to deploy to the Tomcat web projects go, the details of which need not be too tangled for beginners.

Four, Servlet Overview

Java Servlet 是运行在 Web 服务器或应用服务器上的程序,它是作为来自 Web 浏览器或其他 HTTP 客户端的请求和 HTTP 服务器上的数据库或应用程序之间的中间层。
使用 Servlet,您可以收集来自网页表单的用户输入,呈现来自数据库或者其他源的记录,还可以动态创建网页。
Java Servlet 通常情况下与使用 CGI(Common Gateway Interface,公共网关接口)实现的程序可以达到异曲同工的效果。但是相比于 CGI,Servlet 有以下几点优势:

  • 性能明显更好。
  • Servlet 在 Web 服务器的地址空间内执行。这样它就没有必要再创建一个单独的进程来处理每个客户端请求。
  • Servlet 是独立于平台的,因为它们是用 Java 编写的。
  • 服务器上的 Java 安全管理器执行了一系列限制,以保护服务器计算机上的资源。因此,Servlet 是可信的。
  • Java 类库的全部功能对 Servlet 来说都是可用的。它可以通过 sockets 和 RMI 机制与 applets、数据库或其他软件进行交互

来源:Servlet 简介

所以Servlet就是可以运行在服务器(当然包括Tomcat)上面的程序。那么为什么要用Servlet呢?

这里所谓的Servlet不单单是指程序,就像Java不是仅仅指一门高级语言,它们都包括了一些技术(简单理解就是库、接口、方法)、平台之类的东西。Servlet作为来自 Web 浏览器或其他 HTTP 客户端的请求和 HTTP 服务器上的数据库或应用程序之间的中间层,那么我们编写有关Servlet的代码不就是很好地完成了客户端与服务器之间地数据处理嘛?所以要使用Servlet。

五、Servlet的使用

前面提到过Servlet包括很多东西,使用Servlet肯定就离不开使用它的一些包,主要是使用javax.servletjavax.servlet.http(从名字就可以看出来,Servlet是包含在Java里面的)。

实现Servlet就是通过javax.servlet和javax.servlet.http包生成相应的Servlet类。主要有以下三种方法:

  • 实现 Servlet 接口
  • 继承 GenericServlet 类
  • 继承 HttpServlet 方法

用的最多的是继承 HttpServlet 方法,这也是用IDEA默认的生成方式。具体操作:右击项目的src目录,然后
Here Insert Picture Description
名字的话就看自己意愿。
生成的代码如下:

@WebServlet(name = "GenerateServlet") 
public class GenerateServlet extends HttpServlet {
    @Override protected void doPost(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {

    }

    @Override protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    }
}

这里面应该关注的是

@WebServlet(name = "GenerateServlet") 

这一段代码,这是注解,可以简单理解为通过这个将web端与我们实现的Servlet类联系起来(为方便理解)。
另外我们所生成的类实现了==doPost()、doGet()==这两个方法,其中doGet是处理客户端发来的Get请求,doPost是处理客户端发来的Post请求,对于两者请求的区别,详见

doGet是处理客户端发来的Get请求,doPost是处理客户端发来的Post请求,它们的区别可以从以下7个方面来说明:
1、客户端(一般指浏览器)生成的方式
get:form中method属性为get时;或者直接在URL地址栏中输入URL,需要传递参数时,直接在URL后面拼接“?name=张三&age=18”这样的查询参数字符串;
post:form中method属性为post。
2、客户端数据传送方式
get:表单数据存放在URL地址后面。所有get方式提交时HTTP中没有消息体;
post:表单数据存放在HTTP协议的消息体中以实体的方式传送到服务器。
3、服务器获取数据方式
get:服务器采用Servlet中的doGet来获取变量的值;
post:服务器采用Servlet中的doPost来获取数据。
4、传输的数据量
get:数据量长度有限制,一般不超过2kb。因为是参数传递,且在地址栏中,故数据量有限制;
post:适合大规模的数据传送。因为是以实体的方式传送的。
5、安全性
get:安全性差。因为是直接将数据显示在地址栏中,浏览器有缓冲,可记录用户信息。所以安全性低;
post:安全性高。因为post方式提交数据时是采用的HTTP post机制,是将表单中的字段与值放置在HTTP HEADER内一起传送到ACTION所指的URL中,用户是看不见的。

来源:Servlet中doGet与doPost的区别

六、总结

Tomcat is a web server, in the development of web project deployment should project to the server (Tomcat) to function correctly.
Servlet is an intermediate layer between the client and the server, the client and server may be connected through it, typically implemented using methods inherited HttpServlet Servlet.

Here Insert Picture Description
2019.12.10

Published 52 original articles · won praise 59 · views 6837

Guess you like

Origin blog.csdn.net/ataraxy_/article/details/103481339