{"id":107,"date":"2018-07-11T06:00:02","date_gmt":"2018-07-11T06:00:02","guid":{"rendered":"http:\/\/itp16.epgpbooks.inflibnet.ac.in\/?post_type=chapter&#038;p=107"},"modified":"2019-05-15T07:18:37","modified_gmt":"2019-05-15T07:18:37","slug":"basic-servlet-api","status":"publish","type":"chapter","link":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/chapter\/basic-servlet-api\/","title":{"rendered":"Basic Servlet API"},"content":{"raw":"<div><span style=\"float: right;\"><a href=\"https:\/\/youtu.be\/MUP5aKSimGI\" target=\"_blank\" rel=\"noopener\"><img src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"epgp books\" width=\"75px\" height=\"75px;\" \/><\/a>\r\n<\/span><\/div>\r\n<div style=\"text-align: justify;\">\r\n\r\n&nbsp;\r\n\r\n<strong>Introduction<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify;\">In this module we will look at the Servlet interface as a whole and the corresponding interface which are used from Servlet, which are init method in the servlet life cycle, so we have the ServletConfig object being injected in the Servlet object. So, ServletConfig methods would be explored. ServletConfig makes available to us the ServletContext object. ServletContext methods are useful giving us some information in web.xml file. So how to fetch the information from web.xml, and we will also explore ServletRequest and ServletResponse object. And finally we will create our first Servlet which will be used for giving very simple response called \"HelloWorld\". So, we would like to create some HTML output which will generate a \u201cHello world\u201d.<\/p>\r\n&nbsp;\r\n<p class=\"hanging-indent\"><strong>Quick Review of Servlet life cycle. <\/strong><\/p>\r\n&nbsp;\r\n\r\nThree basic methods in servlet life cycle:\r\n\r\n&nbsp;\r\n\r\npublic void init(ServletConfig cfg)\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify;\">As soon as the web container starts, it identifies the applications. Per application it creates a ServletContext Object. At the time of creating ServletContext object it is going through web.xml file and it get the display name from web.xml, which is going to be available through ServletContext object because this is one per application.<\/p>\r\n&nbsp;\r\n<p class=\"hanging-indent\" style=\"text-align: justify;\"><strong>Servlet Context<\/strong><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\">The ServletContext is nothing but your web application. So the display name can be known using the getServletContextName() method.<\/p>\r\n&nbsp;\r\n\r\npublic String getServletContextName()\r\n\r\npublic Enumeration&lt;String&gt; getInitParameterNames()\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify;\">it is used to get the context-param name which is set by the one who has created web.xml to initialize the parameter. If you have got five different parameters then all the parameter names will be available through this Enumeration&lt;String&gt;.<\/p>\r\n&nbsp;\r\n\r\npublic String getInitParameter(String name)\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify;\">Once you know the name of the parameters you can fetch the value of the parameter by using this method. these values are set on the ServletContext.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\">Now it will look for the entries of &lt;servlet&gt;. For each servlet class it creates an object. But before it creates the object of the servlet class it creates an object of ServletConfig. We have got an another interface called ServletConfig. ServletConfig is specific to the Servlet. if we have got five different entries for the &lt;servlet&gt; it has to create five different servlet objects, corresponding to this each &lt;servlet&gt; entries it would be also creating the ServletConfig and each ServletConfig will have ServletContext available to them.<\/p>\r\n&nbsp;\r\n<p class=\"hanging-indent\" style=\"text-align: justify;\"><strong style=\"text-align: initial; font-size: 1em;\">Servlet Config<\/strong><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">ServletConfig has all the information related to the ServletContext and Servlet.<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">ServletConfig methods:<\/span><\/p>\r\n\r\n<\/div>\r\n<div style=\"text-align: justify;\">\r\n<ol>\r\n \t<li style=\"text-align: justify;\">public ServletContext getServletContext() - returns the ServletContext<\/li>\r\n \t<li style=\"text-align: justify;\">public String getServletName() - returns the name of the servlet entry.<\/li>\r\n \t<li style=\"text-align: justify;\">public Enumeration&lt;String&gt; getInitParameterNames() - returns the enumeration of string.<\/li>\r\n \t<li style=\"text-align: justify;\">public String getInitParameter(String name) - returns the value of the paratmeter.<\/li>\r\n<\/ol>\r\n&nbsp;\r\n<p style=\"text-align: justify;\">Then life of the Servlet starts. It begins with the method init. During this init the object of ServletConfig is injected into the Servlet object. ServletContext and ServletConfig are the objects coming from the container. These are the interfaces implemented by the server vendor and not by the developer. The web developer is creating the component called Servlet. in init ServletConfig is made available to the Servlet. ServletContext and ServletConfig are made available to the web developer.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\">init is able to give web developer access to the web container. It is the ServletContext through which the developer is getting access to the entire application. Once the initialization of all the &lt;servlet&gt; entries is done and ServletContext and ServletConfig all of them is getting initialized. Once all the initialization, the one mentioned in deployment descriptor (web.xml file) and other components is created it is then that too corresponding to all the web applications which are on the server it is then that the container now becomes ready to start accepting request from the client.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\">So we have a huge start up phase and it is during this start up phase of the web container the init method is getting invoked and all the servlets are ready with them and initialized. Once the container is ready and any client using the url hits the container, the container looks up for the &lt;url-pattern&gt; in the &lt;servlet-mapping&gt; entry. If it matches any of the &lt;url-pattern&gt; it is able to identify the servlet. After identifying the servlet it will be able to invoke the life-cycle method of the servlet called service.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\">The service method requires the ServletRequest and ServletResponse object as a parameter. These are not the objects of the developer these are the objects of the server vendor. The request arrives using the http protocol. It is the container who knows the http protocol and not the application developer who is having no knowledge of the http protocol. Container is the one who is able to understand the request coming to any of the protocol(Http, SIP etc...). So whenever the request arrives two things should be done by the container:<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\">The container has to create two objects here :<\/p>\r\n&nbsp;\r\n\r\n-\u00a0 ServletRequest object\r\n\r\n-\u00a0 ServletResponse object\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify;\">Considering the http protocol being used here, we would have a ServletRequest object which would be created. It would have nothing but the information which has come as the part of the http protocol. All that information would be encapsulated in the ServletRequest.<\/p>\r\n&nbsp;\r\n<p class=\"hanging-indent\" style=\"text-align: justify;\"><strong>Servlet Response<\/strong><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: initial;\">The other object i.e. ServletResponse object get created by allocating a buffer. The container is allocating a blank buffer, encapsulating it in an object of ServletResponse, these two objects are then going to be made available to the Servlet. The web developer is not going to use http protocol to send his response back but he has to fill in the content into the blank buffer. Whatever content he puts into the buffer it is the containers responsibility to see that the content in the buffer, once the service method is over, to send the content back to the client.<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">The content is generated by the servlet and put into a buffer and sending it back to the client is the responsibility of the container and servlet has no role there. The servlet has nothing to do with how http works. Before sending the content back, the developer may like to see the content received from the client if any, and for that we have the ServletRequest object.. To fill in the buffer we have methods in the ServletResponse object, and to fetch the information from the request, we have methods in the ServletRequest object.<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">ServletResponse methods as follows:<\/span><\/p>\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; text-indent: 2em; font-size: 1em;\">public int getBufferSize()<\/span><\/p>\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">public void setBufferSize(int bufsize)<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">These are the two methods to set and get the buffer size. Before putting the content into the buffer we would like to specify the type of the content and set the type of the content. So for that we have the method:<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">public String getContentType()<\/span><\/p>\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">public void setContentType(String mimeType) most commonly set to \u201ctext\/html\u201d example:<\/span><\/p>\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">response.setContentType(\u201ctext\/html\u201d);<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">If you are interested you can even specify the length there but not compulsory. We have some mechanism to fill in the buffer with the content. What happens in case the buffer is full. It is not an error if you exceed your buffer size but the partial content will be sent to the client. We have various settings in the ServletResponse like the content-length, character-encoding, like I am sending the text content, so what is the character encoding I am using here like UTF-8 etc.. So we have methods for character encoding:<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">public void setCharacterEncoding(String csn) public String getCharacterEncoding()<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">After filling part of the buffer I may feel, I made some mistake and I would like to change the content type and few other things. These are the meta information made available in the ServletResponse about the content. If the buffer is not full you are free to make these changes like change in character encoding, content type etc. But if the buffer is full then the metadata information has been moved out it has already been sent to client. So the client is now aware that the content coming is so and so. like html and not the xml\/pdf\/image and this is how it commits to the client by sending the information about the content to the client. In such a stage the state of the response has changes, the response is set to the committed state. once a response is committed we cannot make any changes to metadata information like content type, character encoding etc. So, if th buffer is full the content will move out\u00a0<\/span><span style=\"font-size: 1em; text-align: initial;\">and no more changes will be allowed. We do have the method to check that:<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"text-align: justify; font-size: 1em;\">public boolean isCommitted() in the servlet response- returns boolean.<\/span><\/p>\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: initial;\">public void flushBuffer() - the response will be committed in this case.<\/span><\/p>\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: initial;\">public void reset() - will reset the buffer content as well as metadata information will also be reset.<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">public void resetBuffer() - will reset only buffer.<\/span><span style=\"font-size: 1em;\">reset and resetBuffer will not work if the response is already committed. We want to set the content type and the content say binary content or it is a text content. If it is a binary content it will be more appropriate to have an OutputStream to write the binary content into the buffer. You will have two kinds of stream to write. A binary stream or the text stream. You can have the binary stream with the help of method called getOutputStream() - return us a servlet output stream which is the subclass of the OutputStream or you can use a method called getWriter() - return PrintWriter.<\/span><\/p>\r\n&nbsp;\r\n\r\n<span style=\"font-size: 1em; text-align: initial;\">public ServletOutputStream getOutputStream()<\/span>\r\n\r\n<span style=\"font-size: 1em; text-align: initial;\">public PrintWriter getWriter()<\/span>\r\n\r\n&nbsp;\r\n\r\n<span style=\"font-size: 1em; text-align: justify;\">So, according to the content type you need to select the stream.<\/span>\r\n\r\n&nbsp;\r\n<p class=\"hanging-indent\"><strong style=\"text-align: initial; text-indent: -1em; font-size: 1em;\">Servlet Request<\/strong><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: justify;\">As part of a request we might get some parameters say for example some form has been submitted then we have &lt;input&gt; tag in the html. In &lt;input&gt; tag we have a name, so that name of an input tag and the form is submitted that is coming as an request to the server. So as part of ServletRequest, a ServletRequest may have parameter name and the corresponding value. So if there is an html form being filled out, the name becomes the parameter name and the value corresponding to the parameter name is the parameter value.<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: justify;\">So any number of parameter values can be there.<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: justify;\">In ServletRequest we have methods for fetching those parameters.<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: initial;\">public Enumeration&lt;String&gt; getParameterNames() - returns enumeration of string.<\/span><\/p>\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: initial;\">public String[] getParameterValues(String pname) - returns an array of string, it can have multiple values.<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: justify;\">The name of parameter can be same with multiple values. For single value we have the method:<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: initial;\">public String getParameterValue(String pname) - returns the single value and it will give you the first value.<\/span><\/p>\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: justify;\">public Map&lt;String, String[]&gt; getParameterMap() - returns the mapping of parameter name and the corresponding values in the string array.<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: justify;\">ServletRequest can have even content coming from it. let us say, you are having an attachment that means client is sending attachment to the server. We have method to fetch the content submitted by the client to the server and also the type of the content by using method:<\/span><\/p>\r\n&nbsp;\r\n\r\n<span style=\"font-size: 1em; text-align: initial;\">public String getContentType()<\/span>\r\n\r\n&nbsp;\r\n\r\n<span style=\"font-size: 1em; text-align: initial;\">and\u00a0 to know the length of the content we have method:<\/span>\r\n\r\n&nbsp;\r\n\r\n<span style=\"font-size: 1em; text-align: initial;\">public int getContentLength()<\/span>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: justify;\">If you want to read the content then we can use the stream. If it is a binary content we can make use of ServletInputStream, which we can get using method<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: initial;\">public ServletInputStream getInputStream() and we can use the method:<\/span><\/p>\r\n<span style=\"font-size: 1em; text-align: initial;\">public BufferedReader getReader()<\/span>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: justify;\">which will return the object of the BufferedReader. BufferedReader has the method called readLine() which reads the content line by line. ServletRequest will also have the information like what are preferences of the user in terms of the locale he likes using the method called getLocale()<\/span><\/p>\r\n&nbsp;\r\n\r\n<span style=\"font-size: 1em; text-align: initial;\">public Locale getLocale()<\/span>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: justify;\">to give us the language and country preference. We have seen only three methods of the Servlet called init(), service() and destroy().<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: justify;\">In Servlet\u2019s init method, whatever the ServletConfig is injected, there is a method called getServletConfig()<\/span><\/p>\r\n&nbsp;\r\n\r\n<span style=\"font-size: 1em; text-align: initial;\">public ServletConfig getServletConfig()<\/span>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: justify;\">to know the ServletConfig made available to the Servlet. We keep one instance variable which is initialised in the init method. Servlet has also another method:<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: initial;\">public String getServletInfo() - implement it to return some copyright message. So servlet has five methods :<\/span><\/p>\r\n<span style=\"font-size: 1em; text-align: initial;\">p<\/span><span style=\"font-size: 1em; text-align: initial;\">ublic void init(ServletConfig cfg) throws ServletException public void destroy()<\/span>\r\n\r\n<span style=\"font-size: 1em; text-align: initial;\">public\u00a0 \u00a0void\u00a0 \u00a0service(ServletRequest\u00a0 \u00a0request,\u00a0 \u00a0ServletResponse\u00a0 \u00a0response)\u00a0 \u00a0throws\u00a0 \u00a0IOException,<\/span>\r\n\r\n&nbsp;\r\n\r\n<span style=\"font-size: 1em; text-align: initial;\">ServletException<\/span>\r\n\r\n<span style=\"font-size: 1em; text-align: initial;\">public ServletConfig getServletConfig() public String getServletInfo()<\/span>\r\n\r\n&nbsp;\r\n<p class=\"hanging-indent\"><strong style=\"text-align: justify; text-indent: -1em; font-size: 1em;\"><span style=\"text-align: initial; font-size: 1em;\">As a first exercise:<\/span><\/strong><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: justify;\">We will write a servlet and deploy it to see what happens. we will give fix response here. Let us define this servlet class by the name HelloServlet under the package org.epgpathshala.<\/span><\/p>\r\n&nbsp;\r\n\r\n<span style=\"font-size: 1em; text-align: initial;\">package org.epgpathshala; import javax.servlet.*;<\/span>\r\n\r\n<span style=\"font-size: 1em; text-align: initial;\">import java.io.servlet.*;<\/span>\r\n\r\n<span style=\"font-size: 1em; text-align: initial;\">public class HelloServlet implements Servlet {<\/span>\r\n\r\n<span style=\"font-size: 1em; text-align: initial;\">private ServletConfig config;<\/span>\r\n\r\n<span style=\"font-size: 1em; text-align: initial;\">public void init(ServletConfig cfg)\u00a0\u00a0\u00a0\u00a0 { this.config = cfg;<\/span>\r\n\r\n<span style=\"font-size: 1em; text-align: initial;\">}<\/span>\r\n\r\n<span style=\"font-size: 1em; text-align: initial;\">public void destroy() {<\/span>\r\n\r\n<span style=\"font-size: 1em; text-align: initial;\">}<\/span>\r\n\r\n<span style=\"font-size: 1em; text-align: initial;\">public ServletConfig getServletConfig() { return this.config;<\/span>\r\n\r\n<span style=\"font-size: 1em; text-align: initial;\">}<\/span>\r\n\r\n<span style=\"font-size: 1em; text-align: initial;\">public String getServletInfo() {<\/span>\r\n\r\n<span style=\"font-size: 1em; text-align: initial;\">return \" HelloServlet (C)opuright epgpathshala\";<\/span>\r\n\r\n<span style=\"font-size: 1em; text-align: initial;\">}<\/span>\r\n\r\n<span style=\"font-size: 1em; text-align: initial;\">public void service(ServletRequest req, ServletResponse res) throws IOException, ServletException {<\/span>\r\n\r\n<span style=\"font-size: 1em; text-align: initial;\">res.setContentType(\"text\/html\"); PrintWriter out = res.getWriter();<\/span>\r\n\r\n<span style=\"font-size: 1em; text-align: initial;\">out.println(\"&lt;html&gt;&lt;head&gt;&lt;title&gt;Hello \u00a0Servlet&lt;\/title&gt;&lt;\/head&gt;\"); out.println(\"&lt;body&gt;&lt;h1&gt;Hello World!&lt;\/h1&gt;&lt;\/body&gt;&lt;\/html&gt;\");<\/span>\r\n\r\n<span style=\"font-size: 1em; text-align: initial;\">}<\/span>\r\n\r\n<span style=\"font-size: 1em; text-align: initial;\">}<\/span>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: justify;\">Once we have finished implementing the Servlet, what we need to do here is compile this and put this under the classes folder of our web application folder. While compiling give -d to specify the classes folder.<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: justify;\">If we call this application as sample we will have a folder called sample within which we will have a WEB-INF folder, where we have a classes folder. So within classes package folder will be created and the classes will be available. We now need to create the web.xml file. The content of web.xml can be as given below:<\/span><\/p>\r\n&nbsp;\r\n\r\n<span style=\"text-align: initial; font-size: 1em;\">&lt;web-app&gt;<\/span>\r\n\r\n<span style=\"text-align: initial; font-size: 1em;\">&lt;display-name&gt;Sample&lt;\/display-name&gt;<\/span>\r\n\r\n<span style=\"text-align: initial; font-size: 1em;\">&lt;servlet&gt;<\/span>\r\n\r\n<span style=\"text-align: initial; font-size: 1em;\">&lt;servlet-name&gt;Hello&lt;\/servlet-name&gt;<\/span>\r\n\r\n<span style=\"text-align: initial; font-size: 1em;\">&lt;servlet-class&gt;org.epgPathshala.HelloServlet&lt;\/servlet-class&gt;<\/span>\r\n\r\n<span style=\"text-align: initial; font-size: 1em;\">&lt;\/servlet&gt;<\/span>\r\n\r\n<span style=\"text-align: initial; font-size: 1em;\">&lt;servlet&gt;<\/span>\r\n\r\n<span style=\"text-align: initial; font-size: 1em;\">&lt;servlet-name&gt;Hi&lt;\/servlet-name&gt;<\/span>\r\n\r\n<span style=\"font-size: 1em; text-align: justify;\">&lt;servlet-class&gt;org.epgPathshala.HelloServlet\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &lt;\/servlet-class&gt;<\/span>\r\n\r\n<span style=\"text-align: justify; font-size: 1em;\">&lt;\/servlet&gt;<\/span>\r\n\r\n<span style=\"text-align: justify; font-size: 1em;\">&lt;servlet-mapping&gt;<\/span>\r\n\r\n<span style=\"text-align: justify; font-size: 1em;\">&lt;servlet-name&gt;Hello&lt;\/servlet-name&gt;<\/span>\r\n\r\n<span style=\"text-align: justify; font-size: 1em;\">&lt;url-pattern&gt;\/Hello&lt;\/url-pattern&gt;<\/span>\r\n\r\n<span style=\"text-align: justify; font-size: 1em;\">&lt;\/servlet-mapping&gt;<\/span>\r\n\r\n<span style=\"text-align: justify; font-size: 1em;\">&lt;servlet-mapping&gt;<\/span>\r\n\r\n<span style=\"text-align: justify; font-size: 1em;\">&lt;servlet-name&gt;Hi&lt;\/servlet-name&gt;<\/span>\r\n\r\n<span style=\"text-align: justify; font-size: 1em;\">&lt;url-pattern&gt;\/hi\/*&lt;\/url-pattern&gt;<\/span>\r\n\r\n<span style=\"text-align: justify; font-size: 1em;\">&lt;\/servlet-mapping&gt;<\/span>\r\n\r\n<span style=\"text-align: justify; font-size: 1em;\">&lt;servlet-mapping&gt;<\/span>\r\n\r\n<span style=\"text-align: justify; font-size: 1em;\">&lt;servlet-name&gt;Hello&lt;\/servlet-name&gt;<\/span>\r\n\r\n<span style=\"text-align: justify; font-size: 1em;\">&lt;url-pattern&gt;*.hello&lt;\/url-pattern&gt;<\/span>\r\n\r\n<span style=\"text-align: justify; font-size: 1em;\">&lt;\/servlet-mapping&gt;<\/span>\r\n\r\n<span style=\"text-align: justify; font-size: 1em;\">&lt;\/web-app&gt;<\/span>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"text-align: justify; font-size: 1em;\">now deploying this sample application. Within tomcat there is a folder called webapps and put it into it and restart the tomcat. Now, from a browser try the following Url.<\/span><\/p>\r\n&nbsp;\r\n\r\n<span style=\"text-align: justify; font-size: 1em;\">http:\/\/localhost:8080\/sample\/Hello<\/span>\r\n\r\n<span style=\"text-align: justify; font-size: 1em;\">or http:\/\/localhost:8080\/sample\/hi\/anypath or<\/span>\r\n\r\n<span style=\"text-align: justify; font-size: 1em;\">http:\/\/localhost:8080\/sample\/somepath.hello<\/span>\r\n\r\n<table>\r\n<tbody>\r\n<tr>\r\n<td><strong>you can view video on Basic Servlet API<\/strong><\/td>\r\n<td><a href=\"https:\/\/youtu.be\/MUP5aKSimGI\" target=\"_blank\" rel=\"noopener\"><img class=\"alignnone wp-image-120\" src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"\" width=\"36\" height=\"36\" \/><\/a><\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n<p class=\"hanging-indent\"><strong style=\"text-align: justify; font-size: 1em;\">Suggested Reading:<\/strong><\/p>\r\n\r\n<\/div>\r\n<div class=\"textLayer\">\r\n<ol>\r\n \t<li style=\"text-align: justify;\">Core Servlets and Java Server Pages Volume1 by Marty Hall &amp; Larry Brown, Second Edition, Pearson Education.<\/li>\r\n \t<li style=\"text-align: justify;\">Inside Servlets by Dustin R Callaway, Pearson Education.<\/li>\r\n \t<li style=\"text-align: justify;\">Java Server Programming for Professionals by Ivan Bayross, Sharanam Shah, Cynhthia Bayross and Vaishali Shah Shroff Publishers and Distributros.<\/li>\r\n \t<li style=\"text-align: justify;\">http:\/\/download.oracle.com\/otndocs\/jcp\/servlet-3.0-fr-oth-JSpec\/<\/li>\r\n<\/ol>\r\n<\/div>\r\n<div class=\"annotationLayer\"><\/div>","rendered":"<div><span style=\"float: right;\"><a href=\"https:\/\/youtu.be\/MUP5aKSimGI\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"epgp books\" width=\"75px\" height=\"75px;\" \/><\/a><br \/>\n<\/span><\/div>\n<div style=\"text-align: justify;\">\n<p>&nbsp;<\/p>\n<p><strong>Introduction<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">In this module we will look at the Servlet interface as a whole and the corresponding interface which are used from Servlet, which are init method in the servlet life cycle, so we have the ServletConfig object being injected in the Servlet object. So, ServletConfig methods would be explored. ServletConfig makes available to us the ServletContext object. ServletContext methods are useful giving us some information in web.xml file. So how to fetch the information from web.xml, and we will also explore ServletRequest and ServletResponse object. And finally we will create our first Servlet which will be used for giving very simple response called &#8220;HelloWorld&#8221;. So, we would like to create some HTML output which will generate a \u201cHello world\u201d.<\/p>\n<p>&nbsp;<\/p>\n<p class=\"hanging-indent\"><strong>Quick Review of Servlet life cycle. <\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>Three basic methods in servlet life cycle:<\/p>\n<p>&nbsp;<\/p>\n<p>public void init(ServletConfig cfg)<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">As soon as the web container starts, it identifies the applications. Per application it creates a ServletContext Object. At the time of creating ServletContext object it is going through web.xml file and it get the display name from web.xml, which is going to be available through ServletContext object because this is one per application.<\/p>\n<p>&nbsp;<\/p>\n<p class=\"hanging-indent\" style=\"text-align: justify;\"><strong>Servlet Context<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">The ServletContext is nothing but your web application. So the display name can be known using the getServletContextName() method.<\/p>\n<p>&nbsp;<\/p>\n<p>public String getServletContextName()<\/p>\n<p>public Enumeration&lt;String&gt; getInitParameterNames()<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">it is used to get the context-param name which is set by the one who has created web.xml to initialize the parameter. If you have got five different parameters then all the parameter names will be available through this Enumeration&lt;String&gt;.<\/p>\n<p>&nbsp;<\/p>\n<p>public String getInitParameter(String name)<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">Once you know the name of the parameters you can fetch the value of the parameter by using this method. these values are set on the ServletContext.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">Now it will look for the entries of &lt;servlet&gt;. For each servlet class it creates an object. But before it creates the object of the servlet class it creates an object of ServletConfig. We have got an another interface called ServletConfig. ServletConfig is specific to the Servlet. if we have got five different entries for the &lt;servlet&gt; it has to create five different servlet objects, corresponding to this each &lt;servlet&gt; entries it would be also creating the ServletConfig and each ServletConfig will have ServletContext available to them.<\/p>\n<p>&nbsp;<\/p>\n<p class=\"hanging-indent\" style=\"text-align: justify;\"><strong style=\"text-align: initial; font-size: 1em;\">Servlet Config<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">ServletConfig has all the information related to the ServletContext and Servlet.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">ServletConfig methods:<\/span><\/p>\n<\/div>\n<div style=\"text-align: justify;\">\n<ol>\n<li style=\"text-align: justify;\">public ServletContext getServletContext() &#8211; returns the ServletContext<\/li>\n<li style=\"text-align: justify;\">public String getServletName() &#8211; returns the name of the servlet entry.<\/li>\n<li style=\"text-align: justify;\">public Enumeration&lt;String&gt; getInitParameterNames() &#8211; returns the enumeration of string.<\/li>\n<li style=\"text-align: justify;\">public String getInitParameter(String name) &#8211; returns the value of the paratmeter.<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">Then life of the Servlet starts. It begins with the method init. During this init the object of ServletConfig is injected into the Servlet object. ServletContext and ServletConfig are the objects coming from the container. These are the interfaces implemented by the server vendor and not by the developer. The web developer is creating the component called Servlet. in init ServletConfig is made available to the Servlet. ServletContext and ServletConfig are made available to the web developer.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">init is able to give web developer access to the web container. It is the ServletContext through which the developer is getting access to the entire application. Once the initialization of all the &lt;servlet&gt; entries is done and ServletContext and ServletConfig all of them is getting initialized. Once all the initialization, the one mentioned in deployment descriptor (web.xml file) and other components is created it is then that too corresponding to all the web applications which are on the server it is then that the container now becomes ready to start accepting request from the client.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">So we have a huge start up phase and it is during this start up phase of the web container the init method is getting invoked and all the servlets are ready with them and initialized. Once the container is ready and any client using the url hits the container, the container looks up for the &lt;url-pattern&gt; in the &lt;servlet-mapping&gt; entry. If it matches any of the &lt;url-pattern&gt; it is able to identify the servlet. After identifying the servlet it will be able to invoke the life-cycle method of the servlet called service.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">The service method requires the ServletRequest and ServletResponse object as a parameter. These are not the objects of the developer these are the objects of the server vendor. The request arrives using the http protocol. It is the container who knows the http protocol and not the application developer who is having no knowledge of the http protocol. Container is the one who is able to understand the request coming to any of the protocol(Http, SIP etc&#8230;). So whenever the request arrives two things should be done by the container:<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">The container has to create two objects here :<\/p>\n<p>&nbsp;<\/p>\n<p>&#8211;\u00a0 ServletRequest object<\/p>\n<p>&#8211;\u00a0 ServletResponse object<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">Considering the http protocol being used here, we would have a ServletRequest object which would be created. It would have nothing but the information which has come as the part of the http protocol. All that information would be encapsulated in the ServletRequest.<\/p>\n<p>&nbsp;<\/p>\n<p class=\"hanging-indent\" style=\"text-align: justify;\"><strong>Servlet Response<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: initial;\">The other object i.e. ServletResponse object get created by allocating a buffer. The container is allocating a blank buffer, encapsulating it in an object of ServletResponse, these two objects are then going to be made available to the Servlet. The web developer is not going to use http protocol to send his response back but he has to fill in the content into the blank buffer. Whatever content he puts into the buffer it is the containers responsibility to see that the content in the buffer, once the service method is over, to send the content back to the client.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">The content is generated by the servlet and put into a buffer and sending it back to the client is the responsibility of the container and servlet has no role there. The servlet has nothing to do with how http works. Before sending the content back, the developer may like to see the content received from the client if any, and for that we have the ServletRequest object.. To fill in the buffer we have methods in the ServletResponse object, and to fetch the information from the request, we have methods in the ServletRequest object.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">ServletResponse methods as follows:<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; text-indent: 2em; font-size: 1em;\">public int getBufferSize()<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">public void setBufferSize(int bufsize)<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">These are the two methods to set and get the buffer size. Before putting the content into the buffer we would like to specify the type of the content and set the type of the content. So for that we have the method:<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">public String getContentType()<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">public void setContentType(String mimeType) most commonly set to \u201ctext\/html\u201d example:<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">response.setContentType(\u201ctext\/html\u201d);<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">If you are interested you can even specify the length there but not compulsory. We have some mechanism to fill in the buffer with the content. What happens in case the buffer is full. It is not an error if you exceed your buffer size but the partial content will be sent to the client. We have various settings in the ServletResponse like the content-length, character-encoding, like I am sending the text content, so what is the character encoding I am using here like UTF-8 etc.. So we have methods for character encoding:<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">public void setCharacterEncoding(String csn) public String getCharacterEncoding()<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">After filling part of the buffer I may feel, I made some mistake and I would like to change the content type and few other things. These are the meta information made available in the ServletResponse about the content. If the buffer is not full you are free to make these changes like change in character encoding, content type etc. But if the buffer is full then the metadata information has been moved out it has already been sent to client. So the client is now aware that the content coming is so and so. like html and not the xml\/pdf\/image and this is how it commits to the client by sending the information about the content to the client. In such a stage the state of the response has changes, the response is set to the committed state. once a response is committed we cannot make any changes to metadata information like content type, character encoding etc. So, if th buffer is full the content will move out\u00a0<\/span><span style=\"font-size: 1em; text-align: initial;\">and no more changes will be allowed. We do have the method to check that:<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: justify; font-size: 1em;\">public boolean isCommitted() in the servlet response- returns boolean.<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: initial;\">public void flushBuffer() &#8211; the response will be committed in this case.<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: initial;\">public void reset() &#8211; will reset the buffer content as well as metadata information will also be reset.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">public void resetBuffer() &#8211; will reset only buffer.<\/span><span style=\"font-size: 1em;\">reset and resetBuffer will not work if the response is already committed. We want to set the content type and the content say binary content or it is a text content. If it is a binary content it will be more appropriate to have an OutputStream to write the binary content into the buffer. You will have two kinds of stream to write. A binary stream or the text stream. You can have the binary stream with the help of method called getOutputStream() &#8211; return us a servlet output stream which is the subclass of the OutputStream or you can use a method called getWriter() &#8211; return PrintWriter.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-size: 1em; text-align: initial;\">public ServletOutputStream getOutputStream()<\/span><\/p>\n<p><span style=\"font-size: 1em; text-align: initial;\">public PrintWriter getWriter()<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-size: 1em; text-align: justify;\">So, according to the content type you need to select the stream.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p class=\"hanging-indent\"><strong style=\"text-align: initial; text-indent: -1em; font-size: 1em;\">Servlet Request<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: justify;\">As part of a request we might get some parameters say for example some form has been submitted then we have &lt;input&gt; tag in the html. In &lt;input&gt; tag we have a name, so that name of an input tag and the form is submitted that is coming as an request to the server. So as part of ServletRequest, a ServletRequest may have parameter name and the corresponding value. So if there is an html form being filled out, the name becomes the parameter name and the value corresponding to the parameter name is the parameter value.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: justify;\">So any number of parameter values can be there.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: justify;\">In ServletRequest we have methods for fetching those parameters.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: initial;\">public Enumeration&lt;String&gt; getParameterNames() &#8211; returns enumeration of string.<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: initial;\">public String[] getParameterValues(String pname) &#8211; returns an array of string, it can have multiple values.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: justify;\">The name of parameter can be same with multiple values. For single value we have the method:<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: initial;\">public String getParameterValue(String pname) &#8211; returns the single value and it will give you the first value.<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: justify;\">public Map&lt;String, String[]&gt; getParameterMap() &#8211; returns the mapping of parameter name and the corresponding values in the string array.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: justify;\">ServletRequest can have even content coming from it. let us say, you are having an attachment that means client is sending attachment to the server. We have method to fetch the content submitted by the client to the server and also the type of the content by using method:<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-size: 1em; text-align: initial;\">public String getContentType()<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-size: 1em; text-align: initial;\">and\u00a0 to know the length of the content we have method:<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-size: 1em; text-align: initial;\">public int getContentLength()<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: justify;\">If you want to read the content then we can use the stream. If it is a binary content we can make use of ServletInputStream, which we can get using method<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: initial;\">public ServletInputStream getInputStream() and we can use the method:<\/span><\/p>\n<p><span style=\"font-size: 1em; text-align: initial;\">public BufferedReader getReader()<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: justify;\">which will return the object of the BufferedReader. BufferedReader has the method called readLine() which reads the content line by line. ServletRequest will also have the information like what are preferences of the user in terms of the locale he likes using the method called getLocale()<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-size: 1em; text-align: initial;\">public Locale getLocale()<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: justify;\">to give us the language and country preference. We have seen only three methods of the Servlet called init(), service() and destroy().<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: justify;\">In Servlet\u2019s init method, whatever the ServletConfig is injected, there is a method called getServletConfig()<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-size: 1em; text-align: initial;\">public ServletConfig getServletConfig()<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: justify;\">to know the ServletConfig made available to the Servlet. We keep one instance variable which is initialised in the init method. Servlet has also another method:<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: initial;\">public String getServletInfo() &#8211; implement it to return some copyright message. So servlet has five methods :<\/span><\/p>\n<p><span style=\"font-size: 1em; text-align: initial;\">p<\/span><span style=\"font-size: 1em; text-align: initial;\">ublic void init(ServletConfig cfg) throws ServletException public void destroy()<\/span><\/p>\n<p><span style=\"font-size: 1em; text-align: initial;\">public\u00a0 \u00a0void\u00a0 \u00a0service(ServletRequest\u00a0 \u00a0request,\u00a0 \u00a0ServletResponse\u00a0 \u00a0response)\u00a0 \u00a0throws\u00a0 \u00a0IOException,<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-size: 1em; text-align: initial;\">ServletException<\/span><\/p>\n<p><span style=\"font-size: 1em; text-align: initial;\">public ServletConfig getServletConfig() public String getServletInfo()<\/span><\/p>\n<p>&nbsp;<\/p>\n<p class=\"hanging-indent\"><strong style=\"text-align: justify; text-indent: -1em; font-size: 1em;\"><span style=\"text-align: initial; font-size: 1em;\">As a first exercise:<\/span><\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: justify;\">We will write a servlet and deploy it to see what happens. we will give fix response here. Let us define this servlet class by the name HelloServlet under the package org.epgpathshala.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-size: 1em; text-align: initial;\">package org.epgpathshala; import javax.servlet.*;<\/span><\/p>\n<p><span style=\"font-size: 1em; text-align: initial;\">import java.io.servlet.*;<\/span><\/p>\n<p><span style=\"font-size: 1em; text-align: initial;\">public class HelloServlet implements Servlet {<\/span><\/p>\n<p><span style=\"font-size: 1em; text-align: initial;\">private ServletConfig config;<\/span><\/p>\n<p><span style=\"font-size: 1em; text-align: initial;\">public void init(ServletConfig cfg)\u00a0\u00a0\u00a0\u00a0 { this.config = cfg;<\/span><\/p>\n<p><span style=\"font-size: 1em; text-align: initial;\">}<\/span><\/p>\n<p><span style=\"font-size: 1em; text-align: initial;\">public void destroy() {<\/span><\/p>\n<p><span style=\"font-size: 1em; text-align: initial;\">}<\/span><\/p>\n<p><span style=\"font-size: 1em; text-align: initial;\">public ServletConfig getServletConfig() { return this.config;<\/span><\/p>\n<p><span style=\"font-size: 1em; text-align: initial;\">}<\/span><\/p>\n<p><span style=\"font-size: 1em; text-align: initial;\">public String getServletInfo() {<\/span><\/p>\n<p><span style=\"font-size: 1em; text-align: initial;\">return &#8221; HelloServlet (C)opuright epgpathshala&#8221;;<\/span><\/p>\n<p><span style=\"font-size: 1em; text-align: initial;\">}<\/span><\/p>\n<p><span style=\"font-size: 1em; text-align: initial;\">public void service(ServletRequest req, ServletResponse res) throws IOException, ServletException {<\/span><\/p>\n<p><span style=\"font-size: 1em; text-align: initial;\">res.setContentType(&#8220;text\/html&#8221;); PrintWriter out = res.getWriter();<\/span><\/p>\n<p><span style=\"font-size: 1em; text-align: initial;\">out.println(&#8220;&lt;html&gt;&lt;head&gt;&lt;title&gt;Hello \u00a0Servlet&lt;\/title&gt;&lt;\/head&gt;&#8221;); out.println(&#8220;&lt;body&gt;&lt;h1&gt;Hello World!&lt;\/h1&gt;&lt;\/body&gt;&lt;\/html&gt;&#8221;);<\/span><\/p>\n<p><span style=\"font-size: 1em; text-align: initial;\">}<\/span><\/p>\n<p><span style=\"font-size: 1em; text-align: initial;\">}<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: justify;\">Once we have finished implementing the Servlet, what we need to do here is compile this and put this under the classes folder of our web application folder. While compiling give -d to specify the classes folder.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: justify;\">If we call this application as sample we will have a folder called sample within which we will have a WEB-INF folder, where we have a classes folder. So within classes package folder will be created and the classes will be available. We now need to create the web.xml file. The content of web.xml can be as given below:<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"text-align: initial; font-size: 1em;\">&lt;web-app&gt;<\/span><\/p>\n<p><span style=\"text-align: initial; font-size: 1em;\">&lt;display-name&gt;Sample&lt;\/display-name&gt;<\/span><\/p>\n<p><span style=\"text-align: initial; font-size: 1em;\">&lt;servlet&gt;<\/span><\/p>\n<p><span style=\"text-align: initial; font-size: 1em;\">&lt;servlet-name&gt;Hello&lt;\/servlet-name&gt;<\/span><\/p>\n<p><span style=\"text-align: initial; font-size: 1em;\">&lt;servlet-class&gt;org.epgPathshala.HelloServlet&lt;\/servlet-class&gt;<\/span><\/p>\n<p><span style=\"text-align: initial; font-size: 1em;\">&lt;\/servlet&gt;<\/span><\/p>\n<p><span style=\"text-align: initial; font-size: 1em;\">&lt;servlet&gt;<\/span><\/p>\n<p><span style=\"text-align: initial; font-size: 1em;\">&lt;servlet-name&gt;Hi&lt;\/servlet-name&gt;<\/span><\/p>\n<p><span style=\"font-size: 1em; text-align: justify;\">&lt;servlet-class&gt;org.epgPathshala.HelloServlet\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &lt;\/servlet-class&gt;<\/span><\/p>\n<p><span style=\"text-align: justify; font-size: 1em;\">&lt;\/servlet&gt;<\/span><\/p>\n<p><span style=\"text-align: justify; font-size: 1em;\">&lt;servlet-mapping&gt;<\/span><\/p>\n<p><span style=\"text-align: justify; font-size: 1em;\">&lt;servlet-name&gt;Hello&lt;\/servlet-name&gt;<\/span><\/p>\n<p><span style=\"text-align: justify; font-size: 1em;\">&lt;url-pattern&gt;\/Hello&lt;\/url-pattern&gt;<\/span><\/p>\n<p><span style=\"text-align: justify; font-size: 1em;\">&lt;\/servlet-mapping&gt;<\/span><\/p>\n<p><span style=\"text-align: justify; font-size: 1em;\">&lt;servlet-mapping&gt;<\/span><\/p>\n<p><span style=\"text-align: justify; font-size: 1em;\">&lt;servlet-name&gt;Hi&lt;\/servlet-name&gt;<\/span><\/p>\n<p><span style=\"text-align: justify; font-size: 1em;\">&lt;url-pattern&gt;\/hi\/*&lt;\/url-pattern&gt;<\/span><\/p>\n<p><span style=\"text-align: justify; font-size: 1em;\">&lt;\/servlet-mapping&gt;<\/span><\/p>\n<p><span style=\"text-align: justify; font-size: 1em;\">&lt;servlet-mapping&gt;<\/span><\/p>\n<p><span style=\"text-align: justify; font-size: 1em;\">&lt;servlet-name&gt;Hello&lt;\/servlet-name&gt;<\/span><\/p>\n<p><span style=\"text-align: justify; font-size: 1em;\">&lt;url-pattern&gt;*.hello&lt;\/url-pattern&gt;<\/span><\/p>\n<p><span style=\"text-align: justify; font-size: 1em;\">&lt;\/servlet-mapping&gt;<\/span><\/p>\n<p><span style=\"text-align: justify; font-size: 1em;\">&lt;\/web-app&gt;<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: justify; font-size: 1em;\">now deploying this sample application. Within tomcat there is a folder called webapps and put it into it and restart the tomcat. Now, from a browser try the following Url.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"text-align: justify; font-size: 1em;\">http:\/\/localhost:8080\/sample\/Hello<\/span><\/p>\n<p><span style=\"text-align: justify; font-size: 1em;\">or http:\/\/localhost:8080\/sample\/hi\/anypath or<\/span><\/p>\n<p><span style=\"text-align: justify; font-size: 1em;\">http:\/\/localhost:8080\/sample\/somepath.hello<\/span><\/p>\n<table>\n<tbody>\n<tr>\n<td><strong>you can view video on Basic Servlet API<\/strong><\/td>\n<td><a href=\"https:\/\/youtu.be\/MUP5aKSimGI\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-120\" src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"\" width=\"36\" height=\"36\" \/><\/a><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p class=\"hanging-indent\"><strong style=\"text-align: justify; font-size: 1em;\">Suggested Reading:<\/strong><\/p>\n<\/div>\n<div class=\"textLayer\">\n<ol>\n<li style=\"text-align: justify;\">Core Servlets and Java Server Pages Volume1 by Marty Hall &amp; Larry Brown, Second Edition, Pearson Education.<\/li>\n<li style=\"text-align: justify;\">Inside Servlets by Dustin R Callaway, Pearson Education.<\/li>\n<li style=\"text-align: justify;\">Java Server Programming for Professionals by Ivan Bayross, Sharanam Shah, Cynhthia Bayross and Vaishali Shah Shroff Publishers and Distributros.<\/li>\n<li style=\"text-align: justify;\">http:\/\/download.oracle.com\/otndocs\/jcp\/servlet-3.0-fr-oth-JSpec\/<\/li>\n<\/ol>\n<\/div>\n<div class=\"annotationLayer\"><\/div>\n","protected":false},"author":4,"menu_order":17,"template":"","meta":{"pb_show_title":"on","pb_short_title":"","pb_subtitle":"","pb_authors":["mr-pravin-jain"],"pb_section_license":""},"chapter-type":[],"contributor":[58],"license":[],"class_list":["post-107","chapter","type-chapter","status-publish","hentry","contributor-mr-pravin-jain"],"part":3,"_links":{"self":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-json\/pressbooks\/v2\/chapters\/107","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-json\/pressbooks\/v2\/chapters"}],"about":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-json\/wp\/v2\/types\/chapter"}],"author":[{"embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-json\/wp\/v2\/users\/4"}],"version-history":[{"count":8,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-json\/pressbooks\/v2\/chapters\/107\/revisions"}],"predecessor-version":[{"id":405,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-json\/pressbooks\/v2\/chapters\/107\/revisions\/405"}],"part":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-json\/pressbooks\/v2\/parts\/3"}],"metadata":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-json\/pressbooks\/v2\/chapters\/107\/metadata\/"}],"wp:attachment":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-json\/wp\/v2\/media?parent=107"}],"wp:term":[{"taxonomy":"chapter-type","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-json\/pressbooks\/v2\/chapter-type?post=107"},{"taxonomy":"contributor","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-json\/wp\/v2\/contributor?post=107"},{"taxonomy":"license","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-json\/wp\/v2\/license?post=107"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}