JSP TUTORIAL

INTRODUCTION OF JSP:

1.What is jsp.

2.How jsp works in Servlet.

3.JSP VS Servlet.

4.JSP life Cycle.

What is jsp:

  • JSP (Java Server Page) is a server side technology used to create dynamic , paltform-indepentent web application using java.
  • When jsp page requested by User, it is complied into a servlet by the server and the server handle the request.
How jsp Works (behind the scenes):
  1. A (.jsp) file is requested by a user..
  2. The JSP Engine on the server converts it into the Servlet. 
  3. The servlet is complied and executed.
  4. The Response (HTML) is send back to the browser.
JSP VS Servlet:


JSP life Cycle:

LINK :https://data-flair.training/blogs/jsp-life-cycle/



DIFFERENCE BETWEEN A WEBSERVER ,WEB CONTAINER AND AN APPLICATION SERVER:

WEB SERVER: 
It is receives  HTTP request , intercepts it. It also processes the HTTP response to the web browser of the client. EX: Apache Web server.

WEB CONTAINER:
A web container is J2EE compliant implementation.Its mains the job is run the runtime environment to JSP and Servlet . EX: Tomcat.

Application Server:
It is regarded as the complete server as it provides the facility both web server as well as web container.
EX:Oracle Appliction Server.



JSP SCRPITING ELEMENTS:

The scripting element provides the ability of insert java code inside the jsp.There are three types of scripting elements.
  1. SCRIPTLETS TAG.
  2. EXPRESSION TAG.
  3. DECLERATION TAG.
1.SCRIPTLETS TAG:

 A Scriptlet tag is used execute java source code in jsp.

Syntax:
 
<% java source code %>

Example:

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>SCRIPTLET</title>

</head>

<body>

<h6>--DataFlair</h6>

<% int a=1;

int b=2;

int c=3;

out.println("The number is" + a*b*c);

%>

</body>

</html>



2.EXPRESSION TAG:

Expression tag is for printing a value to the output

syntax:
<%= statement %>

Example:

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>EXPRESSION</title>

</head>

<body>

<h6>--DataFlair</h6>

<% String s1="captian america"; %>

<% out.print(s1); %>

<% String s2=s1.toUpperCase(); %>

<% out.print(" "+s2); %>


</body>

</html>




3.DECLERATION TAG:


The jsp decleration tag is used to the declare field and methods.

syntax:
<%! field or method declaration %>

Example:

<%@ page language="java" contentType="text/html; charset=UTF-8"

pageEncoding="UTF-8"%>

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>Declaration</title>

</head>

<body>


<%! int count = 10; //instance variable

//method to increment and return count

public int getNextCount() {

return ++count;

}

%>

<h2>Visitor Number: <%= getNextCount() %></h2>

</body>

</html>




HTTP REQUEST METHODS:



1.GET.
Get as a retrieves data from the server (e.g. loading a webpage).


EXAMPLE:

get.html

<html>

<body>


<form action = "get.jsp" method = "GET">

First Name: <input type = "text" name = "first_name">

<br />

Last Name: <input type = "text" name = "last_name" />

<input type = "submit" value = "Submit" />

</form>

</body>

</html>





get.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<html>

<head>

<title>GET using form</title>

</head>

<body>

<h2>GET Method to Read Form Data Using form</h2>


<ul>

<li><p>

<b>First Name:</b> <%= request.getParameter("first_name")%>

</p></li>

<li><p>

<b>Last Name:</b> <%= request.getParameter("last_name")%>

</p></li>

</ul>

</body>

</html>






2.POST.
Post as a send data to the server to create something.(e.g. submitting a form).


post.html

<html>

<body>

<form action = "post.jsp" method = "POST">

First Name: <input type = "text" name = "first_name">

<br />

Last Name: <input type = "text" name = "last_name" />

<input type = "submit" value = "Submit" />

</form>

</body>

</html>



post.jsp


<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<html>

<head>

<title>Using POST Method</title>

</head>

<body>

<h1>POST Method to Read Form Data</h1>

<b>First Name:</b><%= request.getParameter("first_name")%>

<b>Last Name:</b><%= request.getParameter("last_name")%>

</body>

</html>




3.PUT.
Put as also known as Update existing data fully on the server. (e.g. Edit user profile).

4.DELETE.
Removes data from the server (e.g. Delete an account).

5.HEAD.
Head is like a Get,but only retrievers the header(no response body).

6.PATCH.
Patch means partially or particular to update data on the server (e.g. update only email)

7.OPTIONS.
Asks the server which Http methods are supported for a resources.

8.CONNECTION.
Establishes a tunnels to the server (used for https).

9.TRACE.
Echoes the recieved request for testing and debugging.


JSP IMPLICIT OBJECT:
  
REFERENCE LINK:https://data-flair.training/blogs/jsp-implicit-objects/
  1. OUT.
  2. REQUEST.
  3. RESPONSE.
  4. CONFIG.
  5. SESSION.
  6. APPLICATION.
  7. PAGE.
  8. PAGE CONTEXT.
  9. EXCEPTION.
1.OUT:
                This is the PrintWriter object used to send output to the client.
           Syntax : out.methodname();
EXAMPLE:

