<?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 servelet Archives -</title>
	<atom:link href="https://mitindia.in/tag/java-servelet/feed/" rel="self" type="application/rss+xml" />
	<link>https://mitindia.in/tag/java-servelet/</link>
	<description></description>
	<lastBuildDate>Fri, 08 Jul 2016 05:49:16 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://mitindia.in/wp-content/uploads/2023/03/cropped-android-chrome-512x512-1-32x32.png</url>
	<title>java servelet Archives -</title>
	<link>https://mitindia.in/tag/java-servelet/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Java Servlets</title>
		<link>https://mitindia.in/java-servlets/</link>
		
		<dc:creator><![CDATA[SKB]]></dc:creator>
		<pubDate>Mon, 04 Jul 2016 13:37:40 +0000</pubDate>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[java servelet]]></category>
		<guid isPermaLink="false">http://www.mitindia.in/?p=236</guid>

					<description><![CDATA[<p>An alternate form of server-side computation that uses Java. The Web server is extended to support an API, and then Java programs use the API to create dynamic web pages. Using Java servlets provides a platform-independent replacement for CGI scripts. Servlets can be embedded in many different servers because the servlet API, which you use [&#8230;]</p>
<p>The post <a href="https://mitindia.in/java-servlets/">Java Servlets</a> appeared first on <a href="https://mitindia.in"></a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>An alternate form of server-side computation that uses Java.</p>
<p>The Web server is extended to support an API, and then Java programs use the API to create dynamic web pages.</p>
<p>Using Java servlets provides a platform-independent replacement for CGI scripts.</p>
<p>Servlets can be embedded in many different servers because the servlet API, which you use to write servlets, assumes nothing about the server&#8217;s environment or protocol.</p>
<p><span style="color: #ff0000;">Servlet Life Cycle:</span><br />
Initialization: the servlet engine loads the servlet’s *.class file in the JVM memory space and initializes any objects</p>
<p>Execution: when a servlet request is made,<br />
a ServletRequest object is sent with all information about the request a ServletResponse object is used to return the response.</p>
<p>Destruction: the servlet cleans up allocated resources and shuts down<br />
<img fetchpriority="high" decoding="async" class="alignnone" src="http://4.bp.blogspot.com/-TUIQ2iF5vdo/Va3lpZuXANI/AAAAAAAAAb8/DKf3XDeoDn4/s400/servlet_life_cycle.JPG" width="400" height="298" border="0" /><br />
Simple Servlet example to find records in Database</p>
<p>1. Create an HTML file with following syntax.</p>
<pre>&lt;html&gt;
&lt;head&gt;
&lt;title&gt;
Find Employee Information
&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;form method="get" action="http://localhost:70/classes"&gt;
&lt;h2 align=center&gt;Find Employee Information&lt;center&gt;&lt;/h2&gt;
&lt;table&gt;
&lt;tr&gt;
&lt;th&gt;Enter Employee Id&lt;/th&gt;
&lt;td&gt; &lt;input type=text name="id"&gt; &lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;input type=submit value=Submit&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;</pre>
<p>2. Now, create a Java Servlet program with following code in it.</p>
<pre>import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class EmployeeDetails extends HttpServlet{
  static int i;
  Connection con;
  PrintWriter out;
  ResultSet rs;
  public void init(){
    i=0;
    con=null;
    out=null;
    rs=null;
  }


  public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
    i++;
    out=response.getWriter();
    out.println("&lt;b&gt;You are user number " + i + "to visit this site&lt;/b&gt;&lt;br&gt;&lt;br&gt;");
    try{
      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
      con=DriverManager.getConnection("jdbc:odbc:test");
      PreparedStatement pstmt=null;
      String query=null;
      query="select fname,lname,address,salary from Emp_Det where id=?";
      pstmt=con.prepareStatement(query);
      pstmt.setInt(1,Integer.parseInt(request.getParameter("id")));
      rs=pstmt.executeQuery();
      out.println("&lt;b&gt;&lt;center&gt;Employee Details&lt;/center&gt;&lt;/b&gt;&lt;br&gt;&lt;br&gt;");
      ResultSetMetaData rsmd=rs.getMetaData();
      int colcount=rsmd.getColumnCount();
      out.println("&lt;table align=center border=1 cellpadding=2&gt;");
      out.println("&lt;tr&gt;");
      for(int i=1; i&lt;=colcount; i++){
        out.println("&lt;th&gt;" + rsmd.getColumnLabel(i) + "&lt;/th&gt;");
      }
      out.println("&lt;tr&gt;");
      while(rs.next()){
        out.println("&lt;tr&gt;");
        out.println("&lt;td&gt;" + rs.getString("fname") + "&lt;/td&gt;");
        out.println("&lt;td&gt;" + rs.getString("lname") + "&lt;/td&gt;");
        out.println("&lt;td&gt;" + rs.getString("address") + "&lt;/td&gt;");
        out.println("&lt;td&gt;" + rs.getString("salary") + "&lt;/td&gt;");
        out.println("&lt;/tr&gt;");
      }
      out.println("&lt;/table&gt;");
      out.println("&lt;/body&gt;");
    }
    catch(Exception e){
      out.println(e.toString());
    }
  }
  public void destroy(){
    try{
      i=0;
      con.close();
      out.close();
      rs.close();
    }
    catch(SQLException se){
      out.println(se.toString());
    }
  }
}
</pre>
<div></div>
<p>Now, run the above HTML file in the browser from localhost and see the output.</p>
<p><a class="a2a_button_whatsapp" href="https://www.addtoany.com/add_to/whatsapp?linkurl=https%3A%2F%2Fmitindia.in%2Fjava-servlets%2F&amp;linkname=Java%20Servlets" title="WhatsApp" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fmitindia.in%2Fjava-servlets%2F&amp;linkname=Java%20Servlets" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fmitindia.in%2Fjava-servlets%2F&#038;title=Java%20Servlets" data-a2a-url="https://mitindia.in/java-servlets/" data-a2a-title="Java Servlets"><img src="https://static.addtoany.com/buttons/favicon.png" alt="Share"></a></p><p>The post <a href="https://mitindia.in/java-servlets/">Java Servlets</a> appeared first on <a href="https://mitindia.in"></a>.</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
