Validating username and password in servlet
With this design, you can easily find out where the error is instead keeping everything in a JSP. In this first example, client sends user name and password to JSP on the Server. Other scripting elements are Expression and Declaration.Even the service() method is non-abstract and infact with Http Servlet, the service() method can be replaced by the methods do Get() or do Post(). The above code works fine even if Generic Servlet is extended instead of Http Servlet. public void service(Http Servlet Request req, Http Servlet Response res) throws Servlet Exception, IOException service() method is a callback method called automatically when the container loads the servlet for execution when the client request comes.The method takes two parameters – an object of interface Http Servlet Request and an object of interface Http Servlet Response and let us see what these interfaces meant to servlet.First step is to check the file C:\Program Files\Apache Software Foundation\Tomcat 4.1\work\Standalone\localhost\java\Site Validate_jsp.java, line 74.
Http Servlet is an abstact class but without any abstract methods (all are non-abstract methods).That is, req object of Http Servlet Request receives data sent by client.From this object, the servlet programmer can extract the data and can use for validation or other purposes as hereunder. out.println("VALID"); We know earlier the Http Servlet Response is meant to send data (known as response) to client. As a convenience, the get Writer() method of Servlet Response (inherited by Http Servlet Response) returns an object of Print Writer.Server receives, validates and sends back the validation result to client as response. Following is the Servlet version for the above JSP file. See the length of coding between a Servlet and JSP doing the same job.Following is the client-side HTML file calling File Name: User is known as Scriptlet. HTML screen when User name and Password are entered. Enter user name and password and click submit button.
The responsibility of the data to be sent (like valid or invalid etc.) to the client is taken care by Http Servlet Response. Content Type("text/html"); set Content Type(String) is a method of Servlet Response inherited by Http Servlet Response and its job is sending the information to client’s browser of how the response sent by the servlet is to be formatted. That is, the data sent to the client is either in text format (as in ("VALID")) or HTML format (as in (INVALID)). This is more called as setting the MIME (Multipurpose Internet Mail Extension) type. get Parameter(String) method of Servlet Request (inherited by Http Servlet Request) takes a string parameter and returns a string.