out.jsp

<!DOCTYPE html>

<html>

<head>

<title>Date and time</title>

</head>

<body>

<% out.println("Today's date-time: "+java.util.Calendar.getInstance().getTime()); %>

</body>

</html>





2.REQUEST:
                    This is the HttpServletRequest object associated with the request.
                     Syntax:request.methodname("value");

Example:

request.html

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>my login</title>

</head>

<body>

<form action="request.jsp">

<a>UserName:</a><input type="text" name="username">

<br/>

<a>Password</a><input type="text" name="password">

<input type="submit" value="go"><br/>

</form>

</body>

</html>





request.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"

pageEncoding="UTF-8"%>

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>Login</title>

</head>

<body>

<%

String name=request.getParameter("username");

String pass=request.getParameter("password");

out.println("welcome user , you entered your name as" +"\n"+name);

%>

</body>

</html>




3.RESPONSE:
        This is the HttpServletResponse object associated with the response 
to the client.
                 Syntax:response.methodname("value");

 Example:

response.jsp

<%@ page language="java" %>

<%

// Set content type

response.setContentType("text/html");


// Optional: redirect to another page after 3 seconds

response.setHeader("Refresh", "3; URL=https://www.google.com");

%>

<html>

<head><title>Response </title></head>

<body>

<h2>This page will redirect in 3 seconds using response.setHeader()</h2>

</body>

</html>





4.CONFIG :

                        This is the ServletConfig object associated with the page.

                  Syntax : config.methodname();

EXAMPLE:

web.xml:

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="4.0">

<servlet>

<servlet-name>MANI</servlet-name>

<jsp-file>/one.jsp</jsp-file>

<init-param>

<param-name>companyName</param-name>

<param-value> Gamers.</param-value>

</init-param>

</servlet>


<servlet-mapping>

<servlet-name>MANI</servlet-name>

<url-pattern>/one.jsp</url-pattern>

</servlet-mapping>

</web-app>


one.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"

pageEncoding="UTF-8"%>


<html>

<head><title>Config Example</title></head>

<body>

<h2>Using JSP implicit object: Config </h2>

<%

String company = config.getInitParameter("companyName");

out.println("Company Name from config: " + company);

%>

</body>

</html>



getInitParameter:

In JSP or servlets, getInitParameter(String name) is a method used to retrieve

initialization parameters defined in the web.xml file.


These parameters are typically used to configure behavior without modifying code.


5.SESSION :

                       This is the HttpSession object associated with the request.

                Syntax : session.getAttribute();

EXAMPLE:

session.html

<html>

<head><title> Config</title></head>

<body>

<form action="first.jsp">

<input type="text" name="username">

<input type="submit" value="done"><br/>

</form>

</body>

</html>



first.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"

pageEncoding="UTF-8"%>

<!DOCTYPE html>

<html>

<head><title>Session</title></head>

<body>

<%

String myname=request.getParameter("username");

out.print("Welcome "+myname);

session.setAttribute("user",myname); %>

<a href="second.jsp">second jsp page</a>

</body>

</html>




second.jsp


<%@ page language="java" contentType="text/html; charset=UTF-8"

pageEncoding="UTF-8"%>

<html>

<body>

<%

String name = (String) session.getAttribute("user");

out.print("Hello " + name);

%>


</body>

</html>




6.APPLICATION:   
       
                 This is the ServletContext object associated with the application context.

7.PAGECONTEXT: 

              This encapsulates use of server-specefic features like higher performance JspWriters.

8.PAGE: 

          This is simply a synonym for this, and is used to call the methods defined by the translated servlet class.

9.Exception: 

                 The Exception object allows the exception data to be accessed by designated JSP.

EXPRESSION LANGUAGE (EL):

Expression Language in JSP  is a simple language that allows easy access to java components (like javabeans, request parameters, session attributes,etc.) within a jsp page, without the needed for scriptlet tags  (<% %> ).

  Syntax: ${expression}

Uses of EL:

  • Accessing attribute stored in scopes: pagescope, requestscope, sessionscope, applicationscope.
  • Accessing request parameters:${param.name}.
  • Working with javabeans properties.
 EXAMPLE:

<%@ page language="java" contentType="text/html; charset=UTF-8"

pageEncoding="UTF-8"%>

<%

// Set an attribute in request scope

request.setAttribute("username", "Mani");

int num1 = 10;

int num2 = 5;

request.setAttribute("num1", num1);

request.setAttribute("num2", num2);

%>

<html>

<head><title>EL Example</title></head>

<body>

<h2>Expression Language Example</h2>

<!-- Accessing attribute -->

<p>Welcome, ${username}!</p>


<!-- Arithmetic operation -->

<p>Sum: ${num1 + num2}</p>

<p>Product: ${num1 * num2}</p>


<!-- Logical operation -->

<p>Is num1 greater than num2? ${num1 > num2}</p>


<!-- Accessing request parameter -->

<p>City from request: ${param.city}</p>

<!-- Link to test passing city -->

<p><a href="exla.jsp?city=chennai">Click here to pass city=Chennai</a></p>

</body>

</html>             







Comments

Popular posts from this blog

JAVA TUTORIAL

ORACLE DBA