PHP Notes Free
PHP Notes Free
Uses of JavaScript
1) It gives html designers a programming a tool:
2) JavaScript can put dynamic text into text into html pages-
3) It can react to events:
4) JavaScript can read and write HTML elements-
5) It can be used to validate data –
6) It can be used to detect visitor’s browser
7) It can be used to create cookies –
Advantages of JavaScript:
1) Versatility: JavaScript can be used for a wide range of
purposes, from adding small interactive elements to web pages, to building complex web
applications.
2) Cross-platform compatibility: JavaScript can run on virtually any platform, including web
browsers, mobile devices, and desktop computers.
3) Easy to learn: The syntax of JavaScript is relatively simple, making it easier to learn for new
programmers.
4) Large developer community: JavaScript has a massive developer community, which means
there are many resources available, including libraries, frameworks, and tools, to help
developers be more productive.
5) Supports object-oriented programming: JavaScript supports object-oriented programming,
allowing for code reuse and cleaner code.
Disadvantages of JavaScript:
1) Security concerns: JavaScript can be used to inject malicious code into websites, which can
lead to security vulnerabilities.
2) Browser inconsistencies: Different browsers may interpret JavaScript code differently, which
can result in inconsistent behavior.
3) Performance issues: JavaScript can sometimes lead to performance issues, especially when
working with large data sets or complex operations.
4) Limited access to hardware resources: JavaScript does not have direct access to a computer's
hardware resources, such as file systems, which limits its capabilities for certain types of
applications.
5) Lack of static typing: JavaScript is a dynamically-typed language, which can make it harder to
identify certain types of errors before runtime.
2.Explain applet in 16 marks
An applet is a small program or application designed to run within another program or on a
webpage, typically written in the Java programming language. Applets were first introduced by Sun
Microsystems (now Oracle Corporation) in the mid-1990s and were widely used for creating
interactive web content. It runs inside the browser and works at client side.
Lifecycle of Java Applet
Applet is initialized.
Applet is started.
Applet is painted.
Applet is stopped.
Applet is destroyed.
I. public void init(): is used to initialized the Applet. It is invoked only once.
II. public void start(): is invoked after the init() method or browser is maximized.
It is used to start the Applet.
III. public void stop(): is used to stop the Applet. It is invoked when Applet is stop
or browser is minimized.
IV. public void destroy(): is used to destroy the Applet. It is invoked only once.
Advantages of Applets:
1) Cross-platform compatibility: Applets are platform-independent, which means that they can
run on any platform that supports a JVM, such as Windows, Mac, or Linux.
2) Small size: Applets are small in size compared to standalone applications, making them easy
to download and execute over the internet.
3) Security: Applets are executed within a sandbox environment, which prevents them from
accessing the user's file system or other resources on their computer without permission.
4) Dynamic content: Applets can communicate with the web server to provide dynamic content
and data exchange.
5) User interactivity: Applets provide a rich user interface, allowing for the creation of
interactive graphics, animations, and multimedia content.
Disadvantages of Applets:
1) Slow performance: Applets can be slower than standalone applications due to their
execution within a web browser's JVM.
2) Limited access to resources: Applets are restricted in their access to system resources, such
as the file system, which can limit their functionality.
3) Compatibility issues: Applets can sometimes experience compatibility issues with different
web browsers and operating systems.
4) Security vulnerabilities: Although applets are executed within a sandbox environment, there
have been security vulnerabilities in the past that could potentially allow malicious applets to
access a user's computer.
5) Limited capabilities: Applets are limited in their capabilities compared to modern web
technologies such as HTML5, JavaScript, and CSS, which offer more powerful and flexible
options for creating interactive web content.
3.Explain Java AWT components?
java AWT (Abstract Window Toolkit) is an API to develop Graphical User Interface (GUI) or windows-
based applications in Java.these are platform-dependent i.e. components are displayed according to
the view of operating system. AWT is heavy weight i.e. its components are using the resources of
underlying operating system (OS).
Components
All the elements like the button, text fields, scroll bars, etc. are called components. In Java AWT,
there are classes for each component as shown in above diagram. In order to place every component
in a particular position on a screen, we need to add them to a container.
1) Button: A button is a graphical control element that is used to initiate an action or submit a
form.
2) Label: A label is a component that displays text or an image on the screen.
3) TextField: A text field is a component that allows users to input text, such as a name, address,
or password.
4) TextArea: A text area is a multi-line text box that allows users to input or display larger
amounts of text.
5) Checkbox: A checkbox is a graphical control element that allows users to select one or more
options from a set of choices.
6) Choice: A choice is a drop-down menu that allows users to select one option from a set of
choices.
7) List: A list is a component that displays a list of items that can be selected by the user.
8) Scrollbar: A scrollbar is a component that allows users to scroll through large amounts of
content, such as a web page or a document.
9) Menu: A menu is a graphical control element that provides a list of options for the user to
select.
10) MenuBar: A menu bar is a container component that is used to group together multiple
menus in a Java application.
11) MenuItem: A menu item is a graphical control element that represents an option in a menu.
Container-The Container is a component in AWT that can contain another components like buttons,
textfields, labels etc. The classes that extends Container class are known as container
There are four types of containers in Java AWT:
1) Window The window is the container that have no borders and menu bars. You must use
frame, dialog or another window for creating a window. We need to create an instance of
Window class to create this container.
2) Panel: A panel is a container component that is used to group other components together.
3) Frame: A frame is a top-level window that contains the main content of a Java application.
4) Dialog: A dialog is a window that is used to display messages or request input from the user.
4.What is Jsp and lifecycle of JSp?how it is different from Servlet?
o It stands for Java Server Pages.
o It is a server side technology.
o It is used for creating web application.
o It is used to create dynamic web content.
o In this JSP tags are used to insert JAVA code into HTML pages.
o It is an advanced version of Servlet Technology.
o It is a Web based technology helps us to create dynamic and platform independent web
pages.
o In this, Java code can be inserted in HTML/ XML pages or both.
o JSP is first converted into servlet by JSP container before processing the client’s request.
o Java Scriplets :- It allows us to add any number of JAVA code, variables and
expressions.
Syntax:-
<% java code %>
Lifecycle of Jsp
o Translation of JSP page to Servlet :
This is the first step of the JSP life cycle. This translation phase deals with the Syntactic
correctness of JSP. Here test.jsp file is translated to test.java.
o Compilation of JSP page :
Here the generated java servlet file (test.java) is compiled to a class file (test.class).
o Classloading :
Servlet class which has been loaded from the JSP source is now loaded into the container.
o Instantiation :
Here an instance of the class is generated. The container manages one or more instances by
providing responses to requests.
o Initialization :
jspInit() method is called only once during the life cycle immediately after the generation of
Servlet instance from JSP.
o Request processing :
_jspService() method is used to serve the raised requests by JSP. It takes request and
response objects as parameters. This method cannot be overridden.
o JSP Cleanup :
In order to remove the JSP from the use by the container or to destroy the method for
servlets jspDestroy()method is used. This method is called once, if you need to perform any
cleanup task like closing open files, releasing database connections jspDestroy() can be
overridden.
Servlet JSP
1. Servlet is a java code. 1. JSP is a HTML based code.
2. Writing code for servlet is harder than 2. JSP is easy to code as it is java in HTML.
JSP as it is HTML in java.
3. Servlet plays a controller role in the 3. JSP is the view in the MVC approach for
hasMVC approach. showing output.
4. Servlet is faster than JSP. 4. JSP is slower than Servlet because the
first step in the hasJSP lifecycle is the
translation of JSP to java code and then
compile.
5. Servlet can accept all protocol requests. 5. JSP only accepts HTTP requests.
6. In Servlet, we can override the service() 6. In JSP, we cannot override its service()
method. method.
7. In Servlet by default session 7. In JSP session management is
management is not enabled, user have automatically enabled.
to enable it explicitly.
8. In Servlet we have to implement 8. In JSP business logic is separated from
everything like business logic and presentation logic by using
presentation logic in just one servlet file. JavaBeansclient-side.
9. Modification in Servlet is a time- 9. JSP modification is fast, just need to click
consumingcompiling task because it the refresh button.
includes reloading, recompiling, a.
JavaBeans and restarting the server.
Lifecycle of servlet
A servlet life cycle can be defined as the entire process from its creation till the destruction. The
following are the paths followed by a servlet.
The servlet is initialized by calling the init() method.
The servlet calls service() method to process a client's request.
The servlet is terminated by calling the destroy() method.
Finally, servlet is garbage collected by the garbage collector of the JVM.
Now let us discuss the life cycle methods in detail.
The init() Method
The init method is called only once. It is called only when the servlet is created, and not called for
any user requests afterwards. So, it is used for one-time initializations, just as with the init method of
applets.
The servlet is normally created when a user first invokes a URL corresponding to the servlet, but you
can also specify that the servlet be loaded when the server is first started.
When a user invokes a servlet, a single instance of each servlet gets created, with each user request
resulting in a new thread that is handed off to doGet or doPost as appropriate. The init() method
simply creates or loads some data that will be used throughout the life of the servlet.
The init method definition looks like this −
public void init() throws ServletException {
// Initialization code...
}
The service() Method
The service() method is the main method to perform the actual task. The servlet container (i.e. web
server) calls the service() method to handle requests coming from the client( browsers) and to write
the formatted response back to the client.
Each time the server receives a request for a servlet, the server spawns a new thread and calls
service. The service() method checks the HTTP request type (GET, POST, PUT, DELETE, etc.) and calls
doGet, doPost, doPut, doDelete, etc. methods as appropriate.
Here is the signature of this method −
public void service(ServletRequest request, ServletResponse response)
throws ServletException, IOException {
}
The service () method is called by the container and service method invokes doGet, doPost, doPut,
doDelete, etc. methods as appropriate. So you have nothing to do with service() method but you
override either doGet() or doPost() depending on what type of request you receive from the client.
The doGet() and doPost() are most frequently used methods with in each service request. Here is the
signature of these two methods.
The doGet() Method
A GET request results from a normal request for a URL or from an HTML form that has no METHOD
specified and it should be handled by doGet() method.
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// Servlet code
}
The doPost() Method
A POST request results from an HTML form that specifically lists POST as the METHOD and it should
be handled by doPost() method.
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// Servlet code
}
The destroy() Method
The destroy() method is called only once at the end of the life cycle of a servlet. This method gives
your servlet a chance to close database connections, halt background threads, write cookie lists or
hit counts to disk, and perform other such cleanup activities.
After the destroy() method is called, the servlet object is marked for garbage collection. The destroy
method definition looks like this −
public void destroy() {
// Finalization code...
}
Architecture Diagram
The following figure depicts a typical servlet life-cycle scenario.
First the HTTP requests coming to the server are delegated to the servlet container.
The servlet container loads the servlet before invoking the service() method.
Then the servlet container handles multiple requests by spawning multiple threads,
each thread executing the service() method of a single instance of the servlet.
24.Difference between Servlet and Cgi(Common Gateway
Interface)?
Servlet CGI
Basis
Approach It is thread based i.e. for every It is process-based i.e. for every new
new request new thread is request new process is created.
created.
Language The codes are written in JAVA The codes are written any programming
Used programming language. language.
Object- Since codes are written in Java, Since codes are written in any language,
Oriented it is object oriented and the all the languages are not object-oriented
user will get the benefits of thread-based. So, the user will not get the
OOPs benefits of OOPs
Portability It is portable. It is not portable.
Persistence It remains in the memory until It is removed from the memory after the
it is not explicitly destroyed. completion of the process-basedrequest.
Server It can use any of the web- It can use the web-server that supports it.
Independent server.
Data Sharing Data sharing is possible. Data sharing is not possible.
Link It links directly to the server. It does not link the web server directly to
the server.
HTTP server It can read and set HTTP It can neither read nor set HTTP servers.
servers.
Cost Construction and destruction Construction and destruction of the new
of new threads are not costly. processes are costly.
Speed Its can speed is slower. It can speed is faster.
Platform It can be Platform It can be Platform dependent.
dependency Independent