eProgrammingLab

Java


1.	What is JSE?
Java Standard Edition (JSE) is core java, which is used to develop standalone applications e.g. calculator.
2.	What is JEE?
Java Enterprise Edition (JEE) is used to create standalone/web applications e.g. facebook, gmail. The main technologies in JEE are:
JDBC: in Java Database Connectivity, we perform select, insert, update, and delete operations. It is standalone application.
Servlet: –we can develop standalone web applications.
JSP: in Java Server Pages, web applications are developed.
3.	What is servlet?
By using servlet, we develop web applications.
Static web applications: fixed data e.g. about us, contact us, home page. Dynamic web applications: it will change data. Based on our actions, its data will be changed. E.g. Google search.
4.	What are the types of web pages?
Static web pages: display fixed content for all the requests. E.g. about us page, terms and condition page, contact us page etc.
Dynamic web pages: the content of web page changes based on the time of request generation or based on the input values of request. E.g. live game scores page, Gmail inbox page etc.
•	Static web components (generates the static web page) e.g. THML pages.
•	Dynamic web components (generates the dynamic web page) e.g. servlet, jsp, asp.net, php components.
5.	What is web server?
The execution of standalone application takes place only initiate the execution. The web component of web application will be executed automatically and dynamically in automated environment. The moment they are requested from browser for this automation we need special software called web server software. Here we use Tomcat 9 as a server. The web server is special software that listens to client requests continuously, takes the request from client passes the request to appropriate web component, executes the web component dynamically and sends the output to browser as response in the form of web page. E.g. Tomcat, IIS (Internet Information Server), Apache Web Server (AWS).
6.	What is java servlet?
Java servlet is generic server extension that means a java class can be loaded dynamically. To expand the functionality of a server. Servlets are used with web servers run inside a java virtual machine on the server so those are safe and portable.
7.	What is servlet container?
A servlet container is nothing but a compiled, executable program. The main function of the container is to load, initialize, and execute servlet.
8.	What are the types of servlet container?
There are two types of servlets.
A simple servlet container is not fully functional and therefore it can only run very simple servlet, and does the following:
•	Wait for Http request.
•	Construct ServletRequest object and ServerResponse object.
•	If request is for a static resource, invoke the process method of the StaticResourceProcessor instance, passing the ServletRequest and ServletResponse objects.
•	If request is for a servlet, load the servlet class and invoke its service method, passing the _______.
A fully functional servlet container additionally does the following for each http request servlet:
•	When the servlet is called for first time, load the servlet class and call its init method.
•	For each request, construct an instance of java.servlet.ServletRequest and an instance of javax.ServletResponce.
•	Invoke the servlet’s service method, passing the ServletRequest and ServletResponse objects.
•	When the servlet class is shut down, call the servlet’s destroy method and unload the servlet class.
9.	Based on the physical location, how many types of containers are there?
•	Standalone container: here container and web server are together comes as a single software service.
•	In process container: here container comes as separate software service inside the web server software. E.g. servlet container/jsp container to tomcat server.
•	Out of process container: here container resides server software service but will be linked with web server software. E.g. servlet container/jsp container that is linked with IIS server to execute java web application in IIS.
10.	What are the responsibilities of web server?
•	Lister to client requests continuously by starting demon process.
•	Takes the requests and passes the request to container.
•	Provides container to execute server side web comps.
•	Gives middleware services.
•	Provides environment do deploy, manage and un-deploy the web applications.
•	Gathers output given by webcoms and sends those outpouts to browser as response.
11.	What are the responsibilities of containers?
•	Take the request from server and passes to appropriate web component.
•	Execute server side web component.
•	Enables the communication between multiple web components.
•	Passes the output of web component to web erver.
12.	What are the advantages of java servlet?
•	Portability
•	Powerful
•	Efficiency
•	Safety
•	Integration
•	Extensibility
•	Inexpensive

Servlet 2
Download and setup Tomcat 9 software.
First servlet application:
•	When you make any application, you need to implement from Servlet Interface.
•	Whenever we develop web application, we need two objects: request and response objects.
•	In web application, we need to give request. Based on request, it generates response.
•	Servlet is predefined servlet interface. All classes and objects we use here are available in servlet API.
•	Interface and class inheritance: implements. Interface and interface or class and class: extends.
•	Servlet has five methods:
•	What is generic servlet (abstract class)?
•	What is concrete and abstract class?
•	 _______.

13.	What are the five steps _______?
•	Create and extend your class to HttpServlet
•	Override service method
•	Set content type to text/html
•	Get printwriter object
•	Print some message
•	Configure web.xml file (inside servlet, name and class; inside servlet mapping, name and pattern)
•	Close 
Lab 1:
Create a simple servlet program to display welcome message.




Servlet 3
(download tomcat 9)
(Eclipse downloaded in 8:20)
(Start in 27.4)
•	How to configure tomcat in eclipse?
Window => preferences => server => runtime environments => add = Apache Tomcat v9.0 => next => browse the location


(Start from 10 minutes)

•	If you are developing any program, you need to implement from servlet interface.
•	Whenever we develop web application, we need two objects: request object and response object. We need to give a request. Based on request, it generates response.
•	Servlet is predefined interface. All classes are available in servlet API. 
•	Inheritance: between two classes => extends, between two interface => extends, between class and interface => implements.
1.	How many methods are available in servlet interface? Name all.
Five methods are available. https://tomcat.apache.org/tomcat-5.5-doc/servletapi/javax/servlet/Servlet.html (12.50). These are: _______
2.	What is difference between class, interface, and abstract class?
Interface: we can declare method, but we cannot implement. Abstract class: both concrete (method with body) and abstract (method without body) methods are there. We cannot write logic. Class: we can write concrete class only.
3.	What is genericServlet?
It is an abstract class. _______  Overloading, overriding, super class, 
it does not support http protocol
4.	What is httpServlet class_______?
It is abstract class. It has two services methods and seven do get _______ methods.

