Posts

Showing posts from December, 2022

Types of HttpRequest methods

 1. GET 2. POST 3. HEAD 4. PUT 5. DELETE 6. OPTIONS 7. TRACE GET, POST, HEAD are introduced in http protocol version 1.0v PUT, DELETE, OPTIONS, TRACE are introduced in http protocol version 1.0v GET and POST are used in web applications.

Generic Servlet Abstract class

 GenericServlet Abstract class contains below methods  public javax.servlet.GenericServlet();   public void destroy();   public java.lang.String getInitParameter(java.lang.String);   public java.util.Enumeration<java.lang.String> getInitParameterNames();   public javax.servlet.ServletConfig getServletConfig();   public javax.servlet.ServletContext getServletContext();   public java.lang.String getServletInfo();   public void init(javax.servlet.ServletConfig) throws javax.servlet.ServletException;   public void init() throws javax.servlet.ServletException;   public void log(java.lang.String);   public void log(java.lang.String, java.lang.Throwable);   public abstract void service(javax.servlet.ServletRequest, javax.servlet.ServletResponse) throws javax.servlet.ServletException, java.io.IOException;   public java.lang.String getServletName();

HttpServlet Abstract class

 Generic servlet meant for any protocol interaction like http, smtp, ..  For a web application, the protocol used is http, so to work with specific protocol, sun micro system had come up with SRS called HttpServlet. 

service() method in Servlet life cycle

 In order to display the output in browser, we need to below 3 steps in service() method                  // 1. Set the response type to HTML response.setContentType("text/html"); // 2. Use PrintWriter object  to send the response PrintWriter out = response.getWriter(); out.println("<html><head><title>Output</title></head>"); out.println("<body>"); out.println("<h1 style='color:red'><marquee>Welcome to servlet coding...</marquee></h1>"); out.println("</body>"); out.println("</html>"); // 3. Close the PrintWriter out.close();

Servlets

 Tomcat uses catalina container. there are also other servers to deploy out applications like  Weblogic, Glassfish etc. Servlet interface contains five abstract methods which describes the servlet life cycle. These methods will be part of servlet actions.  1. public void init(ServletConfig config) 2. public void service(ServletRequest request, ServletResponse response) 3. public void destroy() 4. public ServletConfig getServletConfig() 5. public String getServletInfo()   In order to make normal class work like a Servlet, that class should implement Servlet interface and that class should implement all the five method. here the thing is we actually write the server logic processing code in service() method. So GenericServlet Abstract class provides the implementation for remaining 4 methods and it will make the service method as Abstract and GenericServlet class implements Servlet, Sereizeiable, ServletConfig interfaces and who so ever wants their...

Connection Pooling

Image
 => If we required to communicate with database multiple times then it is not recommended to create separate Connection object every time, because creating and destroying  Connection object every time creates performance problems. => To overcome this problem, we should go for Connection Pool. => Connection Pool is a pool of already created Connection objects which are ready to use. => If we want to communicate with database then we request Connection pool to provide Connection. Once we got the Connection, by using that we can communicates with database. =>After completing our work, we can return Connection to the pool instead of destroying. Hence the main advantage of Connection Pool is we can reuse same Connection object multiple times, so that overall performance of application will be improved.

SQL Injection

 If we use the Statement object to send the query then SQL Injection will happen  SQLInjection ========== users username      upwd sachin           tendulkar virat                kohli eg: select count(*) from users where username ='"+uname+"'" and upwd =' "+upwd+"'"; username = 'sachin' password = 'tendulkar' Query nature select count(*) from users where username ='sachin' and upwd =' tendulkar' "; validation is succesful and given the authentication eg: select count(*) from users where username ='"+uname+"'" and upwd =' "+upwd+"'"; username = 'sachin'-- password = 'tendulkar' Query nature select count(*) from users where username ='sachin'-- and upwd ='tendulkar' "; validation is succesfull and given the authentication Note: 1. -- Single line sql comment 2. /* Multiline sql comment */ If we use Statement Object to send t...

Difference between Statement and PreparedStatement

Image
 

Statement and PreparedStatement

 Problem with statement Object ========================== Statement stmt = con.createStatement(); ResultSet rs = stmt.exectueQuery("select * from student"); If we use Statement Object, same query will be compiled every time and the query should be executed everytime,this would create performance problems. eg: IRCTC App(select query), BMS APP(select query) PreparedStatement Object ======================= To resolve the above problem don't use Statement Object, use "PreparedStatement(Pre-CompiledStatement)". In case of PreparedStatement, the query will be compiled only once eveythough we are executing it mulitple time with change or no change in inputs. This would overall increase the performance. signature public PreparedStatement prepareStatement(String sqlQuery) throws SQLException //Establish the connection Connection con = DriverManger.getConnection(url,username,password); //Creating a precompiled query which is used at the runtime to execute with the value S...