Showing posts with label JSP & SERVLETS. Show all posts
Showing posts with label JSP & SERVLETS. Show all posts

Monday, 19 February 2018

2.0 JSP & SERVLETS (BEST PROJECT EXAMPLE)

MOVIE PROJECT (CRUD OPERATIONS + LOGIN + LOGOUT EXAMPLE)







































Download Project : Movie Project

Read More »

1.0 JSP & Servlets

BASIC CONCEPTS OF CLIENT AND SERVER

Web Server - A web server takes client request and give something back as a result.















Client and server know HTML and HTTP
HTML - Tells the browser how to display
HTTP -  Hypertext Transfer Protocol , client and server use it to communicate.

HTTP runs on top of TCP and IP protocol
TCP - Transmission/Transfer control protocol is responsible for making sure that the the transmitted file from source reach at destination as a complete singe file
IP - Internet Protocol decides the best route to deliver the packet at the destination.


















HTTP response contains header having all the information regarding the response.
In HTTP request - GET and POST



























GET VS POST














PORTS - A single server supports many ports. A server application identified by a port.

FTP - 21
TELNET - 23
SMTP - 25
TIME - 37
HTTP - 80
POP3 - 110
HTTPS - 443

0 - 1023 are reserved ports. A server can have 65536 ports.

But sometimes you just need more than a web server.Web server simply fetch the static page request and give it back to client. On web server machine there are many applications which can provide the desired dynamic output where computation involves.


What is a CONTAINER -  TOMCAT is an example of container
When a web server application like apache gets a request for servlet, then its not the apache who hands over the HTTP request to the servlet , It is a CONTAINER (TOMCAT) that calls doPost() and doGet() methods of a servlet.


  1. Container provides communication support between web server and your servlet. You don't have to build a server socket etc.
  2. Container controls the lifecycle of your servlet
  3. Multithreading support for every request
  4. Translation of JSP to java.

How does container handles a request

  1. User clicked a link - which is a request for a servlets
  2. The containers sees that the request is for servlet so it create two objects (HttpServletRequest and HttpServletResponse)
  3. Container finds the correct servlet based on the request and allocate a thread for the request, and passes the request and response object to the servlet thread
  4. The container calls the service() method of servlet. Depending upon the request service methods calls either doGet() or doPost().
  5. The doGet() or doPost() method will generate the response and pass it to the HttpResponse Object.
  6. The Container create the HttpResponse Object into HTTP response and send it back to the client.
SERVLET LIFE CYCLE




MVC






















VIEW - Responsible for the presentation
CONTROLLER - Takes user inputs and figure out what it means to the model.
MODEL - Holds the real business logic.



Read More »

3.0 LOG4J

log4j is a very popular logging library for Java development. It provides convenient and flexible logging mechanism as well as fast performance.
Putting log4j into your code is pretty easy
levels of Logging can be : ALL, DEBUG, INFO, WARN, ERROR, FATAL, and OFF.


1. Example



import org.apache.log4j.BasicConfigurator;

import org.apache.log4j.Logger;


public class Test {
static final Logger logger=Logger.getLogger(Test.class);
public static void main(String args[]){
BasicConfigurator.configure();
logger.debug("Sample debug message");
logger.info("Sample info message");
logger.warn("Sample warn message");
logger.error("Sample error message");
logger.fatal("Sample fatal message");
}
}


0 [main] DEBUG Test  - Sample debug message
0 [main] INFO Test  - Sample info message
0 [main] WARN Test  - Sample warn message
0 [main] ERROR Test  - Sample error message
0 [main] FATAL Test  - Sample fatal message


Above example is only for basic knowledge.

Typically, configuring log4j involves  following tasks:
1. Configure the root logger

log4j.rootLogger=DEBUG, ConsoleAppender, FileAppender
log4j.rootLogger=level[, appender1, appender2…]

2.Configure individual loggers
log4j.logger.com.test.Database=INFO, ConsoleAppender
log4j.logger.<logger-name>=level[, appender1, appender2…]

3.Specify appenders
Appedners specifies how messages are logged.

log4j.appender.<appender_name>=<appender_class_name>

