一、get or post?
We can send data to the data processing page by both the GET and POST methods of a form. Both methods are used in form data handling where each one has some difference on the way they work. We will discuss some of the differences.
As you have seen there is a character restriction of 255 in the URL. This is mostly the old browsers restriction and new ones can handle more than that. But we can't be sure that all our visitors are using new browsers. So when we show a text area or a text box asking users to enter some data, then there will be a problem if more data is entered. This restriction is not there in POST method.
In GET method data gets transferred to the processing page in name value pairs through URL, so it is exposed and can be easily traced by visiting history pages of the browser. So any login details with password should never be posted by using GET method.
As the data transfers through address bar ( URL ) there are some restrictions in using space, some characters like ampersand ( & ) etc in the GET method of posting data. We have to take special care for encoding ( while sending ) and decoding ( while receiving ) data if such special characters are present.
There are some special cases where advantage of using GET method is , one can store the name value pairs as bookmark and directly use them by bypassing the form.
二、URL解析
- 协议
- 服务器:所请求的物理服务器的唯一名字,这个名字映射到一个唯一IP地址
- 端口:URL这一部分是可选的,一个服务器可以支持多个端口。一个服务器应由一个端口标识。如在URL中无指定端口,默认为80,这正是web服务器的默认端口。
- 路径:请求的资源在服务器上的路径。这是一个UNIX格式的路径,因为早期的web服务器采用UNIX系统。
- 资源:请求的内容名字。
三、端口
- TCP端口就是一个数字而已。
- 这是一个16位的数,标识服务器硬件上一个特定的软件程序。
- Internet Web(HTTP)服务器软件在端口80上运行,这是一个标准。
- Telnet服务器在23端口运行
- FTP在21端口上运行。
- POP3邮件服务器在37上。
- SMTP---25
- HTTPS---443
- 服务器上有65536个端口(0-65535),而0-1023的TCP端口号已经保留,由一些知名的服务使用,你自己定制的服务器程序不要使用这些端口。
四、一个小小的SERVLET编写,编译,部署,运行过程:
- 建立一个开发源码以及编译的目录结构:
- src用来放置源文件;classes放置编译好的.class文件;etc放置配置文件web.xml
- 编写一个简单的servlet,放在目录p\src\com\test下(也可以直接放在目录\src下)
package com.test; public class Ch1Servlet extends javax.servlet.http.HttpServlet { public void doGet (javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOException { java.io.PrintWriter out = response.getWriter(); java.util.Date now = new java.util.Date(); out.println("<h1 align='center'> CurrentTime is: " + now + "</h1>"); } }
- 编译文件.class应该放到目录“\classes”下
- 在目录“\etc”下编写配置文件web.xml
<?xml version="1.0" encoding="ISO-8859-1"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <Servlet> <Servlet-name>test1.do</Servlet-name> <Servlet-class>com.test.Ch1Servlet</Servlet-class> </Servlet> <Servlet-mapping> <Servlet-name>test1.do</Servlet-name> <url-pattern>/test1.do</url-pattern> </Servlet-mapping> </web-app>
- 至此,完成编写编译阶段,进入部署阶段。在TOMCAT的目录“\webapps”下部署如下目录:
- 将源文件开发阶段的目录“\classes\”下的所有目录拷到部署目录“\classes\”下。
- 新建的web.xml放到部署目录“\WEB-INF\”下
<?xml version="1.0" encoding="ISO-8859-1"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <Servlet> <Servlet-name>test1.do</Servlet-name> <Servlet-class>com.test.Ch1Servlet</Servlet-class> </Servlet> <Servlet-mapping> <Servlet-name>test1.do</Servlet-name> <url-pattern>/test1.do</url-pattern> </Servlet-mapping> </web-app>
- 开启tomcat,即可在浏览器端输入“http://localhost:8080/p/test1.do”访问编写的“Ch1Servlet.class”这个servlet了。