Java server page (JSP) introduction

JSP(Java server pages) is API of J2EE which acccept request and generate dynamic Response.

JSP technology is used to create dynamic web application.

With respect to functionality JSP and servlet are one or same because JSP get
Transleted into servlet at Run time.
JSP Provide implicit objects, predefined tags, expression language which make your development easier.

Advantage of JSP.


  •  Easier to maintain
  •  If you change the JSP code then No need to re-compile and Re-deploy it again.
  •  Less code than Servlet
  •  JSP seperate business logic and presentation logic.

1. web designer can write presentation logic.
2. Web developer can write business logic to generate dynamic response.

In JSP you can write HTML and Java code but in case of servlet you can write
only java code.

When ever first request comes to JSP.


1. Container translate JSP into servlet.
2. Compile the servlet and generate class file(.class)
This class file used to generate dynamic response.

Hence the time taken to generate the response for first request is more compared
to subsequent request

 

Difference between JSP and Servlet


difference-between-servlet-and-jsp

Simple JSP program


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
 
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
 
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
 
<h1>This is JSP page</h1>
 
<p> Javaforlearn is a platform where newbie programmer find a way to learn different Technology 
    like java, J2EE, SQL, ODI , OBIEE and data warehouse concept.</p>
 
 
<%
 
	//here you can write java code.
 
	//Printing simple message in console
	System.out.println("Hi Application is running ");
 
 
	//Printing simple message in Browser
	out.println("Hi This is Bhavesh Lakhani");
 
 
%>
 
</body>
</html>

Accessing JSP Application


simple-jsp

Console output:


console-output

Creating First JSP Program using Eclipse

Steps to Create JSP project in eclipse

1. Create Dynamic web Project in eclipse


1-dynamic-web-project

2. Eclipse Automatically Create Project Structure


2-jsp-project
3. Right click on Web content and select new and Create JSP file


3-create-new-jsp

index.jsp


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
 
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
 
<h1>This is JSP page</h1>
 
<p> Javaforlearn is a platform where newbie programmer find a way to learn different Technology 
    like java, J2EE, SQL, ODI , OBIEE and data warehouse concept.</p>
 
 
<%
 
//here you can write java code.
 
//Printing simple message in console
System.out.println("Hi Application is running ");
 
 
//Printing simple message in Browser
out.println("Hi This is Bhavesh Lakhani");
 
 
%>
 
</body>
</html>

4. You can see index.jsp file under web content folder


4-jsp-file
5. Right click on index.jsp file and select Run as and select Run on server


5-running-jsp
6. You can see output in Browser


6-simple-jsp
7. console output.


7-console-output