Web Container Components Servlet - JSP - Tag Libraries
Web Container Components Servlet - JSP - Tag Libraries
Web Container Components Servlet - JSP - Tag Libraries
com
JSP
Allows some separation of Java and HTML code
Using JavaBeans as backend helpers
JSP scriplets mix a lot of Java and HTML code
Tag libraries
Define custom tags to remove Java code from JSP
pages
ITS, Inc. [email protected]
Java Servlet and JSPs
Supported by all current application servers,
Java Standard Tag Library, Struts, BEA WebLogic , and more tag
library frameworks are available.
ITS, Inc. [email protected]
Java Servlets serve HTTP
The Hypertext Transfer Protocol (HTTP), like HTML was developed by Tim
Berners-Lee and CERN in1989 with the main purpose to transfer arbitrary
data, and therefore is not restricted to just text, as its name might suggest.
Although the HTTP 1.1 spec. contains 7 types of requests, many simple
HTTP servers only know the commands GET, POST, and HEAD
HTML forms have (hidden or visible) input fields and the ACTION field
Default request method is GET
For example, when a browser sends a request to the web server, the server
may forward the request to a servlet that can process the request and
construct a response, usually an HTML page, that is returned to the browser.
//Get HTTP headers, cookies, session and form data from the request
//Apply business logic, e.g., user authorization
//Fill outgoing "response" to specify the HTTP response
//including status code, headers, cookies, session data,
// send output HTML page back to a client
}
}
ITS, Inc. [email protected]
Servlet Code Example
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
response.getWriter().println(html);
}
} ITS, Inc. [email protected]
Servlet Best Practices
New services can be added run time as new JSPs/ASPs or Java/.NET classes
//serviceName and serviceDetails are to be populated
// by servlet doPost() , doGet() or service() methods
XML based Service API allows us to describe any existing and future service
<ServiceRequest service=Mail action=get>
<Param><paramName1=></Param>
</ServiceRequest>
We can find both Dispatcher and Factory patterns in this example. This approach makes it
possible to create a unified API for client server communications. Any service (including
new, unknown design time services) can be requested by a client without code change.
ITS, Inc. [email protected]
Servlets Initialization and web.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application
2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app id="WebApp">
<display-name>JavaSchoolApp</display-name>
<context-param>
<param-name>sqlLocation</param-name>
<param-value>sql</param-value>
<description>Location of SQL statements relative to WEB-INF</description>
</context-param>
<!-- Initialization code is in the ITSServletContextListener.java -- >
<listener>
<listener-class>com.its.util.ITSServletContextListener</listener-class>
</listener>
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>WEB-INF/struts-config.xml</param-value>
</init-param>
</servlet>
ITS, Inc. [email protected]
Servlets Summary
Servlet-Centric Architecture
Web Tier
Submit Servlets
Client(s) Form Other
execute Enterprise
Request business
Web Applications
Browser logic,
Display provide data
Http Page access, and
Post/Get
Response Create
HTML
pages Database
2. Servlets usually:
1.
2.
3.
2. Servlets usually:
1) Accept input data
2) Run or delegate business logics
3) Dynamically generate HTML response page
Standard actions
In JSP, actions are elements that can create and access
programming language objects and affect output
JSP spec 1.1 defines standard actions that can be used
to interact with any Bean
useBean, setProperty, getProperty, include, forward,
param, plugin
Struts
WebLogic
Displaytag
Custom Tags .
Custom Tags .
Practice ...
Email this file to me and I will Copy the file yourName.war to the
deployment directory, for example, Tomcat/Webapps
//Get HTTP headers, cookies, session and form data from the request
//Apply business logic, e.g., user authorization
//Fill outgoing "response" to specify the HTTP response
//including status code, headers, cookies, session data,
// send output HTML page back to a client
}
}
ITS, Inc. [email protected]
Prepare Eclipse to Servlets
1.Provide the Servlet Library
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>train.ServletExample</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>30</session-timeout>
</session-config>
</web-app>
ITS, Inc. [email protected]
Servlets Summary
Servlet-Centric Architecture
Web Tier
Submit Servlets
Client(s) Form Other
execute Enterprise
Request business
Web Applications
Browser logic,
Display provide data
Http Page access, and
Post/Get
Response Create
HTML
pages Database
2. Servlets usually:
1.
2.
3.
2. Servlets usually:
1) Accept input data
2) Run or delegate business logics
3) Dynamically generate HTML response page