log4j.appender.<appender_name>.<property1_name>=<property1_value>
log4j.appender.<appender_name>.<property2_name>=<property2_value>

4. Specify the message format
log4j.appender.<appender_name>.layout=<layout_class_name>
log4j.appender.<appender_name>.layout.ConversionPattern=<conversion_pattern>

Each conversion specifier must follow this format:

<percent sign (%)>[format modifiers]<conversion characters>


The specifiers used in the above example:

 t: name of the current executing thread.
 p: priority
 c: category
 m: log message.
 n: line separator character.





















There are two ways to use log4j 

1. LOG4J.PROPERTIES
#This is the first line of your log4j.properties, where you have to mention the level of #logging you want (ALL,DEBUG,INFO,WARN,ERROR,FATAL) then you have to mention the #appenders name. Appender is where you want to print your logs (on console or in a log #file)
log4j.rootLogger=DEBUG,Appender1, Appender2

#Defination of Appenders - no need to memorize the classname - you can get it from #log4j jar.
# A) mention the name of appender (ConsoleAppender or FileAppender)
# B) mention the appender layout (PatternLayout)
# C) mention the appender format (p=level d=date t=thread c=class x=package #m=message n=newline)

log4j.appender.Appender1=org.apache.log4j.ConsoleAppender
log4j.appender.Appender1.layout=org.apache.log4j.PatternLayout
log4j.appender.Appender1.layout.ConversionPattern=%-7p %d [%t] %c %x - %m%n

log4j.appender.Appender2=org.apache.log4j.FileAppender
log4j.appender.Appender2.File=bikesh.log
log4j.appender.Appender2.layout=org.apache.log4j.PatternLayout

log4j.appender.Appender2.layout.ConversionPattern=%-7p %d [%t] %c %x - %m%n


























































package com.test;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

public class Test {
final static Logger logger=Logger.getLogger(Test.class);
public static void main(String args[])
{
PropertyConfigurator.configure("log4j.properties");
logger.debug("This is debug");
logger.info("This is info");
logger.error("This is error");
logger.warn("This is Warn");
logger.fatal("This is fatal");
}


}


OUTPUT
DEBUG   2018-02-20 01:30:09,901 [main] com.test.Test  - This is debug
INFO    2018-02-20 01:30:09,901 [main] com.test.Test  - This is info
ERROR   2018-02-20 01:30:09,901 [main] com.test.Test  - This is error
WARN    2018-02-20 01:30:09,901 [main] com.test.Test  - This is Warn

FATAL   2018-02-20 01:30:09,901 [main] com.test.Test  - This is fatal





2.LOG4J.XML

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration PUBLIC "-//log4j/log4j Configuration//EN" "log4j.dtd">

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">



   <appender name="appender1" class="org.apache.log4j.ConsoleAppender">
       <layout class="org.apache.log4j.PatternLayout">
          <param name="ConversionPattern" value="%-7p %d [%t] %c %x - %m%n"/>
       </layout>
    </appender>

    <appender name="appender2" class="org.apache.log4j.FileAppender">
       <param name="File" value="applog.txt" />
       <layout class="org.apache.log4j.PatternLayout">
          <param name="ConversionPattern" value="%-7p %d [%t] %c %x - %m%n"/>
       </layout>
    </appender>

    <root>
        <priority value="DEBUG"/>
        <appender-ref ref="appender1" />
        <appender-ref ref="appender2" />
    </root>

</log4j:configuration>


import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.apache.log4j.xml.DOMConfigurator;


public class Test {
final static Logger logger=Logger.getLogger(Test.class);
public static void main(String[] args) {
DOMConfigurator.configure("log4j.xml");
logger.debug("This is debug");
logger.info("This is info");
logger.error("This is error");
logger.warn("This is Warn");
logger.fatal("This is fatal");

}


}

OUTPUT
DEBUG   2018-02-20 02:07:00,655 [main] Test  - This is debug
INFO    2018-02-20 02:07:00,655 [main] Test  - This is info
ERROR   2018-02-20 02:07:00,655 [main] Test  - This is error
WARN    2018-02-20 02:07:00,655 [main] Test  - This is Warn
FATAL   2018-02-20 02:07:00,655 [main] Test  - This is fatal

Read More »