Java Servlets & �Java Server Pages
What is a Servlet?
Common Uses
Architectural Roles
Architectural Roles
What is required?
Web Container/Servlet engine
J2EE Container
Browser
Servlets
Web Server
Deployment Descriptor
Java Classes
web.xml
Deployment Descriptor is used to configure web applications that are hosted in the App Server.
Web Application Directory Structure
Common Containers Are Provided by the App Server
Overview of the Servlet API
Servlet Interface
HttpServlet
Servlet Life Cycle
ResourceExampleServlet
Configuration - web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN" "http://java.sun.com/j2ee/dtds/web-app_2.2.dtd">
<web-app>
<servlet>
<servlet-name>resourceExample</servlet-name>
<servlet-class>ResourceExampleServlet</servlet-class>
<init-param>
<param-name>dbUser</param-name>
<param-value>testUser</param-value>
</init-param>
[ Repeat above for each parameter – init-param ]
</servlet>
</web-app>
ResourceExampleServlet
What about the Code? – init()
public void init() throws ServletException { dbUser = getServletConfig().getInitParameter( "dbUser" );
driver = getServletConfig().getInitParameter( "driver" );
dbUrl = getServletConfig().getInitParameter( "dbUrl" );
exampleMsg = getServletConfig().getInitParameter( "exampleMsg" );
}
What about the code? – doGet()
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head><title>Resource Example Servlet</title></head>");
out.println("<body>");
out.println("<H3>Resource Example Servlet</H3>");
out.println("<p>I could really do some damage if I only knew how to use those" +
" JDBC libraries!<br>I mean I have:<br>" );
out.println("<ul>");
out.println("<li>User: " + dbUser );
out.println("<li>URL: " + dbUrl );
out.println("<li>Driver: " + driver );
out.println("</ul>");
out.println("<p>" + exampleMsg + "</p>");
out.println("</body>");
out.println("</html>");
What about the code? – destroy()
public void destroy() {
dbUser = null;
driver = null;
dbUrl = null;
}
HttpServletRequest &�HttpServletResponse
Servlet Sessions
Client
Server
Response with a Token
Request with a Token
Servlet Sessions
HttpSession
HttpSession session = request.getSession();
Session Example Servlet
Servlet Context
Proposed Architecture of Web Applications
Presentation Layer (JSP, HTML, WML)
Logic Layer
(Servlets, JavaBeans, EJBS, etc)
Data Store Layer
(MySQL, SQL Server, File System)
Highly Coupled Servlets
if( developer == javaProgrammer)
System.out.println(“Stick to Java code!”);
else if( developer == webDesigner )
System.out.println(“Stick to html code!”);
Java Server Pages
How do JSP’s Work
Browser
Servlet/JSP
Server
Generated
Servlet
index.jsp
index.java
compiles
checks
converts
Forwards to
Basic JSP Syntax
Scripting Elements
Declaration Tag
Scriptlet Tag
Expression Tag
JSP Directives
Page Directives
<%@ page language=“Java”
[ extends=“className” ]
[ import=“importList” ]
[ session= “true|false” ]
[ buffer=“none|sizekb” ]
[ autoFlush=“true|false” ]
[ isThreadSafe=“true|false” ]
… %>
Page Directive
Include Directive
JSP Actions
Standard Actions
Standard Actions
Standard Actions - useBean
Standard Actions - useBean
Standard Actions - setProperty
Standard Actions - setProperty
Standard Actions - getProperty
Bean Example
<html>
<title>Random JSP Test</title>
<body bgcolor=“white”>
<jsp:useBean id=“rnd” scope=“page” class=“random.NumberGenerator”/>
<ul>
<li>Next number is: <jsp:getProperty name=“rnd” property=“nextInt”/>
</ul>
</body>
</html>
Bean Example
package random;
import java.util.*;
public class NumberGenerator {
Random rnd = new Random();
public int getNextInt() {
return rnd.nextInt();
}
}
Standard Actions – jsp:include
Standard Actions – jsp:forward
Standard Actions - <jsp:plugin>
Standard Actions - jsp:param
Access Models
Model 1
browser
JSP
BEAN
Database
Model 1
Model 2
browser
servlet
JSP
Beans
Database
It’s a rap!
Resources
Resources