Separating Programming and Presentation: JSP Technology: CSI 3140 WWW Structures, Techniques and Standards
Separating Programming and Presentation: JSP Technology: CSI 3140 WWW Structures, Techniques and Standards
Separating Programming and Presentation: JSP Technology: CSI 3140 WWW Structures, Techniques and Standards
JSP pages are easier to maintain then a Servlet. JSP pages are opposite of Servlets.
Servlet adds HTML code inside Java code while JSP adds Java code inside HTML.
Everything a Servlet can do, a JSP page can also do it.
JSP enables us to write HTML pages containing tags that run powerful Java
programs.
JSP separates presentation and business logic as Web designer can design and
update JSP pages without learning the Java language and Java Developer can also write
code without concerning the web design.
In the third step, the servlet class bytecode is loaded using classloader. The
Container then creates an instance of that servlet class.
The initialized servlet can now service request. For each request the Web
Container call the _jspService()method.
When the Container removes the servlet instance from service, it calls
the jspDestroy() method to perform any required clean up.
JSP Scripting element are written inside <% %> tags. These code inside <%
%> tags are processed by the JSP engine during translation of the JSP page. Any
other text in the JSP page is considered as HTML code or plain text.
Scriptlet Tag allow you write java code inside JSP page.
<% java code %>
In this example we are prompting user to enter name and roll number. On the other JSP page
we are fetching the entered details using param variable of EL.
index.jsp
<html> <body> <form action="display.jsp">
Student Name: <input type="text" name="stuname" />
<br> Student RollNum:<input type="text" name="rollno" /><br>
<input type="submit" value="Submit Details!!"/> </form> </body> </html>
display.jsp
<html>
<body>
Student name is ${ param.stuname } <br>
Student Roll No is ${ param.rollno } </body>
</html>
: JSTL Core provide several core tags such as if, forEach, import, out etc to support some basic scripting task. Url
to include JSTL Core Tag inside JSP page is:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
JSTL if tag: The if tag is a conditional tag used to evaluate conditional expression. Example
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html> <head> <title>Tag Example</title> </head>
<body>
<c:if test="${param.name == 'studytonight' }">
<p>Welcome to ${param.name} </p>
</c:if>
</body>
</html>
<body>
<c:forEach var="message" items="${errorMsgs}" >
<li>${message}</li>
</c:forEach >
</body>
JSTL set tag: The JSTL set tag is used to store a variable in specified scope
or update the property of JavaBean instance
The JSTL SQL tag library provides tags for interacting with relational databases
(RDBMSs) such as Oracle, mySQL, or Microsoft SQL Server.
Following is the syntax to include JSTL SQL library in your JSP:
<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>
Example
To access the java bean class, we should use getter and setter methods.
package mypack;
public class Test{
public static void main(String args[]){
Employee e=new Employee();//object is created
e.setName(“xxxx");//setting value to the object
System.out.println(e.getName());
}}
Model Components
Controller View
HTTP HTTP
request response
Model Components
(beans, DBMS)
Controller View
(Java servlet) (JSP document)
HTTP HTTP
request response