{"id":114,"date":"2018-07-11T06:21:59","date_gmt":"2018-07-11T06:21:59","guid":{"rendered":"http:\/\/itp16.epgpbooks.inflibnet.ac.in\/?post_type=chapter&#038;p=114"},"modified":"2019-05-15T07:20:30","modified_gmt":"2019-05-15T07:20:30","slug":"http-specific-api-for-servlets","status":"publish","type":"chapter","link":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/chapter\/http-specific-api-for-servlets\/","title":{"rendered":"HTTP specific API for Servlets"},"content":{"raw":"<div><span style=\"float: right;\"><a href=\"https:\/\/youtu.be\/yVXicwaLHbA\" 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;\">Welcome to this module on HTTP specific APIs on Servlets. In the earlier module we have seen the general part of API. So far, we have explored the javax.servlet package.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\">Now we are looking at the javax.servlet.http package. As far as the servlet specification is concerned, what it says is, any request-response based protocol is supported by the implementation of servlet specification...But one thing which is mandatory is, Whatever is your servlet container, it must implement and support the HTTP protocol. There as a part of servlet specification itself, we have a package called javax.servlet.http which is talking about specifics of HTTP protocol.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\">Here we have to recollect what we have learnt in HTTP protocol, the various parts of HTTP protocol. From the generic API we have seen the interfaces like ServletRequest, the ServletResponse which were very general. There was nothing specific about HTTP protocol there. But when we talk of javax.servlet.http package, it has additional interfaces. It has an interfaces called<\/p>\r\n&nbsp;\r\n\r\na)\u00a0 HTTPServletRequest\r\n\r\nb)\u00a0 HTTPServletResponse.\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify;\">These are the interfaces which are extending from generic interfaces. The general interfaces were ServletRequest and ServletResponse. Those are being extended to have an API which is specific to HTTP protocol.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\">What exactly happens is whenever the request arrives, since the request which comes is an HTTP request, the container is creating objects which are implementations of HTTPServletRequest and HTTPServletResponse.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\">These are extending the ServletRequest and ServletResponse interfaces. which are from the javax.servlet package.<\/p>\r\n\r\n<\/div>\r\n<div style=\"text-align: justify;\">\r\n<p style=\"text-align: justify;\">So in this module, we will cover these 2 interfaces called HTTPServletRequest and HTTPServletResponse<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\">We also have another class here called HTTPServlet which is nothing but extending from GenericServlet class. So what are all those things which are part of HTTPServletRequest, HTTPServletResponse<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\">What is additional there? \u00a0and things related to class called HTTPServlet.<\/p>\r\n&nbsp;\r\n<p class=\"hanging-indent\"><strong>HttpServletRequest\u00a0<\/strong><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\">Let us begin by understanding what is additional in the HTTPServletRequest? ServletRequest was very general. It just says, oh you could have any parameters. It says you could have content. When it comes to Http, of course HTTP also has that. HTTP can have parameters.<\/p>\r\n&nbsp;\r\n\r\nHow do we have parameters using HTTP? \u00a0There are 2 ways:\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify;\">One way is, as part of URL itself. Someone could put those query string, using a '?' and then parameter_name=value [&amp; (parameter_name=value)] and so on. You could have any number of parameters being specified as part of URL itself. If that is the case the parameter is going to be available in the ServletRequest itself.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\">Another way is, when someone submits a form. In the HTML form, we are able to specify method=post or method=get What happens when someone says method=get? The parameters which are coming from inputs tags become part of URL If the method was specified as POST, the parameters will going as a part of the content rather than going as part of URL. A string similar to the query string becomes the content of the HTTPRequest.<\/p>\r\n&nbsp;\r\n\r\nLet us look at the structure of HTTPRequest.\u00a0 The first ine is typically\r\n\r\nGET resource_or_path protocol_version\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify;\">In the interface called HTTPServletRequest, we have additional methods for these. You might be interested in knowing what is the HTTP method? Whether it is GET or POST or PUT or DELETE etc? we have various HTTP methods.\u00a0<span style=\"text-align: initial; font-size: 1em;\">In HttpServletRequest interface, we have a method called getMethod() which returns a String. This is method from<\/span><\/p>\r\n&nbsp;\r\n<p class=\"hanging-indent\" style=\"text-align: justify;\"><strong style=\"text-align: initial; text-indent: -1em; font-size: 1em;\">HTTPServletRequest.<\/strong><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">You are interested in the resource!!! You have a method called getRequestedURL() which would give us the whole URL as a StringBuffer object. It is a StringBuffer which means it is modifiable. It can be modified before it is getting forwarded to a RequestDispatcher., by modifying it, there could be some parameters that could be added, yes, that could be done.<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">We even have methods to get various parts of URL, This URL from the servlet specification point of view, is<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">Application name as the first part of URL<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">\/application_name\/...<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">so \u00a0you \u00a0are \u00a0interested \u00a0in \u00a0knowing \u00a0the \u00a0application \u00a0name...?\u00a0\u00a0\u00a0 We \u00a0have \u00a0this \u00a0method \u00a0in HTTPServletRequest, called getContextPath().<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">public String getContextPath()<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">that gives us the part which is related to the application, Then we have a method which will be specifying., what part of the URL is identifying the Servlet?<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">There is URL-pattern which is mapped to a Servlet,.So which part of the URL matched the pattern? because of which this invocation has taken place.<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">For that we have a method called getServletPath()<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">public String getServletPath()<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">So you can know the ServletPath which is responsible for the identification \u00a0of the Servlet. and then after the servlet path, for e.g. you have that wildcard \/* at the end.<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">So whatever is there after \/*, if there is something in current URL which matches to it, that could also be identified separately by method called by getPathInfo()..<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">public String getPathInfo()<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">So from HTTPServletRequest, we not only get the parameters, we get the method name, we get various parts of URL, we can get the whole URL as a StringBuffer also. We also have a method called getQueryString()<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">public String getQueryString()<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">to know what is there in the Query String of the URL.<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">w<\/span><span style=\"text-align: initial; font-size: 1em;\">e also have a method called getProtocol(), .<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">public String getProtocol()<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">to know, what's the protocol. We have that third part saying that this request is for HTTP\/1.1 or 1.0, for secured URL you might have used HTTPS, you can even know that also by using a method called getScheme()<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">public String getScheme()<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">which will identify the protocol http or https, what is being followed here in the URL string? You even have a method for knowing that the protocol is secure or not.<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">the method is isSecure().<\/span><\/p>\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">public boolean isSecure()<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">For https isSecure() is true. if we go further in the HTTP Request, what do we have?<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">after the 1st line of the request, we have the headers. You might be interested in knowing what all headers have come and their values. we have methods for fetching the values of headers from the request that has arrived? we have methods like getHeaderNames()<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">public Enumeration&lt;String&gt; getHeaderNames()<\/span><\/p>\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">which returns the Enumeration of String. getHeaders()<\/span><\/p>\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">public String[] getHeaders(String headerName)<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">which takes the Header name because same header may have multiple values, it returns an array of Strings or you may use getHeader() with only the name..<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">public String getHeader(String headerName)<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">which gives the first value of header if the same header is repeated multiple times. We have header-specific methods also. for e.g. there is a header for content-type. So you might use the method called getContentType().<\/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&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">or use the method called getHeader() where you pass \u201ccontent-type\u201d as a parameter. For some popular header names we might have some readymade methods. Instead of using the method called getHeader(),we might use methods for some of the commonly used headers.<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">Talking about headers, in HTTPServletRequest we have a method called getHeader() which takes a String and gives a string. For example, for content-length we know that the integer will be retuned. So instead of using getHeader() and then using the parseInt() method for getting the string converted to int, or for that matter, any header that we understand is going to be an integer type, instead of using a parseInt() separately, you have convenient methods. so wherever you know, this header has an integer value, you could be using the method called getIntHeader().<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: initial;\">public int getIntHeader(String headerName)<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">It is same as getHeader() but also doing conversion into int. There are date-time headers like last-modified, there are headers which are nothing but representative of date and time.if- modified-since is another one. if-modified-since is useful when there is caching being done. So if we have headers which are involving date and time. the HTTP protocol says, there are 3 different format of the dates which are supported. when the request comes, for the specific header which has a date information, you would be having different formats. so here if you use getHeader() method and then try to parse, you will have to try all the 3 formats. you will have to identify which format does it follows. Instead of that there is a convenient method called getDateHeader(),<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">public long getDateHeader(String headername)<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">here you pass a header name, it returns long (time in milliseconds) a standard thing in JAVA.<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">We have this method called getCookies() to return an array of Cookie which we will be looking at in a later module. We might have methods for authentication specifying what kind of authentication was carried out? which is getAuthType().<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">public String getAuthType()<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">We have methods to know if the authentication has been carried out. who is the current user from where the request has come. We also have methods like getRemoteUser().<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">public String getRemoteUser()<\/span><\/p>\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">or getUserPrincipal()<\/span><\/p>\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">public java.security.Princiapl getUserPrincipal()<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">which returns an object of Principal which belongs to java.security package. Principal in turn has a method called getName()<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">public String getName()<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">which returns the name of the user.<\/span><\/p>\r\n&nbsp;\r\n<p class=\"hanging-indent\" style=\"text-align: justify;\"><strong style=\"text-align: initial; font-size: 1em;\">HttpServletResponse\u00a0<\/strong><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">Now let\u2019s have a look at the methods of HTTPServletResponse. if we look at the structure of the an Http response,\u00a0<\/span><span style=\"font-size: 1em;\">A Http response as discussed in an earlier module, has first line as: protocol-version response_code\u00a0 message\u00a0<\/span><span style=\"text-align: initial; font-size: 1em;\">eg.<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">HTTP\/1.1 200 OK<\/span><\/p>\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">HTTPServletResponse has a method called getStatus()<\/span><\/p>\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">public int getStatus()<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">which gives us the status\u00a0 \u00a0code determining what is the response. There is a method called setStatus()<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">public void setStatus(int status)<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">to set the status that is to be sent as a part of response. As it is server side, we are actually setting up the response. As part of setting up the response, we can also set up the response status also. to send a specific response to the client, we have a method called sendError().(don\u2019t go by the name)<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">public void sendError(int status)<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">sendError() takes integer status code and the string for the message.<\/span><\/p>\r\n<span style=\"text-align: initial; font-size: 1em;\">public void sendError(int status, String message)<\/span>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"text-align: justify; font-size: 1em;\">it would not be an error, you might be giving a successful status code here and a message. When we use the sendError() method, the container has its own specific template according to which it will send the content. As far as the status code is concerned, it is this code which we use in this sendError() method. So sendError() doesn\u2019t necessarily mean that there is an error response.<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"text-align: justify; font-size: 1em;\">Now coming to the headers. Some default values will be there for some common headers decided by the container. If you want to send some headers which are not normally sent. we have a method called setHeader().We might have to send a header when we do redirection, for that we have convenience method called sendRedirect()<\/span><\/p>\r\n&nbsp;\r\n\r\n<span style=\"text-align: initial; font-size: 1em;\">public void sendRedirect(String location)<\/span>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"text-align: justify; font-size: 1em;\">where you specify the new location as URL string. You don\u2019t have to set up a header for the location. The appropriate status code and the appropriate location header and the other content would then be sent by the container for us. We don\u2019t have to setup anything else. We just call sendRedirect(). Many times when we want to say sendRedirect(),we are passing a URL, we want to have some escaping done for us. Sometimes session information need to be included as a part of URL itself. we have a method in HTTPServletResponse called encodeURL().<\/span><\/p>\r\n&nbsp;\r\n\r\n<span style=\"text-align: initial; font-size: 1em;\">public String encodeURL(String url)<\/span>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"text-align: justify; font-size: 1em;\">This could be used for encoding the URL by removing the escape sequences for us, and including session information in the url.<\/span><\/p>\r\n&nbsp;\r\n\r\n<span style=\"text-align: initial; font-size: 1em;\">We have got methods for setting up headers again. We have a method called setHeader()<\/span>\r\n\r\n<span style=\"text-align: initial; font-size: 1em;\">public void setHeader(String headerName, String headerValue)<\/span>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"text-align: justify; font-size: 1em;\">which takes a name and a corresponding value. That will replace any header which was used earlier(same headername). Same header could have multiple values, that could also be possible. For that we have another method called addHeader().<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">public void addHeader(String headerName, String headerValue)<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">Both these methods, setHeader() and addHeader() have convenient form in case the header values are of integer type or date type. the convenience methods are addIntHeader() or setIntHeader().<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">public void addIntHeader(String headerName, int headerValue) public void setIntHeader(String headerName, int headerValue) same way we have the addDateHeader() and setDateHeader() methods also.<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">public void addDateHeader(String headerName, long headerValue) public void setDateHeader(String headerName, long headerValue)<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">Talking about the development of the servlet. We have seen two ways of creating a Servlet. One was using implementation of Servlet. Another was extending GenericServlet. You could even use the BaseServlet that we have developed earlier.<\/span><\/p>\r\n&nbsp;\r\n<p class=\"hanging-indent\" style=\"text-align: justify;\"><strong style=\"text-align: initial; text-indent: -1em; font-size: 1em;\">HttpServlet\u00a0<\/strong><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">In javax.servlet.http package, there is a class called HTTPServlet. This is the class which extends from GenericServlet. It is an abstract class with no abstract methods.Let\u2019s see how it overrides some of the methods from GenericServlet and how it adds few more methods to it. service() was abstract in the GenericServlet.<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">public void service(ServletRequest req, ServletResponse res)<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">The parameters were ServletRequest and ServletResponse which is actually from the generic part of the servlet API. we have that method being overridden here to simply invoke another form of service() method. We have one more service() method added in the HttpServlet class.<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">protected void service(HttpServletRequest request, HttpServletResponse response)<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">This service() method has parameters of type HttpServletRequest and HttpServletResponse. The implementation of the earlier service method is to simply cast the ServletRequest and ServletResponse parameters to HttpServletRequest and HttpServletResponse and then invoke the other service method.<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">public void service(ServletRequest req, ServletResponse res) { service((HttpServletRequest) req, (HttpServletResponse) res);<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">}<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">This new service method takes HttpServletRequest and HttpServletResponse as parameters. What does this service() methods do? This service method which has parameters of HttpServletRequest and HttpServletResponse is implemented to identify from the request which is an instance of HttpServletRequest, the Http method which was used by the client. In the HttpServletRequest interface we have a method called getMethod which returns the Http method used by the client.<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">public String getMethod()<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">The implementation of the service method would identify the http method and it would then invoke the corresponding doXXX method, where XXX is the http method.<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">Depending on the http request we may be interested in handling the request differently. We may not want to handle some type of http method in request. To make this possible what has been done here is, the service method which takes HttpServletRequest and HttpServletResponse as parameter has been written in a manner, where the first thing it does is, looks at the http request, find out the http method, what is the http method which is used and then it says, if the method is GET, then I will use the doGet method, if the method is POST, then I will use the doPost method. For each of the http method like GET, POST, HEAD, TRACE, etc., there is a separate method in our HttpServlet class. So we have various kinds of do methods doGet, doPost, doHead, doTrace, and so on. The service method detects what is the method in the request and then accordingly invokes the specific do mthod. So, what is done in\u00a0<\/span><span style=\"font-size: 1em;\">the do methods here? What is the default implementation of the doGet method? The doGet method which we have in the HttpServletRequest is simply implemented to send an error to say that GET method is not supported. So implementation of doGet is to send error to client that GET method is not supported, implementation of doPost is to send an error to the client saying POST method is not supported, and so on. Why such an implementation of these methods? The idea here is that when you want to write a Servlet, you will create a sub-class of the HttpServlet and whichever http method you want to support, you should be overriding that, the others by default would not be supported.<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">Now, what are the ways to develop a Servlet? We have now all the options available, first option, what we did with HelloServlet? The option we used was implementing the Servlet interface. Next option extending from GenericServlet,b ut since we know we have Http Protocol, we need not go for this option, we could create Servlet by extending the HttpServlet, and here again we have the option of overriding any of those two service methods, none of those are final, or we can override the required do method. If we want to support a specific http method, eg. We are interested in handling on the GET method, or may be interested in handling more than one method eg. Interested in handling GET as well POST, and both of them doing the same thing, we could override both the doGet method as well as the doPost method and one of them may simply invoke the other one, or we may have two different ways of handling both the methods.<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">There is one more method added in the HttpServlet, called lastModified to return long. This is simply to give a timestamp what you consider for this URL what is the last modification date and time. By default it would return a -1 indicating, that this content cant be cached.<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">So, in this module we have covered the HttpServletRequest, the HttpServletRequest and the HttpServlet, how it extends from the GenericServlet and implements the service method.<\/span><\/p>\r\n<table>\r\n<tbody>\r\n<tr>\r\n<td><strong>you can view video on HTTP specific API for Servlets<\/strong><\/td>\r\n<td><a href=\"https:\/\/youtu.be\/yVXicwaLHbA\" 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\" style=\"text-align: justify;\"><strong style=\"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>","rendered":"<div><span style=\"float: right;\"><a href=\"https:\/\/youtu.be\/yVXicwaLHbA\" 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;\">Welcome to this module on HTTP specific APIs on Servlets. In the earlier module we have seen the general part of API. So far, we have explored the javax.servlet package.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">Now we are looking at the javax.servlet.http package. As far as the servlet specification is concerned, what it says is, any request-response based protocol is supported by the implementation of servlet specification&#8230;But one thing which is mandatory is, Whatever is your servlet container, it must implement and support the HTTP protocol. There as a part of servlet specification itself, we have a package called javax.servlet.http which is talking about specifics of HTTP protocol.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">Here we have to recollect what we have learnt in HTTP protocol, the various parts of HTTP protocol. From the generic API we have seen the interfaces like ServletRequest, the ServletResponse which were very general. There was nothing specific about HTTP protocol there. But when we talk of javax.servlet.http package, it has additional interfaces. It has an interfaces called<\/p>\n<p>&nbsp;<\/p>\n<p>a)\u00a0 HTTPServletRequest<\/p>\n<p>b)\u00a0 HTTPServletResponse.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">These are the interfaces which are extending from generic interfaces. The general interfaces were ServletRequest and ServletResponse. Those are being extended to have an API which is specific to HTTP protocol.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">What exactly happens is whenever the request arrives, since the request which comes is an HTTP request, the container is creating objects which are implementations of HTTPServletRequest and HTTPServletResponse.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">These are extending the ServletRequest and ServletResponse interfaces. which are from the javax.servlet package.<\/p>\n<\/div>\n<div style=\"text-align: justify;\">\n<p style=\"text-align: justify;\">So in this module, we will cover these 2 interfaces called HTTPServletRequest and HTTPServletResponse<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">We also have another class here called HTTPServlet which is nothing but extending from GenericServlet class. So what are all those things which are part of HTTPServletRequest, HTTPServletResponse<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">What is additional there? \u00a0and things related to class called HTTPServlet.<\/p>\n<p>&nbsp;<\/p>\n<p class=\"hanging-indent\"><strong>HttpServletRequest\u00a0<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">Let us begin by understanding what is additional in the HTTPServletRequest? ServletRequest was very general. It just says, oh you could have any parameters. It says you could have content. When it comes to Http, of course HTTP also has that. HTTP can have parameters.<\/p>\n<p>&nbsp;<\/p>\n<p>How do we have parameters using HTTP? \u00a0There are 2 ways:<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">One way is, as part of URL itself. Someone could put those query string, using a &#8216;?&#8217; and then parameter_name=value [&amp; (parameter_name=value)] and so on. You could have any number of parameters being specified as part of URL itself. If that is the case the parameter is going to be available in the ServletRequest itself.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">Another way is, when someone submits a form. In the HTML form, we are able to specify method=post or method=get What happens when someone says method=get? The parameters which are coming from inputs tags become part of URL If the method was specified as POST, the parameters will going as a part of the content rather than going as part of URL. A string similar to the query string becomes the content of the HTTPRequest.<\/p>\n<p>&nbsp;<\/p>\n<p>Let us look at the structure of HTTPRequest.\u00a0 The first ine is typically<\/p>\n<p>GET resource_or_path protocol_version<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">In the interface called HTTPServletRequest, we have additional methods for these. You might be interested in knowing what is the HTTP method? Whether it is GET or POST or PUT or DELETE etc? we have various HTTP methods.\u00a0<span style=\"text-align: initial; font-size: 1em;\">In HttpServletRequest interface, we have a method called getMethod() which returns a String. This is method from<\/span><\/p>\n<p>&nbsp;<\/p>\n<p class=\"hanging-indent\" style=\"text-align: justify;\"><strong style=\"text-align: initial; text-indent: -1em; font-size: 1em;\">HTTPServletRequest.<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">You are interested in the resource!!! You have a method called getRequestedURL() which would give us the whole URL as a StringBuffer object. It is a StringBuffer which means it is modifiable. It can be modified before it is getting forwarded to a RequestDispatcher., by modifying it, there could be some parameters that could be added, yes, that could be done.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">We even have methods to get various parts of URL, This URL from the servlet specification point of view, is<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">Application name as the first part of URL<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">\/application_name\/&#8230;<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">so \u00a0you \u00a0are \u00a0interested \u00a0in \u00a0knowing \u00a0the \u00a0application \u00a0name&#8230;?\u00a0\u00a0\u00a0 We \u00a0have \u00a0this \u00a0method \u00a0in HTTPServletRequest, called getContextPath().<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">public String getContextPath()<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">that gives us the part which is related to the application, Then we have a method which will be specifying., what part of the URL is identifying the Servlet?<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">There is URL-pattern which is mapped to a Servlet,.So which part of the URL matched the pattern? because of which this invocation has taken place.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">For that we have a method called getServletPath()<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">public String getServletPath()<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">So you can know the ServletPath which is responsible for the identification \u00a0of the Servlet. and then after the servlet path, for e.g. you have that wildcard \/* at the end.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">So whatever is there after \/*, if there is something in current URL which matches to it, that could also be identified separately by method called by getPathInfo()..<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">public String getPathInfo()<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">So from HTTPServletRequest, we not only get the parameters, we get the method name, we get various parts of URL, we can get the whole URL as a StringBuffer also. We also have a method called getQueryString()<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">public String getQueryString()<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">to know what is there in the Query String of the URL.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">w<\/span><span style=\"text-align: initial; font-size: 1em;\">e also have a method called getProtocol(), .<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">public String getProtocol()<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">to know, what&#8217;s the protocol. We have that third part saying that this request is for HTTP\/1.1 or 1.0, for secured URL you might have used HTTPS, you can even know that also by using a method called getScheme()<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">public String getScheme()<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">which will identify the protocol http or https, what is being followed here in the URL string? You even have a method for knowing that the protocol is secure or not.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">the method is isSecure().<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">public boolean isSecure()<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">For https isSecure() is true. if we go further in the HTTP Request, what do we have?<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">after the 1st line of the request, we have the headers. You might be interested in knowing what all headers have come and their values. we have methods for fetching the values of headers from the request that has arrived? we have methods like getHeaderNames()<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">public Enumeration&lt;String&gt; getHeaderNames()<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">which returns the Enumeration of String. getHeaders()<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">public String[] getHeaders(String headerName)<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">which takes the Header name because same header may have multiple values, it returns an array of Strings or you may use getHeader() with only the name..<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">public String getHeader(String headerName)<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">which gives the first value of header if the same header is repeated multiple times. We have header-specific methods also. for e.g. there is a header for content-type. So you might use the method called getContentType().<\/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>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">or use the method called getHeader() where you pass \u201ccontent-type\u201d as a parameter. For some popular header names we might have some readymade methods. Instead of using the method called getHeader(),we might use methods for some of the commonly used headers.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">Talking about headers, in HTTPServletRequest we have a method called getHeader() which takes a String and gives a string. For example, for content-length we know that the integer will be retuned. So instead of using getHeader() and then using the parseInt() method for getting the string converted to int, or for that matter, any header that we understand is going to be an integer type, instead of using a parseInt() separately, you have convenient methods. so wherever you know, this header has an integer value, you could be using the method called getIntHeader().<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: initial;\">public int getIntHeader(String headerName)<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">It is same as getHeader() but also doing conversion into int. There are date-time headers like last-modified, there are headers which are nothing but representative of date and time.if- modified-since is another one. if-modified-since is useful when there is caching being done. So if we have headers which are involving date and time. the HTTP protocol says, there are 3 different format of the dates which are supported. when the request comes, for the specific header which has a date information, you would be having different formats. so here if you use getHeader() method and then try to parse, you will have to try all the 3 formats. you will have to identify which format does it follows. Instead of that there is a convenient method called getDateHeader(),<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">public long getDateHeader(String headername)<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">here you pass a header name, it returns long (time in milliseconds) a standard thing in JAVA.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">We have this method called getCookies() to return an array of Cookie which we will be looking at in a later module. We might have methods for authentication specifying what kind of authentication was carried out? which is getAuthType().<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">public String getAuthType()<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">We have methods to know if the authentication has been carried out. who is the current user from where the request has come. We also have methods like getRemoteUser().<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">public String getRemoteUser()<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">or getUserPrincipal()<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">public java.security.Princiapl getUserPrincipal()<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">which returns an object of Principal which belongs to java.security package. Principal in turn has a method called getName()<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">public String getName()<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">which returns the name of the user.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p class=\"hanging-indent\" style=\"text-align: justify;\"><strong style=\"text-align: initial; font-size: 1em;\">HttpServletResponse\u00a0<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">Now let\u2019s have a look at the methods of HTTPServletResponse. if we look at the structure of the an Http response,\u00a0<\/span><span style=\"font-size: 1em;\">A Http response as discussed in an earlier module, has first line as: protocol-version response_code\u00a0 message\u00a0<\/span><span style=\"text-align: initial; font-size: 1em;\">eg.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">HTTP\/1.1 200 OK<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">HTTPServletResponse has a method called getStatus()<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">public int getStatus()<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">which gives us the status\u00a0 \u00a0code determining what is the response. There is a method called setStatus()<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">public void setStatus(int status)<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">to set the status that is to be sent as a part of response. As it is server side, we are actually setting up the response. As part of setting up the response, we can also set up the response status also. to send a specific response to the client, we have a method called sendError().(don\u2019t go by the name)<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">public void sendError(int status)<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">sendError() takes integer status code and the string for the message.<\/span><\/p>\n<p><span style=\"text-align: initial; font-size: 1em;\">public void sendError(int status, String message)<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: justify; font-size: 1em;\">it would not be an error, you might be giving a successful status code here and a message. When we use the sendError() method, the container has its own specific template according to which it will send the content. As far as the status code is concerned, it is this code which we use in this sendError() method. So sendError() doesn\u2019t necessarily mean that there is an error response.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: justify; font-size: 1em;\">Now coming to the headers. Some default values will be there for some common headers decided by the container. If you want to send some headers which are not normally sent. we have a method called setHeader().We might have to send a header when we do redirection, for that we have convenience method called sendRedirect()<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"text-align: initial; font-size: 1em;\">public void sendRedirect(String location)<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: justify; font-size: 1em;\">where you specify the new location as URL string. You don\u2019t have to set up a header for the location. The appropriate status code and the appropriate location header and the other content would then be sent by the container for us. We don\u2019t have to setup anything else. We just call sendRedirect(). Many times when we want to say sendRedirect(),we are passing a URL, we want to have some escaping done for us. Sometimes session information need to be included as a part of URL itself. we have a method in HTTPServletResponse called encodeURL().<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"text-align: initial; font-size: 1em;\">public String encodeURL(String url)<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: justify; font-size: 1em;\">This could be used for encoding the URL by removing the escape sequences for us, and including session information in the url.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"text-align: initial; font-size: 1em;\">We have got methods for setting up headers again. We have a method called setHeader()<\/span><\/p>\n<p><span style=\"text-align: initial; font-size: 1em;\">public void setHeader(String headerName, String headerValue)<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: justify; font-size: 1em;\">which takes a name and a corresponding value. That will replace any header which was used earlier(same headername). Same header could have multiple values, that could also be possible. For that we have another method called addHeader().<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">public void addHeader(String headerName, String headerValue)<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">Both these methods, setHeader() and addHeader() have convenient form in case the header values are of integer type or date type. the convenience methods are addIntHeader() or setIntHeader().<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">public void addIntHeader(String headerName, int headerValue) public void setIntHeader(String headerName, int headerValue) same way we have the addDateHeader() and setDateHeader() methods also.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">public void addDateHeader(String headerName, long headerValue) public void setDateHeader(String headerName, long headerValue)<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">Talking about the development of the servlet. We have seen two ways of creating a Servlet. One was using implementation of Servlet. Another was extending GenericServlet. You could even use the BaseServlet that we have developed earlier.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p class=\"hanging-indent\" style=\"text-align: justify;\"><strong style=\"text-align: initial; text-indent: -1em; font-size: 1em;\">HttpServlet\u00a0<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">In javax.servlet.http package, there is a class called HTTPServlet. This is the class which extends from GenericServlet. It is an abstract class with no abstract methods.Let\u2019s see how it overrides some of the methods from GenericServlet and how it adds few more methods to it. service() was abstract in the GenericServlet.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">public void service(ServletRequest req, ServletResponse res)<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">The parameters were ServletRequest and ServletResponse which is actually from the generic part of the servlet API. we have that method being overridden here to simply invoke another form of service() method. We have one more service() method added in the HttpServlet class.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">protected void service(HttpServletRequest request, HttpServletResponse response)<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">This service() method has parameters of type HttpServletRequest and HttpServletResponse. The implementation of the earlier service method is to simply cast the ServletRequest and ServletResponse parameters to HttpServletRequest and HttpServletResponse and then invoke the other service method.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">public void service(ServletRequest req, ServletResponse res) { service((HttpServletRequest) req, (HttpServletResponse) res);<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">}<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">This new service method takes HttpServletRequest and HttpServletResponse as parameters. What does this service() methods do? This service method which has parameters of HttpServletRequest and HttpServletResponse is implemented to identify from the request which is an instance of HttpServletRequest, the Http method which was used by the client. In the HttpServletRequest interface we have a method called getMethod which returns the Http method used by the client.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">public String getMethod()<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">The implementation of the service method would identify the http method and it would then invoke the corresponding doXXX method, where XXX is the http method.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">Depending on the http request we may be interested in handling the request differently. We may not want to handle some type of http method in request. To make this possible what has been done here is, the service method which takes HttpServletRequest and HttpServletResponse as parameter has been written in a manner, where the first thing it does is, looks at the http request, find out the http method, what is the http method which is used and then it says, if the method is GET, then I will use the doGet method, if the method is POST, then I will use the doPost method. For each of the http method like GET, POST, HEAD, TRACE, etc., there is a separate method in our HttpServlet class. So we have various kinds of do methods doGet, doPost, doHead, doTrace, and so on. The service method detects what is the method in the request and then accordingly invokes the specific do mthod. So, what is done in\u00a0<\/span><span style=\"font-size: 1em;\">the do methods here? What is the default implementation of the doGet method? The doGet method which we have in the HttpServletRequest is simply implemented to send an error to say that GET method is not supported. So implementation of doGet is to send error to client that GET method is not supported, implementation of doPost is to send an error to the client saying POST method is not supported, and so on. Why such an implementation of these methods? The idea here is that when you want to write a Servlet, you will create a sub-class of the HttpServlet and whichever http method you want to support, you should be overriding that, the others by default would not be supported.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">Now, what are the ways to develop a Servlet? We have now all the options available, first option, what we did with HelloServlet? The option we used was implementing the Servlet interface. Next option extending from GenericServlet,b ut since we know we have Http Protocol, we need not go for this option, we could create Servlet by extending the HttpServlet, and here again we have the option of overriding any of those two service methods, none of those are final, or we can override the required do method. If we want to support a specific http method, eg. We are interested in handling on the GET method, or may be interested in handling more than one method eg. Interested in handling GET as well POST, and both of them doing the same thing, we could override both the doGet method as well as the doPost method and one of them may simply invoke the other one, or we may have two different ways of handling both the methods.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">There is one more method added in the HttpServlet, called lastModified to return long. This is simply to give a timestamp what you consider for this URL what is the last modification date and time. By default it would return a -1 indicating, that this content cant be cached.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">So, in this module we have covered the HttpServletRequest, the HttpServletRequest and the HttpServlet, how it extends from the GenericServlet and implements the service method.<\/span><\/p>\n<table>\n<tbody>\n<tr>\n<td><strong>you can view video on HTTP specific API for Servlets<\/strong><\/td>\n<td><a href=\"https:\/\/youtu.be\/yVXicwaLHbA\" 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\" style=\"text-align: justify;\"><strong style=\"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","protected":false},"author":4,"menu_order":19,"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-114","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\/114","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\/114\/revisions"}],"predecessor-version":[{"id":407,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-json\/pressbooks\/v2\/chapters\/114\/revisions\/407"}],"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\/114\/metadata\/"}],"wp:attachment":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-json\/wp\/v2\/media?parent=114"}],"wp:term":[{"taxonomy":"chapter-type","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-json\/pressbooks\/v2\/chapter-type?post=114"},{"taxonomy":"contributor","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-json\/wp\/v2\/contributor?post=114"},{"taxonomy":"license","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-json\/wp\/v2\/license?post=114"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}