JSP APP DEVLOPMENT.pptx Related to Android App Development
1.
JSP APPLICATION DEVELOPMENT
SUBMITTED TO :
Dr.Gopal Singh
DCSA ,MDU,Rohtak
SUBMITTED BY :
Bhawna
MCA 2ND
SEM 23103
2.
INTRODUCTION TO JSP
PROBLEM WITH SERVLET
Servlet is persistent until it destroys.
One servlet is loaded into JVM. It does not matters the number of requests.
When there is a request ,there is a thread , not a process.
Designing in a servlet is difficult and slows down the application.
You need a JRE (Java Runtime Environment) on the server to run servlets.
HTML code is mixed up with JAVA code therefore changes done in one code can affect another code
Developers must take care of the exception handling because servlet program is not thread-safe by
default.
3.
ANATOMY OFJSP PAGE
🠶 JSP technology is used to create web application just like Servlet technology. It can be
thought of as an extension to Servlet because it provides more functionality than servlet such
as expression language, JSTL, etc.
🠶 A JSP page consists of HTML tags and JSP tags. The JSP pages are easier to maintain than
Servlet because we can separate designing and development. It provides some additional
features such as Expression Language, Custom Tags, etc.
4.
JSP Processing
Theweb server have JSP engine which act as a container to process JSP
pages.
All the requests for JSP pages are intercepted by JSP container.
JSP stands for Java Server Pages. All your JSP pages are stored on the
server.
JSP container along with web server provide the run time environment
to JSP.
5.
Advantages Of JSP
1)Extension to Servlet
JSP technology is the extension to Servlet technology. We can use all the
features of the Servlet in JSP. In addition to, we can use implicit objects,
predefined tags, expression language and Custom tags in JSP, that makes JSP
development easy.
2) Easy to maintain
JSP can be easily managed because we can easily separate our business logic
with presentation logic.
3) Fast Development: No need to recompile and redeploy
If JSP page is modified, we don't need to recompile and redeploy the project.
4) Less code than Servlet
In JSP, we can use many tags such as action tags, JSTL, custom tags, etc. that
reduces the code. Moreover, we can use EL, implicit objects, etc.
6.
The DirectoryStructure For JSP
The directory structure of JSP page is same
as Servlet. We contain the JSP page
outside the WEB-INF folder or in any
directory.
7.
Creating
🠶 To createthe first JSP page, write
some HTML code as given below, and
save it by .jsp extension. We have
saved this file as index.jsp. Put it in a
folder and paste the folder in the web-
apps directory in apache tomcat to run
the JSP page.
🠶 index.jsp
🠶 Let's see the simple example of JSP
where we are using the scriptlet tag to
put Java code in the JSP page. We will
learn scriptlet tag later.
🠶 <html>
🠶 <body>
🠶 <% out.print(2*5); %>
🠶 </body>
🠶 </html>
🠶 It will print 10 on the browser.
A simple JSP
page
8.
How to run
🠶Follow the following steps to execute
this JSP page:
🠶 Start the server
🠶 Put the JSP file in a folder and deploy
on the server
🠶 Visit the browser by the URL
http://localhost:portno/contextRoot/
jspfile
, for example,
http://localhost:8888/myapplication/
index.jsp
🠶 Do I need to follow the directory
structure to run a simple JSP?
🠶 No, there is no need of directory
structure if you don't have class files
or TLD files. For example, put JSP
files in a folder directly and deploy
that folder. It will be running fine.
However, if you are using Bean class,
Servlet or TLD file, the directory
structure is required.
a simple JSP
page?
MVC in JSPApplication with Example
MVC
MVC stands for Model-View-Controller.
It is a design pattern used for developing
web applications. It is a layout pattern used
to isolate the information, presentation
logic, business logic. Web application Logic
is divided into three logics:
1. Presentation Logic
2. Business Logic
3. Persistent Logic
11.
Advantages ofMVC
The main advantage of using MVC in a Web Application is it makes complex
applications easy to manage with divisions of Model, View, and Controllers.
MVC provides a strong routing mechanism with a Front Controller pattern.
MVC works well for development with large teams with multiple web
developers and designers working simultaneously.
In MVC, separating the model from View makes the web application more
robust and easier to maintain.
In MVC, separating the Controller from the Model allows configurable mapping
of user actions on the Controller to application functions on the Model.
12.
MVC inJSP Application
we are showing how to
use MVC architecture in JSP. In this
example, we are creating an
example in which servlet as a
controller, JSP as a view
component, Java Bean class as a
model. In this example, we have
created 6 pages.
index.jsp
ControllerServlet.java
LoginBean.java
login-success.jsp
login-error.jsp
web.xml
13.
Example of MVCin JSP Application
index.jsp helps us to get the
input from the user.
ControllerServlet.java acts as a
controller, login-success.jsp
and login-error.jsp file acts as a
view component. Login-
success.jsp page will show the
“Welcome” message if the user
will successfully login whereas
login-error.jsp page will show
the error message and returns
you back to the index.jsp page.
LoginBean.java acts as a Model
Layer and a web.xml file is
used to mapping the servlet.
<%@ page language="java" contentType="text/html;
charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="ControllerServlet" method="post">
Name:<input type="text" name="name"><br>
Password:<input
type="password" name="password"><br> <input
type="submit" value="login">
</form>
</body>
</html>
Index.jsp
14.
Loginbean.java
public classLoginBean {
private String name, password;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password)
{
this.password = password;
}
public boolean validate() {
if (password.equals("admin")) {
return true;
} else {
return false;
}
}
}
OUTPUT
15.
login-succes.jsp
<%@ page language="java“
contentType="text/html;charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML
4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=ISO-8859-1">
<title></title>
</head>
<body>
<%
out.print("Welcome");
%>
</body>
</html>
OUTPUT
16.
Loginerror.jsp
<%@ pagelanguage="java“
contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<p>Sorry! username or password error</p>
<%@ include file="index.jsp"%>
</body>
</html>
OUTPUT
Setting upthe Java Development Kit
Step1: It involves downloading an implementation of the Java Software
Development Kit (SDK) and fixing the environment variable. You can download
SDK from Oracle’s Java site by visiting the below URL:
https://www.oracle.com/java/technologies/javase-downloads.html
Step2: You download your Java implementation, follow the given instructions to
put in, and configure the setup. Finally set the JAVA_HOME environment variables to
ask the directory that contains java and javac, typically java_install_dir/bin and
java_install_dir respectively.
set PATH = C:jdk1.8.0_181bin;%PATH%
set JAVA_HOME = C:jdk1.8.0_181
20.
Setting upWeb Server: Tomcat
Apache Tomcat is an open-source software implementation
of the JavaServer Pages and Servlet technologies. It should
act as a standalone server for testing JSP and Servlets. It
should be integrated with the Apache Web Server. Here are
the steps to line up Tomcat on your machine −
1. Download the latest version of Tomcat from
https://tomcat.apache.org/
2. After downloading, unpack the binary distribution into a
convenient location. For example, in C:apache-tomcat-9.0
on windows
3. After unpacking pointing to the same location create
CATALINE_HOME environment variables.
21.
Start TomcatServer
Tomcat is often started by executing the subsequent commands on the
Windows machine:
%CATALINA_HOME%binstartup.bat
or
C:apache-tomcat-9.0binstartup.bat
After a successful startup, the default web applications included with Tomcat are
available by visiting http://localhost:8080/
Tomcat is often stopped by executing the subsequent commands on the
Windows machine:
%CATALINA_HOME%binshutdown
or
C:apache-tomcat-9.0binshutdown
22.
Setting upCLASSPATH
Since servlets are not a part of the Java Platform, you need to identify the
servlet classes to the compiler.
If you’re running Windows, you would like to place the subsequent lines in your
C:autoexec.bat file.
set CATALINA = C:apache-tomcat-9.0
set CLASSPATH = %CATALINA%commonlibjsp-api.jar;
%CLASSPATH%