<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Java Interview Questions &#187; Servlet Tutorial</title>
	<atom:link href="http://www.bestjavainterviewquestions.com/category/java-important-notes/servlet-tutorial/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.bestjavainterviewquestions.com</link>
	<description>Java Interview Questions &#124; Core Java Interview Questions &#124; Advanced Java Interview Questions &#124; EJB Interview Questions &#124; J2EE Interview Questions &#124; Hibernate Interview Questions</description>
	<lastBuildDate>Fri, 03 Feb 2012 10:23:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Define ServletInputStream</title>
		<link>http://www.bestjavainterviewquestions.com/define-servletinputstream/</link>
		<comments>http://www.bestjavainterviewquestions.com/define-servletinputstream/#comments</comments>
		<pubDate>Mon, 25 Aug 2008 17:45:17 +0000</pubDate>
		<dc:creator>Ramakrishna</dc:creator>
				<category><![CDATA[Servlet Tutorial]]></category>
		<category><![CDATA[servletinputstream]]></category>

		<guid isPermaLink="false">http://www.bestjavainterviewquestions.com/?p=206</guid>
		<description><![CDATA[The ServletInputStream Class The ServletInputStream class extends InputStream. It is implemented by the server and provides an input stream that a servlet developer can use to read the data from a client request. It defines the default constructor. In addition, a method is provided to read bytes from the stream. Its signature is shown here: [...]]]></description>
			<content:encoded><![CDATA[<p><strong>The ServletInputStream Class</strong></p>
<p>The ServletInputStream class extends InputStream. It is implemented by the server and provides an input stream that a servlet developer can use to read the data from a client request. It defines the default constructor. In addition, a method is provided to read bytes from the stream. Its signature is shown here:</p>
<p><em><strong>int readLine(byte[] <em>buffer</em>, int <em>offset</em>, int <em>size</em>) throws IOException</strong></em></p>
<p>Here, <em>buffer </em> is the array into which size bytes are placed starting at <em>offset</em>. The method returns the actual number of bytes read or -1 if an end-of-stream condition is encountered.</p>
<p><em>Sample example by using ServletOutputStream Class are as follows:</em></p>
<p><em>Sample Ex-1:</em></p>
<div style="overflow: auto; width: 450px; height: 800px; background-color: #e9e0da;">import javax.servlet.*;<br />
import java.io.IOException;<br />
import java.io.BufferedReader;<br />
import java.io.PrintWriter;</p>
<p>/**<br />
*    A Test for readLine method<br />
*/</p>
<p>public class ReadLineTestServlet extends GenericServlet {</p>
<p>public void service ( ServletRequest request, ServletResponse response ) throws ServletException, IOException {<br />
PrintWriter out = response.getWriter();<br />
ServletInputStream sins = request.getInputStream();</p>
<p>int contentLen = request.getContentLength();</p>
<p>if ( contentLen &gt;= 1 ) {</p>
<p>byte buffer[] = new byte[ contentLen ];</p>
<p>int len = sins.readLine( buffer, 0, buffer.length );<br />
String expectedResult = &#8220;ULTRA SPARC&#8221;;<br />
//our client sent ULTRA SPARC  in the stream</p>
<p>String result = new String( buffer, 0, len );<br />
if ( result.trim().equals( expectedResult ) ) {<br />
out.println( &#8220;ReadLineTest test PASSED&#8221; );<br />
} else {<br />
out.println( &#8220;ReadLineTest test FAILED&lt;BR&gt;&#8221; );<br />
out.println( &#8220;     ServletInputStream.readLine() returned incorrect result &lt;BR&gt;&#8221; );<br />
out.println( &#8220;     Expected result = &#8221; + expectedResult + &#8221; &lt;BR&gt;&#8221; );<br />
out.println( &#8220;     Actual result = |&#8221; + result + &#8220;| &lt;BR&gt;&#8221; );<br />
}<br />
} else {<br />
out.println( &#8220;ReadLineTest test FAILED&lt;BR&gt;&#8221; );<br />
out.println( &#8220;     ServletRequest.getContentLength() returned incorrect result &lt;BR&gt;&#8221; );<br />
out.println( &#8220;     Expected a result &gt;= 1 &lt;BR&gt;&#8221; );<br />
out.println( &#8220;     Actual result = &#8221; + contentLen + &#8221; &lt;BR&gt;&#8221; );<br />
}<br />
}<br />
}</p></div>
<h2  class="related_post_title">Random Posts</h2><ul class="related_post"><li>July 21, 2010 -- <a href="http://www.bestjavainterviewquestions.com/how-to-make-a-class-serializable/" title="How to Make a Class Serializable">How to Make a Class Serializable</a> (1)</li><li>February 15, 2009 -- <a href="http://www.bestjavainterviewquestions.com/providing-current-date-and-time-using-stateless-session-bean/" title="Providing current date and time using Stateless session bean">Providing current date and time using Stateless session bean</a> (0)</li><li>October 16, 2008 -- <a href="http://www.bestjavainterviewquestions.com/what-the-webxml-file-can-do/" title="What the web.xml file can do">What the web.xml file can do</a> (1)</li><li>May 22, 2008 -- <a href="http://www.bestjavainterviewquestions.com/core-java-interview-questions/" title="Core Java Interview Questions">Core Java Interview Questions</a> (1)</li><li>December 17, 2008 -- <a href="http://www.bestjavainterviewquestions.com/define-static-keyword/" title="Define Static Keyword">Define Static Keyword</a> (0)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.bestjavainterviewquestions.com/define-servletinputstream/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Define ServletOutputStream</title>
		<link>http://www.bestjavainterviewquestions.com/define-servletoutputstream/</link>
		<comments>http://www.bestjavainterviewquestions.com/define-servletoutputstream/#comments</comments>
		<pubDate>Mon, 25 Aug 2008 17:24:10 +0000</pubDate>
		<dc:creator>Ramakrishna</dc:creator>
				<category><![CDATA[Servlet Tutorial]]></category>
		<category><![CDATA[servletoutputstream]]></category>

		<guid isPermaLink="false">http://www.bestjavainterviewquestions.com/?p=204</guid>
		<description><![CDATA[The ServletOutputStream Class The ServletOutputStream class extends OutputStream.It is implemented by the server and provides an output stream that a servlet developer can use to write data to a client response. A default constructor is define.It also defines the print() and println() method, which output data to the stream Sample example by using ServletOutputStream Class [...]]]></description>
			<content:encoded><![CDATA[<p><strong>The ServletOutputStream Class</strong></p>
<p>The ServletOutputStream class extends OutputStream.It is implemented by the server and provides an output stream that a servlet developer can use to write data to a client response. A default constructor is define.It also defines the <em>print()</em> and <em>println()</em> method, which output data to the stream</p>
<p><em>Sample example by using ServletOutputStream Class are as follows:</em><br />
<strong><br />
sample1;Example Using the jsp servletoutputstream<br />
</strong></p>
<div style="overflow: auto; width: 500px; height: 630px; background-color: #e9e0da;">JSP servlet output stream</p>
<p>package com.ack.web.servlet;</p>
<p>import java.io.IOException;<br />
import javax.servlet.ServletOutputStream;<br />
import javax.servlet.http.HttpServletResponse;</p>
<p>/**<br />
* In the ServletOutputStream all methods converge on the write(int)<br />
* output method.  So it is this method that we override to customise<br />
* what is sent back to an HTTP client that uses the JSPServletOutputStream.<br />
*<br />
* In this case we simply replace &#8216;&lt;&#8217; with &#8216;[' and '&gt;' with ']&#8216;, each<br />
* with ascii values 60, 91, 62, 93 respectively.<br />
*<br />
*/<br />
public class JSPServletOutputStream extends ServletOutputStream {<br />
private HttpServletResponse delegate;</p>
<p>public JSPServletOutputStream( HttpServletResponse hss ) {<br />
delegate = hss;<br />
}</p>
<p>public void write( int c ) throws IOException {<br />
if( c == 60 ) {<br />
delegate.getOutputStream().write( 91 );<br />
}<br />
else if( c == 62 ) {<br />
delegate.getOutputStream().write( 93 );<br />
}<br />
else {<br />
delegate.getOutputStream().write( c );<br />
}<br />
}<br />
}</p></div>
<p><strong>sample 2:</strong><strong>Example Using the servletoutputstream</strong><strong> in servlet program</strong></p>
<div style="overflow: auto; width: 500px; height: 850px; background-color: #e9e0da;">import javax.servlet.http.*;<br />
import javax.servlet.*;<br />
import javax.servlet.ServletOutputStream;<br />
import java.io.*;</p>
<p>public class FileDownloadServlet extends HttpServlet {</p>
<p>public void doGet ( HttpServletRequest req, HttpServletResponse res )<br />
throws ServletException, IOException<br />
{<br />
String docSid = null;<br />
String fullName = null;<br />
String name = null;<br />
ServletOutputStream out = res.getOutputStream (  ) ;</p>
<p>String fullName = &#8220;fileToBeDownloaded.xxx&#8221;;</p>
<p>String pathName = getServletContext (  ) .getRealPath ( &#8220;/&#8221; + fullName ) ;<br />
String contentType = getServletContext (  ) .getMimeType ( pathName ) ;</p>
<p>if  ( contentType != null )<br />
res.setContentType ( contentType ) ;<br />
else<br />
res.setContentType ( &#8220;application/octet-stream&#8221; ) ;</p>
<p>res.setHeader  ( &#8220;Content-Disposition&#8221;, &#8220;attachment; filename=\&#8221;" + name + &#8220;\&#8221;" ) ;<br />
FileInputStream fis = null;</p>
<p>// Return the file<br />
try  {<br />
fis = new FileInputStream ( fullName ) ;<br />
byte [  ]  buf = new byte [ 4 * 1024 ] ; // 4K buffer<br />
int bytesRead;<br />
while  (  ( bytesRead = fis.read ( buf )  )  != -1 )<br />
out.write ( buf, 0, bytesRead ) ;<br />
}<br />
catch  ( FileNotFoundException e )   {<br />
out.println ( &#8220;File not found: &#8221; + fullName ) ;<br />
}<br />
catch  ( IOException e )   {<br />
out.println ( &#8220;Problem sending file &#8221; + pathName + &#8220;: &#8221; + e.getMessage (  )  ) ;<br />
}<br />
finally  {<br />
if  ( fis != null )<br />
fis.close (  ) ;<br />
}<br />
}<br />
}</p></div>
<h2  class="related_post_title">Random Posts</h2><ul class="related_post"><li>September 22, 2008 -- <a href="http://www.bestjavainterviewquestions.com/difference-between-print-and-println-methods-in-java/" title="Difference between Print() and Println() Methods in Java">Difference between Print() and Println() Methods in Java</a> (0)</li><li>October 5, 2008 -- <a href="http://www.bestjavainterviewquestions.com/goto-statement-in-java/" title="GoTo statement in Java">GoTo statement in Java</a> (0)</li><li>October 27, 2008 -- <a href="http://www.bestjavainterviewquestions.com/define-connection-pooling/" title="Define Connection Pooling">Define Connection Pooling</a> (0)</li><li>June 6, 2008 -- <a href="http://www.bestjavainterviewquestions.com/applet-interview-questions/" title="Applet Interview Questions">Applet Interview Questions</a> (0)</li><li>October 4, 2008 -- <a href="http://www.bestjavainterviewquestions.com/difference-between-executeupdate-and-executequery-methods/" title="Difference between executeUpdate and executeQuery Methods">Difference between executeUpdate and executeQuery Methods</a> (0)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.bestjavainterviewquestions.com/define-servletoutputstream/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Can we call destroy() method on servlets from service method</title>
		<link>http://www.bestjavainterviewquestions.com/can-we-call-destroy-method-on-servlets-from-service-method/</link>
		<comments>http://www.bestjavainterviewquestions.com/can-we-call-destroy-method-on-servlets-from-service-method/#comments</comments>
		<pubDate>Sun, 10 Aug 2008 13:35:23 +0000</pubDate>
		<dc:creator>Ramakrishna</dc:creator>
				<category><![CDATA[Servlet Tutorial]]></category>
		<category><![CDATA[servlet destroy method]]></category>

		<guid isPermaLink="false">http://www.bestjavainterviewquestions.com/?p=119</guid>
		<description><![CDATA[we can call the destroy() method. but it works like normal method , for example ,if u have one method in ur servlet class like show(), it acts like show() method. whenever we click on the stop link in manager page of that particular application,then only application will be stopped and it calls the destory [...]]]></description>
			<content:encoded><![CDATA[<p>we can call the destroy() method. but it works like normal method , for example ,if u have one method in ur servlet class like show(), it acts like show() method. whenever we click on the stop link in manager page of that particular application,then only application will be stopped and it calls the destory method. At that time only the application objects permenantly destroyed.sample code: Try it out..</p>
<p>public class TestServlet extends HttpServlet implements SingleThreadModel<br />
{<br />
public void init()<br />
{<br />
System.out.println(&#8220;hello in init&#8221;);<br />
}<br />
public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException<br />
{<br />
PrintWriter out=response.getWriter();<br />
out.println(&#8220;Selected Button Name :&#8221;+request.getParameter(&#8220;button&#8221;));<br />
}<br />
public void destroy()<br />
{ System.out.println(&#8220;Hello in destroy&#8221;); }</p>
<p><span id="sort1"><span class="tdvamseel"><br />
Destroy method is a method where we can provide the actions (to free up the resources)Â to be performed while the servlet is getting unloaded. In general when a servlet is getting destroyed the servlet container calls this method and also does many other things. So if you want to unloadÂ a servlet you have to do all the things the servlet container does including calling the servlet method.</p>
<p>When you directly call the destroy method from service method the method will be successfully called as you are calling the method from same object but you are not doing all the other things the servlet does to unload the servlet. So the servlet will not be unloaded but the destroy method will be called as any other non life cycle method (like display()).</span></span></p>
<h2  class="related_post_title">Random Posts</h2><ul class="related_post"><li>June 30, 2008 -- <a href="http://www.bestjavainterviewquestions.com/java-exception-handling-questions/" title="Java Exception Handling Questions">Java Exception Handling Questions</a> (0)</li><li>December 17, 2008 -- <a href="http://www.bestjavainterviewquestions.com/what-is-abstract-classexplan/" title="What is Abstract Class?Explan">What is Abstract Class?Explan</a> (0)</li><li>August 15, 2008 -- <a href="http://www.bestjavainterviewquestions.com/main-exceptions-in-thread/" title="Main Exceptions in Thread">Main Exceptions in Thread</a> (0)</li><li>September 16, 2008 -- <a href="http://www.bestjavainterviewquestions.com/converting-hibernate-with-eclipse-integration/" title="Converting Hibernate with Eclipse Integration">Converting Hibernate with Eclipse Integration</a> (0)</li><li>October 16, 2008 -- <a href="http://www.bestjavainterviewquestions.com/what-u-mean-by-web-application/" title="What u mean by Web Application">What u mean by Web Application</a> (1)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.bestjavainterviewquestions.com/can-we-call-destroy-method-on-servlets-from-service-method/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Example programs of lifecycle of servlets</title>
		<link>http://www.bestjavainterviewquestions.com/example-programs-of-lifecycle-of-servlets/</link>
		<comments>http://www.bestjavainterviewquestions.com/example-programs-of-lifecycle-of-servlets/#comments</comments>
		<pubDate>Sun, 10 Aug 2008 13:25:42 +0000</pubDate>
		<dc:creator>Ramakrishna</dc:creator>
				<category><![CDATA[Servlet Tutorial]]></category>
		<category><![CDATA[servlet example]]></category>

		<guid isPermaLink="false">http://www.bestjavainterviewquestions.com/?p=114</guid>
		<description><![CDATA[A Generic servlet contains the following five methods: init(): public void init(ServletConfig config) throws ServletException init() method :This Method is called only once by the servlet container throughout the life of a servlet. By this init() method the servlet get to know that it has been placed into service. The servlet cannot be put into [...]]]></description>
			<content:encoded><![CDATA[<p><em>A Generic servlet contains the following five methods:</em></p>
<p><strong>init()</strong>:</p>
<p>public void init(ServletConfig config) throws ServletException</p>
<p><strong>init() method</strong> :This Method is called only once by the servlet container throughout the life of a servlet. By this init() method the servlet get to know that it has been placed into service.</p>
<p>The servlet cannot be put into the service if</p>
<p>*  The init() method does not return within a fix time set by the web server.<br />
*  It throws a ServletException<br />
<em><br />
Parameters</em> &#8211; The init() method takes a ServletConfig object that contains the initialization parameters and servlet&#8217;s configuration and throws a ServletException if an exception has occurred.</p>
<p><strong>service() Method:</strong></p>
<p>public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException</p>
<p>Once the servlet starts getting the requests, the service() method is called by the servlet container to respond. The servlet services the client&#8217;s request with the help of two objects. These two objects javax.servlet.ServletRequest and  javax.servlet.ServletResponse are passed by the servlet container.</p>
<p>The status code of the response always should be set for a servlet that throws or sends an error.</p>
<p><em>Parameters </em>-  The service() method takes the ServletRequest object that contains the client&#8217;s request and the object ServletResponse contains the servlet&#8217;s response. The service() method throws ServletException and IOExceptions exception.</p>
<p><strong>getServletConfig()</strong> <strong>Method:</strong></p>
<p>public ServletConfig getServletConfig()</p>
<p>This method contains parameters for initialization and startup of the servlet and returns a ServletConfig object. This object is then passed to the init method. When this interface is implemented then it stores the ServletConfig object in order to return it. It is done by the generic class which implements this inetrface.</p>
<p>Returns -  the ServletConfig object</p>
<p><strong>getServletInfo() Method:</strong></p>
<p>public String getServletInfo()</p>
<p>The information about the servlet is returned by this method like version, author etc. This method returns a string which should be in the form of plain text and not any kind of markup.</p>
<p>Returns &#8211; a string that contains the information about the servlet</p>
<p><strong>destroy() Method:</strong></p>
<p>public void destroy()</p>
<p>This method is called when we need to close the servlet. That is before removing a servlet instance from service, the servlet container calls the destroy() method. Once the servlet container calls the destroy() method, no service methods will be then called . That is after the exit of all the threads running in the servlet, the destroy() method is called. Hence, the servlet gets a chance to clean up all the resources like memory, threads etc which are being held.</p>
<p><em>Some Important methods  used in the lifecycle of servlets:</em></p>
<p>Sample:#1</p>
<p>import java.io.*;<br />
import javax.servlet.*;</p>
<p>public class HelloServlet extends GenericServlet {</p>
<p>public void  init ( ServletConfig  config ) {</p>
<p>super.init ( config );<br />
}<br />
public void  service (<br />
ServletRequest  req, ServletResponse  res )<br />
throws ServletException, IOException {</p>
<p>PrintStream  out = new PrintStream ( res.getOutputStream ( ) );<br />
out.println ( &#8220;Hello, World!&#8221; );<br />
}<br />
public void  destroy ( ) {</p>
<p>super.destroy ( );<br />
}<br />
}</p>
<p>Sample:#2</p>
<p><strong>The Basic Example of servlet Program</strong></p>
<p>The Login Page of servlet:<br />
&lt;html&gt;<br />
&lt;head&gt;<br />
&lt;title&gt;ADMINSTRATOR LOGIN&lt;/title&gt;<br />
&lt;/head&gt;<br />
&lt;script language=&#8217;javascript&#8217;&gt;<br />
var ele_arr=new Array(&#8220;Center ID&#8221;,&#8221;Password&#8221;);</p>
<p>function validate()<br />
{<br />
var i=0;<br />
var ele=login.elements.length-2;</p>
<p>var status;<br />
a:for(i=0;i&lt;=ele;i++)<br />
{<br />
if(login.elements[i].value==&#8221;")<br />
{<br />
alert(&#8220;Sorry!! The &#8216;&#8221;+ele_arr[i]+&#8221;&#8216; field cannot be blank\nPlease enter a value&#8221;);<br />
login.elements[i].focus();<br />
status=&#8221;NO&#8221;;<br />
break a;<br />
}<br />
else<br />
{<br />
status=&#8221;YES&#8221;;<br />
}<br />
}</p>
<p>if(status==&#8221;YES&#8221;)<br />
{<br />
return true;<br />
}<br />
else<br />
{<br />
return false;<br />
}<br />
}</p>
<p>&lt;/script&gt;</p>
<p>&lt;body background=&#8221;images\backgrd.GIF&#8221; bgcolor=&#8221;#FFCCFF&#8221; leftmargin=&#8221;0&#8243; rightmargin=&#8221;0&#8243;&gt;<br />
&lt;FORM action=&#8221;/../Login&#8221; method=&#8221;Get&#8221; name=login onsubmit=&#8217;return validate();&#8217;&gt;</p>
<p>&lt;font color=&#8221;#FF0000&#8243; size=&#8221;5&#8243; face=&#8221;Verdana, Arial, Helvetica, sans-serif&#8221;&gt;&lt;strong&gt;LOGIN HERE &lt;/strong&gt;&lt;/font&gt;&lt;br&gt;</p>
<p>&lt;font color=&#8221;#FF00FF&#8221; size=&#8221;4&#8243;&gt;User Name:&lt;/font&gt;<br />
&lt;input type=TEXT name=&#8221;text1&#8243;  &gt;</p>
<p>&lt;font color=&#8221;#FF33FF&#8221; size=&#8221;4&#8243;&gt;PASSWORD:&lt;/font&gt;<br />
&lt;input type=PASSWORD name=&#8221;text2&#8243;&gt;</p>
<p>&lt;input type=&#8221;submit&#8221; name=&#8221;Submit1&#8243; value=&#8221; L O G I N&#8221;&gt;<br />
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;<br />
&lt;input type=&#8221;reset&#8221; name=&#8221;Submit2&#8243; value=&#8221; R E S E T &#8220;&gt;</p>
<p>&lt;/FORM&gt;<br />
&lt;/body&gt;<br />
&lt;/html&gt;</p>
<p><em>Servlet program for the above html page is using the login servlet we are validating the login members<br />
</em></p>
<p>import javax.servlet.*;<br />
import javax.servlet.http.*;<br />
import java.sql.*;<br />
import java.io.*;<br />
public class Login extends HttpServlet<br />
{<br />
Connection con;<br />
public void init()<br />
{<br />
try{<br />
Class.forName(&#8220;sun.jdbc.odbc.JdbcOdbcDriver&#8221;);<br />
con=DriverManager.getConnection(&#8220;jdbc:odbc:iams&#8221;);<br />
}<br />
catch(ClassNotFoundException e)<br />
{  System.out.println(e);  }<br />
catch(SQLException e)<br />
{ System.out.println(e); }<br />
}<br />
public void doGet(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException<br />
{<br />
try<br />
{<br />
res.setContentType(&#8220;text/html&#8221;);<br />
PrintWriter out=res.getWriter();<br />
String log=req.getParameter(&#8220;text1&#8243;);<br />
String pwd=req.getParameter(&#8220;text2&#8243;);</p>
<p>PreparedStatement stmt=con.prepareStatement(&#8220;select * from login where username=?&#8221;);<br />
stmt.setString(1,log);<br />
ResultSet rs=stmt.executeQuery();<br />
if(rs.next())<br />
{<br />
String user=rs.getString(&#8220;username&#8221;);<br />
String pword=rs.getString(&#8220;password&#8221;);<br />
if(pword.equals(pwd))<br />
{<br />
HttpSession s1=req.getSession();<br />
s1.setAttribute(&#8220;userid&#8221;,user);<br />
HttpSession s2=req.getSession();<br />
s2.setAttribute(&#8220;password&#8221;,pword);<br />
ServletContext ctx=getServletContext();<br />
RequestDispatcher rd=ctx.getRequestDispatcher(&#8220;/XXX.html&#8221;);<br />
rd.forward(req,res);<br />
}<br />
else<br />
{<br />
ServletContext ctx=this.getServletContext();<br />
RequestDispatcher rd=ctx.getRequestDispatcher(&#8220;/AError.html&#8221;);<br />
rd.forward(req,res);<br />
}<br />
}<br />
else {<br />
ServletContext ctx=this.getServletContext();<br />
RequestDispatcher rd=ctx.getRequestDispatcher(&#8220;/AError.html&#8221;);<br />
rd.forward(req,res);<br />
}</p>
<p>}catch(SQLException e)<br />
{  System.out.println(e); }<br />
}</p>
<p>public void destroy()<br />
{<br />
try{<br />
con.close();<br />
} catch(SQLException e)<br />
{ System.out.println(e);  }<br />
}<br />
}</p>
<p><strong>Configure the web.xml for this servlet by using the code:</strong></p>
<p><em>web.xml (Excerpt illustrating initialization parameters)</em></p>
<p>&lt;?xml version=&#8221;1.0&#8243; encoding=&#8221;ISO-8859-1&#8243;?&gt;<br />
&lt;!DOCTYPE web-app PUBLIC<br />
&#8220;-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN&#8221;<br />
&#8220;http://java.sun.com/j2ee/dtds/web-app_2_2.dtd&#8221;&gt;</p>
<p>&lt;web-app&gt;<br />
&lt;servlet&gt;<br />
&lt;servlet-name&gt;SomeName&lt;/servlet-name&gt;<br />
&lt;servlet-class&gt;Login&lt;/servlet-class&gt;<br />
/* &lt;init-param&gt;<br />
&lt;param-name&gt;parameter1&lt;/param-name&gt;<br />
&lt;param-value&gt;First Parameter Value&lt;/param-value&gt;<br />
&lt;/init-param&gt;*/</p>
<p>&lt;/servlet&gt;<br />
&lt;servlet-mapping&gt;<br />
&lt;servlet-name&gt;SomeName&lt;/servlet-name&gt;<br />
&lt;url-pattern&gt;/the action path given in the html action&lt;/url-pattern&gt;<br />
&lt;servlet-mapping&gt;<br />
&lt;!&#8211; &#8230; &#8211;&gt;<br />
&lt;/web-app&gt;</p>
<h2  class="related_post_title">Random Posts</h2><ul class="related_post"><li>December 17, 2008 -- <a href="http://www.bestjavainterviewquestions.com/what-is-port-number/" title="What is Port Number">What is Port Number</a> (0)</li><li>October 5, 2008 -- <a href="http://www.bestjavainterviewquestions.com/goto-statement-in-java/" title="GoTo statement in Java">GoTo statement in Java</a> (0)</li><li>June 25, 2008 -- <a href="http://www.bestjavainterviewquestions.com/jdbc-interview-questions/" title="Jdbc Interview Questions">Jdbc Interview Questions</a> (0)</li><li>September 7, 2008 -- <a href="http://www.bestjavainterviewquestions.com/define-stateless-session/" title="Define Stateless Session">Define Stateless Session</a> (0)</li><li>September 20, 2008 -- <a href="http://www.bestjavainterviewquestions.com/difference-between-sessionsave-and-sessionsaveorupdate-3/" title="Difference between session.save() and session.saveOrUpdate()">Difference between session.save() and session.saveOrUpdate()</a> (0)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.bestjavainterviewquestions.com/example-programs-of-lifecycle-of-servlets/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

