Java Server Pages
Java Server Pages
• Expression tag evaluates the expression placed in it, converts the result into
String and send the result back to the client through response object.
• Basically it writes the result to the client(browser).
• So you need not write out.print() to write data.
• It is mainly used to print the values of variable or method.
• <%= used to specify the Expression tag
Syntax
<%= statement %>
Ex:
<html> <body>
<%= "welcome to jsp" %>
</body> </html>
Example of JSP expression tag that prints current time & date
<HTML>
<BODY>
Hello ! CSE Students
<br>
Current time is: <%=new java.util.Date()%>
</BODY>
</HTML>
Output:
Hello ! CSE Students
Current time is: Sun Oct 29 10:50:04 IST 2017
Example of JSP expression tag that prints the user name
<html>
<html>
<body> <body>
<form action="welcome.jsp"> <
%= "Welcome "+request.getParameter("uname") %>
<input type="text" name="uname"> </body>
<input type="submit" value="go"><br/ </html>
></form>
</body>
</html>
<html>
<body>
<%!
int cube(int n){
return n*n*n;
}
%>
<%= "Cube of 3 is:" +cube(3) %>
</body>
</html>
Jsp Implicit Objects
Jsp objects are created by JSP Engine during translation phase (while
translating JSP to Servlet).
There are total 9 implicit objects available in JSP.
1. out
2. request
3. response
4. config
5. application
6. session
7. pageContext
8. page
9. exception
Jsp Implicit Objects
Out:
• Out is one of the implicit objects to write the data to the buffer and send
output to the client in response
• Out is object of jspWriter class
Request:
• The request object is an instance of java.servlet.http.HttpServletRequest.
• It will be created by container for every request.
• It will be used to request the information like parameter, header
information , server name, etc.
• It uses getParameter() to access the request parameter.
Jsp Implicit Objects
Response:
• "Response" is an instance of class which implements HttpServletResponse
interface
• "Response object" will be created by the container for each request.
• It represents the response that can be given to the client
• The response implicit object is used to content type, add cookie and redirect
to response page
• void sendRedirect(String address) – It redirects the control to a new JSP page
• response.sendRedirect("http://beginnersbook.com");
Jsp Implicit Objects
Config:
• Config is of the type java.servlet.servletConfig
• It is created by the container for each jsp page
• It is used to get the initialization parameter in web.xml
Application :
• Application object is an instance of javax.servlet.ServletContext
• This is used for getting application-wide initialization parameters and to
maintain useful data across whole JSP application.
• Application object is created by container one per application, when the
application gets deployed.
Jsp Implicit Objects
Session
• The session is holding "httpsession" object
• Session object is used to get, set and remove attributes to session scope and
also used to get session information
pageContext:
• It is an instance of javax.servlet.jsp.PageContext
• It is used for accessing page, request, application and session attributes.
Exception:
• Exception implicit object is used in exception handling for displaying the error
messages
Index.html check.jsp
<html>
<html>
<body>
<head> <%
<title>Login Page</title> String uid=request.getParameter("id");
</head> String password=request.getParameter("pass");
<body> session.setAttribute("session-uid", uid);
<form action="check.jsp"> if(uid.equals("devansh") && password.equals("Harshi"))
{
UserId: <input type="text" name="id" /> <br>
response.sendRedirect("success.jsp");
Password: <input type="text"
}
name="pass" /> <br>
else
<input type="submit" value="Sign In!!"/>
{
</form> response.sendRedirect("failed.jsp");
</body> }
</html> %>
</body> </html>
success.jsp failed.jsp
<%@page contentType="text/html"
pageEncoding="UTF-8"%> <%@page contentType="text/html"
pageEncoding="UTF-8"%>
<html>
<html>
<body>
<body>
<h1 style=color:blue>Hey Devansh U
have Successfully login on </h1> <h1>U have Failed!</h1>
1) Page Directive
2) Include Directive
3) TagLib Directive
Page Directive
• The Page directive defines a number of page dependent properties
which communicates with the Web Container at the time of
translation.
Basic syntax of using the page directive is
<%@ page attribute="value" %>
Some of the attributes are
• import attribute
• language attribute
• contentType attribute
Page Directive
import:
• This attribute is used to import packages.
Syntax : <%@page import="value"%>
language :
• language attribute defines scripting language to be used in the page.
Syntax : <%@ page language="value"%>
contentType:
• This attribute is used to set the content type of a JSP page.
Default value: text/html
2) Include Directive
• Include directive is used to copy the content of one JSP page to
another.
• It’s like including the code of one file into another.
<%@include file ="value"%>
• here value is the JSP file name which needs to be included.
• If the file is in the same directory then just specify the file name
otherwise complete URL(or path) needs to be mentioned in the value
field.
• Note: It can be used anywhere in the page.
Example:
• <%@include file="myJSP.jsp"%>
include.jsp declaration.jsp
emp.html <td>phno:</td>
<html> <td><input type="text"name="ephno"/></td>
<body> </tr>
<h3><center>Employee Registration <tr>
Form</center></h3> <td>Address:</td>
<form method="POST“ <td><input type="text"name="eaddress"/></td>
action="http://localhost:8080/employee/employee </tr>
"> <tr>
<table align="center"> <td>email:</td>
<td><input type="text"name="email"/></td>
<tr> </tr>
<td>Eid:</td> <tr>
<td>Salary:</td>
<td><input type="text"name="eid"/></td>
<td><input type="text"name="esalary"/></td>
</tr> </tr>
<tr> <tr>
<td></td><td><input type="submit" value="submit"/></td>
<td>Ename:</td>
<td><input type="reset" value="Reset"/></td></tr>
<td><input type="text"name="ename"/></td> </table></form></body></html>
</tr> <tr>
emp.jsp
import java.io.*; PreparedStatement ps=con.prepareStatement("insert into employee
import java.util.*; values(?,?,?,?,?,?)");
import java.sql.*; ps.setString(1,eid);
import javax.servlet.*; ps.setString(2,ename);
import javax.servlet.http.*; ps.setString(3,ephno);
public class employee extends HttpServlet ps.setString(4,eaddress);
{ ps.setString(5,email);
public void doPost(HttpServletRequest ps.setString(6,esalary);
request,HttpServletResponse response)throws int i=ps.executeUpdate();
IOException,ServletException if(i>0)
{ {
response.setContentType("text/html");
PrintWriter out=response.getWriter(); response.sendRedirect("employeesalary.html");
String eid=request.getParameter("eid"); }
String else
ename=request.getParameter("ename"); {
String out.println("Not
ephno=request.getParameter("ephno"); inserted");
String eaddress=request.getParameter("eaddress");
String email=request.getParameter("email"); }
String }
esalary=request.getParameter("esalary"); catch(Exception e)
try {
{ } }}
JSP Application Design with MVC.
• Reenskaug formulated the model–view–controller (MVC) pattern for
graphical user interface (GUI) software design in 1979 while visiting
the Xerox Palo Alto Research Center (PARC)
• This model has since been used for GUI applications developed in all
popular programming languages.
• MVC is an architecture that separates business logic, presentation and
data
• In MVC, M stands for Model, V stands for View,C stands for controller.
• MVC is a systematic way to use the application where the flow starts
from the view layer, where the request is raised and processed in
controller layer and sent to model layer to insert data and get back the
success or failure message.
Model Layer
• This is the data layer which consists of the business logic of the
system.
• It consists of all the data of the application.
• It consists of JavaBeans, EJB, etc. into it.
• It consists of classes which have the connection to the database.
• The controller connects with model and fetches the data and
sends to the view layer.
• The model connects with the database as well and stores the
data into a database which is connected to it.
View Layer
• This is a presentation layer.
• It consists of HTML, JSP, etc. into it.
• It normally presents the UI of the application.
• It is used to display the data which is fetched from the controller
which in turn fetching data from model layer classes.
• This view layer shows the data on UI of the application.
Controller Layer: