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.
- A (.jsp) file is requested by a user..
- The JSP Engine on the server converts it into the Servlet.
- The servlet is complied and executed.
- The Response (HTML) is send back to the browser.
- SCRIPTLETS TAG.
- EXPRESSION TAG.
- DECLERATION TAG.
<!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:
<%@ 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>
<%@ 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>
<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>
- OUT.
- REQUEST.
- RESPONSE.
- CONFIG.
- SESSION.
- APPLICATION.
- PAGE.
- PAGE CONTEXT.
- EXCEPTION.
<%@ 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>
This is the HttpServletResponse object associated with the response
to the client.
Example:
<%@ 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>
<%@ 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
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.
<%@ 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
Post a Comment