Java Servlets - A Tutorial. Introduction. In the early days, web servers deliver static contents that are indifferent to users' requests. Java servlets are server- side programs (running inside a web server) that handle clients' requests and return a customized or dynamic response for each request. The dynamic response could be based on user's input (e. Java servlets typically run on the HTTP protocol. HTTP is an asymmetrical request- response protocol. The client sends a request message to the server, and the server returns a response message as illustrated. Server- Side Technologies.
There are many (competing) server- side technologies available: Java- based (servlet, JSP, JSF, Struts, Spring, Hibernate), ASP, PHP, CGI Script, and many others. Java servlet is the foundation of the Java server- side technology, JSP (Java. Server Pages), JSF (Java. Server Faces), Struts, Spring, Hibernate, and others, are extensions of the servlet technology. Pre- requisites. HTML, Java Programming Language, HTTP and Apache Tomcat Server, SQL and My. SQL Database System, and many others. Apache Tomcat Server. Servlets are server- side programs run inside a Java- capable HTTP server. You can run these examples by launching Tomcat and issuing URL http: //localhost: 8. Java Servlet Versions. Java Servlet has these versions: . For developers, check out the Servlet Developers @ http: //java. Java Servlet is the foundation technology for Java server- side programming. You need to understand Servlet thoroughly before you could proceed to other Java server- side technologies such as Java. Server Pages (JSP) and Java. Server Faces (JSF). Review of HTTPA HTTP Servlet runs under the HTTP protocol. It is important to understanding the HTTP protocol in order to understand server- side programs (servlet, JSP, ASP, PHP, etc) running over the HTTP. The client sends a request message to the server. The server, in turn, returns a response message. The messages consists of two parts: header (information about the message) and body (contents). Header provides information about the messages. The data in header is organized in name- value pairs. Read . We shall use JDK and Tomcat to understand the basics, instead of IDE such as Eclipse/Net. Beans. Once you understand the basics, you should use Eclipse/Net. Beans to develop your webapp for better productivity. Create a new Webapp . A webapp, known as a web context in Tomcat, comprises a set of resources, such as HTML files, CSS, Java. Scripts, images, programs and libraries. A Java webapp has a standardized directory structure for storing various types of resources. Create a directory . Create sub- directories . Create sub- sub- directories . Take note that the directory names are case- sensitive. The resources must be kept in the respective directories: < CATALINA. These resources will be delivered to the clients as it is. Nuts and Bolts: The Basics of Android Programming Nuts and Bolts Introduction Setting Up Your System. A Simple Servlet Example – (write, deploy, run) By mkyong . You could create sub- directories such as images, css and scripts, to further categories the resources.< CATALINA. This is where you keep your application- specific configuration files (such as . Classes defined in packages must be kept according to the package directory structure.< CATALINA. It keeps resources and configurations (e. A user can invoke a servlet by issuing a specific URL from the browser (HTTP client). In this example, we shall write a servlet called . Let's call our package . Create a sub- directory called . Use a programming text editor to enter the following source codes, and save as . Line 2 places this class in a package called mypkg. Hence, we save the source file under . Servlet API is not part of JDK or Java SE (but belongs to Java EE). Tomcat provides a copy of servlet API called . The output is < CATALINA. The compiler creates the package directory . Instead, we create a servlet by sub- classing javax. Http. Servlet (in Line 8). As mentioned, a servlet is invoked in response to a request URL issued by a client. Specifically, a client issues an HTTP request, the server routes the request message to the servlet for processing. The servlet returns a response message to the client. An HTTP request could use either GET or POST request methods, which will be processed by the servlet's do. Get() or do. Post() method, respectively. In the Hello. Servlet, we override the do. Get() method (as denoted by the annotation @Override). The do. Get() runs in response to an HTTP GET request issued by a user via an URL. The Http. Servlet. Response object can be used to set the HTTP response headers (e. In Line 1. 3, we set the . The client need to know the message type in order to correctly display the data received. We then use the out. HTML page containing the message . This servlet also echoes some of the clients's request information, and prints a random number for each request. Configure the Application Deployment Descriptor - . In this example, we shall configure the following request URL to trigger the . Verify that the web context . Assume that Tomcat is running in port number 8. We shall see the output . User does not receive the servlet's program codes, which are kept under a hidden directory . The likely errors are . This allows web users to interact with the web server by submit data. For example,Create the following HTML script, and save as . Issue the following URL to request for the HTML page. The < legend>..< /legend> tag provides the legend for the box. This HTML form (enclosed within < form>..< /form> ) contains the following types of input elements. Text field (< input type=. If an element is selected, its . BUT observe the destination URL. Alan+Smith& gender=m&.. Observe that: The URL http: //localhost: 8. Relative URL is used in this example. The base URL for the current page . Hence, the relative URL . Also take note that the blank (in . This is because special characters are not permitted in the URL and have to be encoded (known as URL- encoding). Blank is encoded as '+' (or %2. Other characters are encoded as %xx, where xx is the ASCII code in hex. For example, '& ' as %2. F. Some input elements such as checkboxes may trigger multiple parameter values, e. For GET request, the query parameters are appended behind the URL. For POST request, the query string are sent in the request message's body. POST request is often preferred, as users will not see the strange string in the URL and it can send an unlimited amount of data. The amount of data that can be sent via the GET request is limited by the length of the URL. The request method is specified in the < form method=. In this tutorial, we use the GET request, so that you can inspect the query string. Write a Servlet to Process Form Data - . Let us write a servlet called Echo. Servlet, which shall be mapped to the URL . The servlet simply echoes the data back to the client. Similar to the . We can retrieve the query parameters from the request message (captured in do. Get()'s argument Http. Servlet. Request request) via one of the following methods. Parameter(. If the parameter is present (not null), we trim() the returned string to remove the leading and trailing white spaces. We also replace the special HTML characters (> , < , & , . This step is necessary to prevent the so- called command- injection attack, where user enters a script into the text field. The replacement is done via a static helper method html. Filter(). That is, '+' will be decoded to blank, %xx decoded into the corresponding character. Configure the Servlet URL mapping in . Issue URL http: //localhost: 8. Fill up the form, click the submit button to trigger the servlet. Alternatively, you could issue a URL with query string. Form- Data Submission Methods: GET. GET and POST performs the same basic function. That is, gather the name- value pairs of the selected input elements, URL- encode, and pack them into a query string. However, in a GET request, the query string is appended behind the URL, separated by a '?'. Whereas in a POST request, the query string is kept in the request body (and not shown in the URL). The length of query string in a GET request is limited by the maximum length of URL permitted, whereas it is unlimited in a POST request. I recommend POST request for production, as it does not show the strange looking query string in the URL, even if the amount of data is limited. In this tutorial, I use GET method, so that you can inspect the query string on the URL. To try out the POST request, modify the . Since they often perform identical operations, we re- direct do. Post() to do. Get() (or vice versa), as follows. My. Servlet extends Http. Servlet . The client sends a request message to the server. The server, in turn, returns a response message. The request and response messages consists of two parts: header (information about the message) and body (contents). Header provides information about the messages. The data in header is organized in name- value pairs. Http. Servlet. Request provides many methods for you to retrieve the headers: General methods: get. Header(name), get. Headers(name), get. Header. Names(). Specific methods: get. Content. Length(), get. Content. Type(), get. Cookies(), get. Auth. Type(), etc. URL related: get. Request. URI(), get. Query. String(), get. Protocol(), get. Method(). Example: Read . In other words, the current request does not know what has been done in the previous requests. This creates a problem for applications that runs over many requests, such as online shopping (or shopping cart). You need to maintain a so- called session to pass data among the multiple requests. You can maintain a session via one of these three approaches: Cookie: A cookie is a small text file that is stored in the client's machine, which will be send to the server on each request. You can put your session data inside the cookie. The biggest problem in using cookie is clients may disable the cookie. URL Rewriting: Passes data by appending a short text string at the end of every URL, e. You need to rewrite all the URLs (e. Again, you need to include the hidden field in all the pages.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
January 2017
Categories |