public void doGet (HttpServletRequest, HttpServletResponse) throws ServletException, IOException public void doPost (HttpServletRequest, HttpServletResponse) throws ServletException, IOException
Note:All real world applications always extends HttpServlet only and it is always recommended to overwrite either doGet () method or doPost () method.
Associated with servlet we have three names which are specified in web.xml, they are public URL name (known to everybody), deployer URL name or dummy name (known to that person who is deploying) and secret or internal URL name (known to servlet container or server).
Write a servlet which retrieves the data from database?
Answer:
Servlet program:
package ddrs; import javax.servlet.*; import javax.servlet.http.*; import java.sql.*; import java.io.*; public class RetrieveDataBaseServ extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse res) { res.setContentType("text/html"); PrintWriter pw = res.getWriter(); try { DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver()); Connection con = DriverManager.getConnection("oracle:jdbc:thin:@localhost:1521:Hanuman", "scott", "tiger"); Statement st = con.createStatement(); ResultSet rs = st.executeQuery("select * from emp"); while (rs.next()) { pw.println(rs.getString(1) + " " + rs.getString(2)); } } catch (Exception e) { res.sendError(504, "PROBLEM IN SERVLET..."); } } };
<web-app> <servlet> <servlet-name>pskills</servlet-name> <serclet-class>ddrs.RetrieveDataBaseServ</servlet-class> </servlet> <servlet-mapping> <servlet-name>pskills</servlet-name> <url-pattern>psurl</url-pattern> </servlet-mapping> </web-app>
In the above program we are making use of 'n' number of servlet classes, OracleDriver class; it is required to set the classpath for servlet-Api.jar and classes111.jar
For weblogic:
set classpath=F:\bea\weblogic81\server\lib\weblogic.jar;F:\oracle\ora92\jdbc\ lib\classes111.jar;
For Tomcat:
set classpath=F:\Program Files\Apache Software Foundation\Tomcat 5.5\common\ lib\servlet-api.jar;F:\oracle\ora92\jdbc\lib\classes111.jar;
When we run the above program on weblogic, it is not necessary to copy classes111.jar into lib folder of document root. Since, the weblogic server itself contains an existing jar file called ojdbc14.jar to deal with OracleDriver.