(Serlet(t) GenericServlet(ac), HttpServilet(ac)


•	Step 1: Extend you class from HttpServlet.
•	Step 2: override service method
•	Step 3: set content type
•	Step 4: 
•	Step 5: 

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

// 1. Create serlet class extending from servlet
public class ServletExample1 extends HttpServlet {

     public static void main(String []args){
         
         // 2. Override service method
         @Override
         protected void doGet(HttpServletRequest req, HttpServletResponse res) throws servletException() {
             
         // 3. Set content type
         res.setContentType("text/html");
         
         // 4. Get printwriter object
         PrintWriter pw=res.getWriter();
         
         // 5. Print some message with html tags
         pw.println("

ServletExample1") System.out.println("ServletExample1"); } } } import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; // 1. Create serlet class extending from servlet public class ServletExample2 extends HttpServlet { public static void main(String []args){ // 2. Override service method @Override protected void doGet(HttpServletRequest req, HttpServletResponse res) throws servletException() { // 3. Set content type res.setContentType("text/html"); • • // 4. Get printwriter object • PrintWriter pw=res.getWriter(); • • // 5. Print some message with html tags • pw.println("

ServletExample2

") • System.out.println("ServletExample2"); • } • } } • From interface, you need to override methods. Servlet has five methods, you need to override all five methods. • Overloading, overriding, super class, • From interface, you need to override methods. Servlet has five methods, you need to override all five methods. HTML to Servlet Communication 1. How many ways do we can communicate HTML with servlet? Giving request to servlet component by directly url in thw browser address bar is quiet complex process, non-technical end-user may feel complex. To overcome this problem we need to provide gui to end user having hyperlinks and submit buttons. For this we need to design webpage by using html file and should go for html to servlet communication. Html to servlet communication is possible in three ways: • Using hyperlinks • Using from submit buttons • Using java script 2. How does servlet communicate using hyperlinks (anchor type)? For this use servlet component request url as href attribute value of tag. Html to servlet communication using form page submit button: hyperlink generated requested cannot carry end users supplied data along with the request From page submitted requested can carry end users supplied data along with the data request. The form page submitted requested goes to that servlet component, whose requested url placed in action attribute of
tag. The form page submitted requested carries the values of form component as input value along with the requested to lead these values we can use req.parameter() on servlet component. 3. _______Form page can submit request in two modes/methods/methodologies. • Get mode: can carry limited amount of data along with the request (2kb-8kb). • Post mode: can carry unlimited amount of data along with the request. • Use doXxx(-,-) in servlet component instead of service(-,-) to process different modes/methods of requested coming from client (browser). 4. What are the differences between get mode request and post mode request methods? 35:52 GET POST History bookmarked back button re-submit behavior parameters hacked restrictions on form data type security restriction on form data length usability visibility cached 5. What is form validation? Verifying the format, pattern of form data is called validations and such logic is called validation logic. It is always recommended to use form data in business logic/request processing logic only after form validations. 6. How many types of form validations in web application are available? • Client side form validation • Server side form validation 7. What is client side form validation? • Place java script code/vb script code in html file. • This code comes to browser along with form page and excutes using java script engine. 8. What is server side form validation? Place java code in servlet/jsp after receiving from data and before using that form data. In business, code is executed at server side along with servlet/jsp components code. 9. How many approaches are there to place for form validations logic in out application? Four. 10. What is approach 1? If we place only server side form validation logic, then the network round trips between client and server will be increased if form page is rejected by server for multiple times. 11. What is approach 2? If we place only client side form validation logic, then it reduces the network round trips between client and server. Because the form validation takes place in the client itself without going to server. 12. What is approach 3? Write both client and server side validation logics, so that server side validations takes place even though client side validation are not done. But, if client side form validations are performed then it also performs server side validation. But, if client side form validations are performed then it also performs server side form validations, which degrades the performance. 13. What is approach 4? Write both client side and server side form validations, but enable server side form validations only when client side form validations are done. Servlet to Database Communication By using servlet life cycle method => using servlet container which has three methods: initialization, init method, request method, destroy method. • To save the inputs coming to servlet component in db software or save the result geneteged by servlet component in db software or get the inputs from db software to servlet component we need to go for servlet to db communication. Approaches 1: Threre are four approaches to place jdbc code in servlet coponent Create jdbc connection in the init() Use jdbc connection in the service(-,-) Close jdbc connection in the destroy in the destroy method Disadvantage: Since jdbc connection is instance variable, so it is not thread save variable by default. So, we should use synchronization concept. Advantage: all the request coming to servlet component will use signle jdbc connection to interact with db software, due to this the performance will be improved. Approach 2: only use service method • Create jdbc connection in service(-,-) • Use jdbc connection in service(-,) • Close jdbc connection in service(-,-) Disadvantage: For every request given servlet one jdbc connection will be created with db software, so the performance is poor. Advantage: _______? Approach 3: Get jdbc connection object from jdbc connection pool being from service(-, -) Use jdbc connection object in service(-,-) Return jdbc connection back to jdbc connection pool being from service(-,-) Advantage: Here jdbc connection is local to service (-,-) so it becomes thread safe. We can get all the advantages of jdbc connection pool a) Reusuability of jdbc connection objects with minimum jdbc connection objects we can make max clints/requests taking with Approach 4: Write JDBC code in DAO (database accessibility object) _______ Servlet to Servlet Communication