Servlets & JSP Series 1 - Why do we use JSP
- Web由数以亿计的客户(浏览器)和服务器(Apache应用服务器)组成,这些客户和服务器之间通过有线和无线网络连接,Web服务器接收客户请求,然后向客户返回一些结果。
- 客户的请求包含客户所找资源的名字和地址(URL),服务器通常有很多“内容”发给客户,这些内容可能是Web页面或其他资源,服务器的响应包含客户请求的具体文档(在无法处理客户的请求,返回一个错误值)。
- Web客户允许用户请求服务器上的某个资源,并且向用户显示请求的结果,用户在浏览器上点击一个链接,浏览器对请求格式化,并把它发送给服务器,服务器找到所请求的页面,然后服务器格式化响应,并将其发送给客户,浏览器得到HTML,并显示给用户。
- 客户和服务器要想通信,它们必须有一种共同的语言,在Web上,客户和服务器必须说HTTP,而且浏览器必须懂HTML,HTML告诉浏览器怎样向用户显示内容,HTTP是Web上客户和服务器之间通信所用的协议,服务器使用HTTP向客户发送HTML。
- HTML的目标是拿到一个文本文档,然后为它增加一些标记,告诉浏览器如何对这个文本格式化,浏览器读取HTML代码,创建Web页面,并将该页面显示给用户。
- HTTP协议是TCP/IP的上层协议,TCP负责确保从一个网络节点向另一个网络节点发送的文件能作为一个完整的文件到达目的地,尽管在具体传送过程中这个文件可能会分解为小块传输,IP是一个底层协议,负责把数据块沿路由到达目的地,HTTP则是另一个网络协议,有一些Web特定的特性,不过它要依赖于TCP/IP从一处向另一处完整地传送请求和响应。
- HTTP会话结构是一个简单的请求/响应序列:浏览器发出请求,服务器作出响应。
- 请求流关键要素:HTTP方法(要完成的动作);要访问的页面(URL);表单参数(方法参数)。
- 响应流的关键要素:状态码(表明是否成功);内容类型(包括文本、图片、HTML等等);内容(具体的HTML、图片等等)。
- HTTP响应可以包含HTML,它还会在响应中包含的内容前面增加首部信息,HTML浏览器使用首部信息来帮助处理HTML页面。
- HTTP在响应时,首先会看到一个HTTP方法名,道理和Java方法一样,方法名告诉服务器是哪一类请求,并指出消息中余下的部分该如何格式化,HTTP有很多方法,最常用的当属:GET和POST。
- GET:用户点击指向一个页面的链接,浏览器向服务器发送一个HTTP GET,请求服务器获得(GET)页面。
- POST:用户填写表单,点击Submit(提交)按钮,浏览器向服务器发送一个HTTP POST,为服务器提供用户在表单中键入的信息。
- Get is the simplest HTTP method, and its main job in life is to ask the server to get a resource and send it back. The point of GET is to get something back from server.
- Post is more powerful request, it’s like a GET plus plus, with POST, you can request something and at the same time send form data to the server.
- Get & Post are the two big ones that everybody uses. But there are a few rarely used methods including HEAD, TRACE, PUT, DELETE, OPTIONS, and CONNECT.
- Also we can send a little data with HTTP GET, but the reason you might use POST instead of GET include: 1.The total amount of characters in a GET is really limited; 2.The data you send with the GET is appended to the URL up in the browser bar, so whatever you send is exposed; 3.The User cannot use bookmark a form submission if you use POST instead GET.
- All the pieces on one page: 1.the user types a URL; 2. The browser create an HTTP GET request; 3.The HTTP GET is sent to the server; 4.The server finds the page; 5.The server generates an HTTP response; 6. The HTTP response is sent to the browser; 7. The browser renders the HTML; 8.Client looks forward to a successful page transaction.
- For each description, circle either POST or GET depending on which HTTP method you’d choose for implementing that functionality.
- Every resource on the web has its own unique address, URL format: 1.Protocal; 2.Server; 3.Port; 4.Path; 5.Resource.
- Well-known TCP port numbers for common server applications: 1.FTP-21; 2.Telnet-23; 3. SMTP-25; 4.Time-37; 5.HTTP-80; 6.POP3-110; 7.HTTPS-443. The TCP port numbers from 0 to 1023 are reserved for well-known services(including the big one we care about-port 80). Better do not use these ports for our own custom server programs.
- A static page just sits there in a directory. The server finds it and hands it back to the client as is. Every client sees the same thing.
- If you need just-in-time pages(dynamically-created pages that do not exist before the request) and the ability to write/save data on the server(which means: writing to: 1.a file or 2.database), you cannot rely on the web server alone. Just-in-time pages do not exist before the request comes in. It’s like making an HTML page out of thin air. The request comes in, the helper app “writes” the HTML, and the web server gets it back to the client.
- The non-Java term for a web server helper app is “CGI” program, CGI stands for Common Gateway Interface.
- Servlets and CGI both play the role of a helper app in the web server.
- Servlets demystified(write, deploy, run): 1. Build directory tree 2.Write a servlet named Ch1Servlet.java and put it in the src directory 3.Create a deployment descriptor(DD) named web.xml and put it in the etc directory 4.Build this directory tree under the existing tomcat directory 5.From the project1 directory, compile the servlet 6.Copy the Ch1Servlet.class file to WEB-INF/classes, and copy the web.xml file to WEB-INF 7.From the tomcat directory, start Tomcat 8.Launch your browser and type in the URL & Path 9.Every time you update either a servlet class or the deployment descriptor, shutdown Tomcat.
- Actually, trying to format HTML inside a servlet’s out.println() pretty much sucks. A JSP page looks just like an HTML page, except you can put Java and Java-related things inside the page. So it really is like inserting a variable into your HTML.
- JSP lets the Java programmer off the hook for writing HTML, but it doesn’t really help the HTML designer.
- HTTP stands for Hypertext Transfer Protocol, and it the network protocol used on the Web. It runs on top of TCP/IP. HTTP uses a request/response model-the client makes an HTTP request, and the web server gives back an HTTP response that the browser then figures out how to handle.
- If the response from the server is an HTML page, the HTML is added to the HTTP response, an HTTP request includes the request URL, the HTTP method, and form parameter data, an HTTP response includes a status code, the content-type, and the actual content of the response.
- A GET request appends form data to the end of the URL, a POST request includes form data in the body of the request.
- A MIME type tells the browser what kind of data the browser is about to receive so that the browser will know what to do with it.
- URL stands for Uniform Resource Locator, every resource on the web has its own unique address in this format. It starts with a protocol, followed by the server name, an optional port number, and usually a specific path and resource name, it can also include an optional query string, if the URL is for a GET request.
- Web servers are good at serving static HTML pages, but if you need dynamically-generated data in the page, you need some kind of helper app can work with the server. The non-Java term for these helper apps is CGI.
- Putting HTML inside a println() statement is ugly and error-prone, but JSPs solve that problem by letting you put Java in to an HTML page rather than putting HTML into Java code.