This document discusses the Model-View-Controller (MVC) architecture and how to implement it using servlets and JSP. It begins with an overview of MVC and the benefits of combining servlets and JSP. It then covers implementing MVC with RequestDispatcher to forward requests from servlets to JSP pages, and how to handle relative URLs and share data between the servlet and JSP using request, session, and application scopes. It provides examples of using beans to represent data and accessing bean properties in JSP.
In this Java JSP Training session, you will learn JSP. Topics covered in this session are:
• JSP vs Servlet
• LifeCycle of Servlet
• JSP Elements
• JSP Page directive
• Directives vs Action tags
For more information, visit this link:
https://www.mindsmapped.com/courses/software-development/jsp-and-servlets-designing-web-applications-with-java/
In this Java JSP Training session, you will learn JSP. Topics covered in this session are:
• JSP vs Servlet
• LifeCycle of Servlet
• JSP Elements
• JSP Page directive
• Directives vs Action tags
For more information, visit this link:
https://www.mindsmapped.com/courses/software-development/jsp-and-servlets-designing-web-applications-with-java/
basic introduction,architecture,life-cycle, difference b/w jsp and servlets and advantages and disadvantages of jsp.
For more information:-
ishan khan
email:- ishan0214@gmail.com
JSP
The Anatomy of a JSP Page, JSP Processing, Declarations, Directives, Expressions, Code Snippets, implicit objects, Using Beans in JSP Pages, Using Cookies and session for session tracking, connecting to database in JSP.
jsp implicit objects (predefined java objects)
jsp implicit objects are java objects that the jsp container makes available to developers in each page so that developer can call them directly without being explicitly declared......
there are 9 pre defined objects in jsp... which are being discussed in this ppt with diagram representation and fewer points..... not full information in slides ... just prepared for presentation purpose.... so u have to do the research work from internet and you tube.....
this ppt is self prepared and taken reference form you tube
In this Java Spring Training session, you will learn Spring MVC. Topics covered in this session are:
For more information, visit this link:
• Spring framework
• Inversion of Control
• Dependency Injection – Two types
• Defining beans using XML
• Inheriting beans
• Auto-wiring
• Annotations based configuration
• Java based configuration
• Spring AOP
https://www.mindsmapped.com/courses/software-development/spring-fundamentals-learn-spring-framework-and-spring-boot/
basic introduction,architecture,life-cycle, difference b/w jsp and servlets and advantages and disadvantages of jsp.
For more information:-
ishan khan
email:- ishan0214@gmail.com
JSP
The Anatomy of a JSP Page, JSP Processing, Declarations, Directives, Expressions, Code Snippets, implicit objects, Using Beans in JSP Pages, Using Cookies and session for session tracking, connecting to database in JSP.
jsp implicit objects (predefined java objects)
jsp implicit objects are java objects that the jsp container makes available to developers in each page so that developer can call them directly without being explicitly declared......
there are 9 pre defined objects in jsp... which are being discussed in this ppt with diagram representation and fewer points..... not full information in slides ... just prepared for presentation purpose.... so u have to do the research work from internet and you tube.....
this ppt is self prepared and taken reference form you tube
In this Java Spring Training session, you will learn Spring MVC. Topics covered in this session are:
For more information, visit this link:
• Spring framework
• Inversion of Control
• Dependency Injection – Two types
• Defining beans using XML
• Inheriting beans
• Auto-wiring
• Annotations based configuration
• Java based configuration
• Spring AOP
https://www.mindsmapped.com/courses/software-development/spring-fundamentals-learn-spring-framework-and-spring-boot/
The Change! Tool is a comprehensive evaluation and project management
tool with a history of rigorous development and testing. The Tool enables
practitioners to evaluate the impact of community engagement projects
that bring about behaviour change towards sustainable lifestyles.
Mumbai Academics is Mumbai’s first dedicated Professional Training Center for Training with Spoke and hub model with Multiple verticles . The strong foundation of Mumbai Academics is laid by highly skilled and trained Professionals, carrying mission to provide industry level input to the freshers and highly skilled and trained Software Professionals/other professional to IT companies.
JavaScript, often abbreviated as JS, is a programming language and core techn...MathivananP4
JavaScript, often abbreviated as JS, is a programming language and core technology of the World Wide Web, alongside HTML and CSS. As of 2024, 98.9% of websites use JavaScript on the client side for webpage behavior, often incorporating third-party libraries.
This slide is about basics of java servlet and java server page.
A basic example of JSP using multiple directives.
Further information of setting up and using of Apache Tomcat server.
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfPaige Cruz
Monitoring and observability aren’t traditionally found in software curriculums and many of us cobble this knowledge together from whatever vendor or ecosystem we were first introduced to and whatever is a part of your current company’s observability stack.
While the dev and ops silo continues to crumble….many organizations still relegate monitoring & observability as the purview of ops, infra and SRE teams. This is a mistake - achieving a highly observable system requires collaboration up and down the stack.
I, a former op, would like to extend an invitation to all application developers to join the observability party will share these foundational concepts to build on:
GridMate - End to end testing is a critical piece to ensure quality and avoid...ThomasParaiso2
End to end testing is a critical piece to ensure quality and avoid regressions. In this session, we share our journey building an E2E testing pipeline for GridMate components (LWC and Aura) using Cypress, JSForce, FakerJS…
Dr. Sean Tan, Head of Data Science, Changi Airport Group
Discover how Changi Airport Group (CAG) leverages graph technologies and generative AI to revolutionize their search capabilities. This session delves into the unique search needs of CAG’s diverse passengers and customers, showcasing how graph data structures enhance the accuracy and relevance of AI-generated search results, mitigating the risk of “hallucinations” and improving the overall customer journey.
Sudheer Mechineni, Head of Application Frameworks, Standard Chartered Bank
Discover how Standard Chartered Bank harnessed the power of Neo4j to transform complex data access challenges into a dynamic, scalable graph database solution. This keynote will cover their journey from initial adoption to deploying a fully automated, enterprise-grade causal cluster, highlighting key strategies for modelling organisational changes and ensuring robust disaster recovery. Learn how these innovations have not only enhanced Standard Chartered Bank’s data infrastructure but also positioned them as pioneers in the banking sector’s adoption of graph technology.
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...SOFTTECHHUB
The choice of an operating system plays a pivotal role in shaping our computing experience. For decades, Microsoft's Windows has dominated the market, offering a familiar and widely adopted platform for personal and professional use. However, as technological advancements continue to push the boundaries of innovation, alternative operating systems have emerged, challenging the status quo and offering users a fresh perspective on computing.
One such alternative that has garnered significant attention and acclaim is Nitrux Linux 3.5.0, a sleek, powerful, and user-friendly Linux distribution that promises to redefine the way we interact with our devices. With its focus on performance, security, and customization, Nitrux Linux presents a compelling case for those seeking to break free from the constraints of proprietary software and embrace the freedom and flexibility of open-source computing.
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...Neo4j
Leonard Jayamohan, Partner & Generative AI Lead, Deloitte
This keynote will reveal how Deloitte leverages Neo4j’s graph power for groundbreaking digital twin solutions, achieving a staggering 100x performance boost. Discover the essential role knowledge graphs play in successful generative AI implementations. Plus, get an exclusive look at an innovative Neo4j + Generative AI solution Deloitte is developing in-house.
DevOps and Testing slides at DASA ConnectKari Kakkonen
My and Rik Marselis slides at 30.5.2024 DASA Connect conference. We discuss about what is testing, then what is agile testing and finally what is Testing in DevOps. Finally we had lovely workshop with the participants trying to find out different ways to think about quality and testing in different parts of the DevOps infinity loop.
GraphRAG is All You need? LLM & Knowledge GraphGuy Korland
Guy Korland, CEO and Co-founder of FalkorDB, will review two articles on the integration of language models with knowledge graphs.
1. Unifying Large Language Models and Knowledge Graphs: A Roadmap.
https://arxiv.org/abs/2306.08302
2. Microsoft Research's GraphRAG paper and a review paper on various uses of knowledge graphs:
https://www.microsoft.com/en-us/research/blog/graphrag-unlocking-llm-discovery-on-narrative-private-data/
Transcript: Selling digital books in 2024: Insights from industry leaders - T...BookNet Canada
The publishing industry has been selling digital audiobooks and ebooks for over a decade and has found its groove. What’s changed? What has stayed the same? Where do we go from here? Join a group of leading sales peers from across the industry for a conversation about the lessons learned since the popularization of digital books, best practices, digital book supply chain management, and more.
Link to video recording: https://bnctechforum.ca/sessions/selling-digital-books-in-2024-insights-from-industry-leaders/
Presented by BookNet Canada on May 28, 2024, with support from the Department of Canadian Heritage.
Essentials of Automations: The Art of Triggers and Actions in FMESafe Software
In this second installment of our Essentials of Automations webinar series, we’ll explore the landscape of triggers and actions, guiding you through the nuances of authoring and adapting workspaces for seamless automations. Gain an understanding of the full spectrum of triggers and actions available in FME, empowering you to enhance your workspaces for efficient automation.
We’ll kick things off by showcasing the most commonly used event-based triggers, introducing you to various automation workflows like manual triggers, schedules, directory watchers, and more. Plus, see how these elements play out in real scenarios.
Whether you’re tweaking your current setup or building from the ground up, this session will arm you with the tools and insights needed to transform your FME usage into a powerhouse of productivity. Join us to discover effective strategies that simplify complex processes, enhancing your productivity and transforming your data management practices with FME. Let’s turn complexity into clarity and make your workspaces work wonders!
Elevating Tactical DDD Patterns Through Object CalisthenicsDorra BARTAGUIZ
After immersing yourself in the blue book and its red counterpart, attending DDD-focused conferences, and applying tactical patterns, you're left with a crucial question: How do I ensure my design is effective? Tactical patterns within Domain-Driven Design (DDD) serve as guiding principles for creating clear and manageable domain models. However, achieving success with these patterns requires additional guidance. Interestingly, we've observed that a set of constraints initially designed for training purposes remarkably aligns with effective pattern implementation, offering a more ‘mechanical’ approach. Let's explore together how Object Calisthenics can elevate the design of your tactical DDD patterns, offering concrete help for those venturing into DDD for the first time!
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...DanBrown980551
Do you want to learn how to model and simulate an electrical network from scratch in under an hour?
Then welcome to this PowSyBl workshop, hosted by Rte, the French Transmission System Operator (TSO)!
During the webinar, you will discover the PowSyBl ecosystem as well as handle and study an electrical network through an interactive Python notebook.
PowSyBl is an open source project hosted by LF Energy, which offers a comprehensive set of features for electrical grid modelling and simulation. Among other advanced features, PowSyBl provides:
- A fully editable and extendable library for grid component modelling;
- Visualization tools to display your network;
- Grid simulation tools, such as power flows, security analyses (with or without remedial actions) and sensitivity analyses;
The framework is mostly written in Java, with a Python binding so that Python developers can access PowSyBl functionalities as well.
What you will learn during the webinar:
- For beginners: discover PowSyBl's functionalities through a quick general presentation and the notebook, without needing any expert coding skills;
- For advanced developers: master the skills to efficiently apply PowSyBl functionalities to your real-world scenarios.
Securing your Kubernetes cluster_ a step-by-step guide to success !KatiaHIMEUR1
Today, after several years of existence, an extremely active community and an ultra-dynamic ecosystem, Kubernetes has established itself as the de facto standard in container orchestration. Thanks to a wide range of managed services, it has never been so easy to set up a ready-to-use Kubernetes cluster.
However, this ease of use means that the subject of security in Kubernetes is often left for later, or even neglected. This exposes companies to significant risks.
In this talk, I'll show you step-by-step how to secure your Kubernetes cluster for greater peace of mind and reliability.
Unlocking Productivity: Leveraging the Potential of Copilot in Microsoft 365, a presentation by Christoforos Vlachos, Senior Solutions Manager – Modern Workplace, Uni Systems
3. Uses of JSP Constructs
• Scripting elements calling servletSimple Scripting elements calling servlet
code directly
• Scripting elements calling servlet
p
Application
g g
code indirectly (by means of utility
classes)
B• Beans
• Servlet/JSP combo (MVC)
MVC ith JSP i l• MVC with JSP expression language
• Custom tags
MVC ith b t t d
Complex
Application
• MVC with beans, custom tags, and
a framework like Struts or JSF
6
Why Combine Servlets & JSP?
• Typical picture: use JSP to make it easier to
d l d i i h HTMdevelop and maintain the HTML content
– For simple dynamic code, call servlet code from
scripting elementsscripting elements
– For slightly more complex applications, use custom
classes called from scripting elements
– For moderately complex applications,
use beans and custom tags
• But that’s not enough• But, that s not enough
– For complex processing, starting with JSP is awkward
– Despite the ease of separating the real code into separatep p g p
classes, beans, and custom tags, the assumption behind
JSP is that a single page gives a single basic look
7
4. Possibilities for Handling a
Single RequestSingle Request
• Servlet only. Works well when:
O i bi E i– Output is a binary type. E.g.: an image
– There is no output. E.g.: you are doing forwarding or redirection as
in Search Engine example.
– Format/layout of page is highly variable. E.g.: portal.
• JSP only. Works well when:
– Output is mostly character data. E.g.: HTMLp y g
– Format/layout mostly fixed.
• Combination (MVC architecture). Needed when:
A i l t ill lt i lti l b t ti ll diff t– A single request will result in multiple substantially different-
looking results.
– You have a large development team with different team members
doing the Web development and the business logicdoing the Web development and the business logic.
– You perform complicated data processing, but have a relatively
fixed layout.
8
MVC Misconceptions
• An elaborate framework is necessary
– Frameworks are sometimes useful
• Struts
• JavaServer Faces (JSF)JavaServer Faces (JSF)
– They are not required!
• Implementing MVC with the builtin RequestDispatcher
works very well for most simple and moderately complexworks very well for most simple and moderately complex
applications
• MVC totally changes your overall system
design
– You can use MVC for individual requests
Thi k f it th MVC h t th– Think of it as the MVC approach, not the
MVC architecture
• Also called the Model 2 approach9
6. Bean Properties: Examples
Method Names Property Name Example JSP Usage
getFirstName
setFirstName
firstName <jsp:getProperty … property="firstName"/>
<jsp:setProperty … property="firstName"/>
${customer.firstName}
isExecutive
setExecutive
(boolean property)
executive <jsp:getProperty … property="executive"/>
<jsp:setProperty … property="executive"/>
${customer.executive}
getExecutive
setExecutive
(boolean property)
executive <jsp:getProperty … property="executive"/>
<jsp:setProperty … property="executive"/>
${customer.executive}
getZIP
setZIP
ZIP <jsp:getProperty … property="ZIP"/>
<jsp:setProperty … property="ZIP"/>
${address.ZIP}
12
Note 1: property name does not exist anywhere in your code. It is just a shortcut for the method name.
Note 2: property name is derived only from method name. Instance variable name is irrelevant.
Example: StringBean
package coreservlets;
public class StringBean {
private String message = "No message specified";
public String getMessage() {
return(message);
}
public void setMessage(String message) {
hithis.message = message;
}
}
• Beans installed in normal Java directory
– Eclipse: src/folderMatchingPackage
l d / / l /f ld h k– Deployed: …/WEB-INF/classes/folderMatchingPackage
• Beans (and utility classes) must always be in packages!
13
8. Implementing MVC with
RequestDispatcherRequestDispatcher
1. Define beans to represent result data
– Ordinary Java classes with at least one getBlah method
2. Use a servlet to handle requests
S l d h k f i i– Servlet reads request parameters, checks for missing
and malformed data, calls business logic, etc.
3. Obtain bean instances3. Obtain bean instances
– The servlet invokes business logic (application-specific
code) or data-access code to obtain the results.
4. Store the bean in the request, session, or
servlet context
Th l t ll tAtt ib t th t i– The servlet calls setAttribute on the request, session, or
servlet context objects to store a reference to the beans
that represent the results of the request.16
Implementing MVC with
RequestDispatcher (Continued)RequestDispatcher (Continued)
5. Forward the request to a JSP page.
– The servlet determines which JSP page is appropriate to
the situation and uses the forward method of
RequestDispatcher to transfer control to that page.RequestDispatcher to transfer control to that page.
6. Extract the data from the beans.
– JSP 1.2: the JSP page accesses beans with jsp:useBeanp g j p
and a scope matching the location of step 4. The page
then uses jsp:getProperty to output the bean properties.
JSP 2 0: the JSP page uses– JSP 2.0: the JSP page uses
${nameFromServlet.property} to output bean properties
– Either way, the JSP page does not create or modify the
bean; it merely extracts and displays data that the servlet
created.
17
9. Request Forwarding Example
public void doGet(HttpServletRequest request,
HttpServletResponse response)p p p )
throws ServletException, IOException {
... // Do business logic and get data
String operation = request.getParameter("operation");
if (operation == null) {if (operation == null) {
operation = "unknown";
}
String address;
if (operation.equals("order")) {
address = "/WEB-INF/Order.jsp";
} else if (operation.equals("cancel")) {
address = "/WEB-INF/Cancel jsp";address = /WEB INF/Cancel.jsp ;
} else {
address = "/WEB-INF/UnknownOperation.jsp";
}
RequestDispatcher dispatcher =
request.getRequestDispatcher(address);
dispatcher.forward(request, response);
}18
jsp:useBean in MVC vs.
in Standalone JSP Pagesin Standalone JSP Pages
• The JSP page should not create
h bjthe objects
– The servlet, not the JSP page, should create all the data
objects So to guarantee that the JSP page will not createobjects. So, to guarantee that the JSP page will not create
objects, you should use
<jsp:useBean ... type="package.Class" />
i d finstead of
<jsp:useBean ... class="package.Class" />
• The JSP page should not modify
the objects
S h ld j b– So, you should use jsp:getProperty but not
jsp:setProperty.
19
11. Request-Based Data Sharing
• Servlet
ValueObject value = LookupService.findResult(...);
request.setAttribute("key", value);
RequestDispatcher dispatcher =
request.getRequestDispatcher
("/WEB-INF/SomePage.jsp");
dispatcher.forward(request, response);
• JSP 1.2
<jsp:useBean id="key" type="somePackage.ValueObject"<jsp:useBean id key type somePackage.ValueObject
scope="request" />
<jsp:getProperty name="key" property="someProperty" />
JSP 2 0
Name chosen by the servlet
• JSP 2.0
${key.someProperty}
22
Name chosen by the servlet.
Name of accessor method, minus the
word "get", with next letter changed
to lower case.
Request-Based Data Sharing:
Simplified ExampleSimplified Example
• Servlet
C t C t
Assume that the Customer constructor
handles missing/malformed data.
Customer myCustomer =
Lookup.findCust(request.getParameter("customerID"));
request.setAttribute("customer", myCustomer);
RequestDispatcher dispatcher =RequestDispatcher dispatcher
request.getRequestDispatcher
("/WEB-INF/SomePage.jsp");
dispatcher.forward(request, response);
• JSP 1.2
<jsp:useBean id="customer" type="somePackage.Customer"j p yp g
scope="request" />
<jsp:getProperty name="customer" property="firstName"/>
• JSP 2 0• JSP 2.0
${customer.firstName}
23
Note: the Customer class must
have a method called “getFirstName”.
12. Session-Based Data Sharing
• Servlet
l bj l k i fi d l ( )ValueObject value = LookupService.findResult(...);
HttpSession session = request.getSession();
session.setAttribute("key", value);
RequestDispatcher dispatcher =RequestDispatcher dispatcher
request.getRequestDispatcher
("/WEB-INF/SomePage.jsp");
dispatcher.forward(request, response);
• JSP 1.2
<jsp:useBean id="key" type="somePackage.ValueObject"j p y yp g j
scope="session" />
<jsp:getProperty name="key" property="someProperty" />
• JSP 2 0JSP 2.0
${key.someProperty}
24
Session-Based Data Sharing:
VariationVariation
• Redirect to page instead of forwarding to it
U dR di i d f R Di h f d– Use response.sendRedirect instead of RequestDispatcher.forward
• Distinctions: with sendRedirect:
– User sees JSP URL (user sees only servlet URL with( y
RequestDispatcher.forward)
– Two round trips to client (only one with forward)
• Advantage of sendRedirectAdvantage of sendRedirect
– User can visit JSP page separately
• User can bookmark JSP page
Disadvantages of sendRedirect• Disadvantages of sendRedirect
– Two round trips to server is more expensive
– Since user can visit JSP page without going through servlet first,
b d i h b il blbean data might not be available
• So, JSP page needs code to detect this situation
25
13. ServletContext-Based Data
Sharing (Rare)Sharing (Rare)
• Servlet
synchronized(this) {
ValueObject value = SomeLookup.findResult(...);
getServletContext().setAttribute("key", value);
Req estDispatcher dispatcherRequestDispatcher dispatcher =
request.getRequestDispatcher
("/WEB-INF/SomePage.jsp");
dispatcher forward(request response);dispatcher.forward(request, response);
}
• JSP 1.2
<j B id "k " t " P k V l Obj t"<jsp:useBean id="key" type="somePackage.ValueObject"
scope="application" />
<jsp:getProperty name="key" property="someProperty" />
• JSP 2.0
${key.someProperty}
26
Relative URLs in JSP Pages
• Issue:
– Forwarding with a request dispatcher is transparent to the
client. Original URL is only URL browser knows about.
• Why does this matter?• Why does this matter?
– What will browser do with tags like the following?
<img src="foo.gif" …>
<link rel="stylesheet"
href="my-styles.css"href my styles.css
type="text/css">
<a href="bar.jsp">…</a>
– Browser treats addresses as relative to servlet URL
27
15. Bank Account Balances:
Servlet CodeServlet Code
public class ShowBalance extends HttpServlet {
public void doGet(HttpServletRequest request,p ( p q q ,
HttpServletResponse response)
throws ServletException, IOException {
BankCustomer currentCustomer =
BankCustomerLookup getCustomerBankCustomerLookup.getCustomer
(request.getParameter("id"));
request.setAttribute("customer", currentCustomer);
String address;
if (currentCustomer == null) {
address =
"/WEB-INF/bank-account/UnknownCustomer.jsp";
} else if (currentCustomer getBalance() < 0) {} else if (currentCustomer.getBalance() < 0) {
address =
"/WEB-INF/bank-account/NegativeBalance.jsp";
} ...
RequestDispatcher dispatcher =
request.getRequestDispatcher(address);
dispatcher.forward(request, response);
30
Bank Account Balances:
BeanBean
public class BankCustomer {
private final String id, firstName, lastName;p g , , ;
private final double balance;
public BankCustomer(String id,
String firstName
Since the constructor is called from Java only
(never from JSP), the requirement for a zero-arg
constructor is eliminated. Also, since bean state
is set only with constructor, rather than with
jsp:setProperty, we can eliminate setter
methods and make the class immutable.
String firstName,
String lastName,
double balance) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.balance = balance;
}}
// Getters for four instance variables. No setters.
public double getBalanceNoSign() {
return(Math.abs(balance));
}
}31
16. Bank Account Balances:
Business LogicBusiness Logic
public class BankCustomerLookup {
private static Map<String,BankCustomer> customers;private static Map<String,BankCustomer> customers;
static {
// Populate Map with some sample customersp p p
}
…
public static BankCustomer getCustomer(String id) {
return(customers.get(id));
}}
}
32
Bank Account Balances:
Input FormInput Form
…
<fieldset><fieldset>
<legend>Bank Account Balance</legend>
<form action="./show-balance">
Customer ID: <input type="text" name="id"><br>p yp
<input type="submit" value="Show Balance">
</form>
</fieldset> For the servlet, use the address http://host/appName/show-balance that is set via url-pattern in web.xml
…
33
17. Bank Account Balances:
JSP 1 2 Code (Negative Balance)JSP 1.2 Code (Negative Balance)
…
<BODY>
<TABLE BORDER=5 ALIGN="CENTER">
<TR><TH CLASS="TITLE">
We Know Where You Live!</TABLE>
<P><P>
<IMG SRC="/bank-support/Club.gif" ALIGN="LEFT">
<jsp:useBean id="customer"
type="coreservlets.BankCustomer"
scope="request" />
Watch out,
<jsp:getProperty name="customer"
property="firstName" />property= firstName />,
we know where you live.
<P>
Pay us the $<jsp:getProperty name="customer"
/property="balanceNoSign" />
you owe us before it is too late!
</BODY></HTML>
34
Bank Account Balances:
JSP 2 0 Code (Negative Balance)JSP 2.0 Code (Negative Balance)
…
<BODY><BODY>
<TABLE BORDER=5 ALIGN="CENTER">
<TR><TH CLASS="TITLE">
We Know Where You Live!</TABLE>
<P>
<IMG SRC="/bank-support/Club.gif" ALIGN="LEFT">
Watch out, ${customer.firstName},
we know where you live.
<P>
Pay us the $${customer.balanceNoSign}
b f it i t l t !you owe us before it is too late!
</BODY></HTML>
35
18. Bank Account Balances:
web xmlweb.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2 4" ><web app version 2.4 ...>
<!-- Use the URL http://host/app/show-balance instead of
http://host/app/servlet/coreservlets.ShowBalance -->
<servlet>
<servlet-name>ShowBalance</servlet-name>
<servlet-class>coreservlets.ShowBalance</servlet-class>
</servlet>
l i<servlet-mapping>
<servlet-name>ShowBalance</servlet-name>
<url-pattern>/show-balance</url-pattern>
</servlet-mapping></servlet-mapping>
...
</web-app>
36
Bank Account Balances:
ResultsResults
37
19. Comparing Data-Sharing
Approaches: RequestApproaches: Request
• Goal
– Display a random number to the user
Type of sharing• Type of sharing
– Each request should result in a new number, so request-
based sharing is appropriate.g pp p
38
Request-Based Sharing: Bean
package coreservlets;
public class NumberBean {
private final double num;
public NumberBean(double number) {
this.num = number;
}}
public double getNumber() {
return(num);etu ( u );
}
}
The property name in JSP will be “number”. The property name is derived from the method name, not from
39
The property name in JSP will be number . The property name is derived from the method name, not from
the instance variable name. Also note the lack of a corresponding setter.
20. Request-Based Sharing: Servlet
public class RandomNumberServlet extends HttpServlet {
public void doGet(HttpServletRequest requestpublic void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
NumberBean bean =NumberBean bean
RanUtils.getRandomNum(request.getParameter("range"));
request.setAttribute("randomNum", bean);
String address = "/WEB-INF/mvc-sharing/RandomNum.jsp";g / / g/ j p ;
RequestDispatcher dispatcher =
request.getRequestDispatcher(address);
dispatcher.forward(request, response);p q p
}
}
40
Request-Based Sharing:
Business LogicBusiness Logic
public class RanUtils {
public static NumberBean getRandomNum(String rangeString) {public static NumberBean getRandomNum(String rangeString) {
double range;
try {
range = Double.parseDouble(rangeString);range Double.parseDouble(rangeString);
} catch(Exception e) {
range = 10.0;
}}
return(new NumberBean(Math.random() * range));
}
}
41
23. Request-Based Sharing:
ResultsResults
46
Comparing Data-Sharing
Approaches: SessionApproaches: Session
• Goal
– Display users’ first and last names.
– If the users fail to tell us their name, we want to use
whatever name they gave us previouslywhatever name they gave us previously.
– If the users do not explicitly specify a name and no
previous name is found, a warning should be displayed.
• Type of sharing
D i d f h li i b d h i i– Data is stored for each client, so session-based sharing is
appropriate.
47
24. Session-Based Sharing: Bean
public class NameBean implements Serializable {
private String firstName = "Missing first name";p g g
private String lastName = "Missing last name";
public String getFirstName() {
return(firstName);return(firstName);
}
public void setFirstName(String firstName) {
if (!isMissing(firstName)) {if (!isMissing(firstName)) {
this.firstName = firstName;
}
}
... // getLastName, setLastName
private boolean isMissing(String value) {
return((value == null) || (value.trim().equals("")));
}
}
48
Session-Based Sharing: Servlet
public class RegistrationServlet extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
HttpSession session = request.getSession();
synchronized(session) {y ( ) {
NameBean nameBean =
(NameBean)session.getAttribute("name");
if (nameBean == null) {
nameBean = new NameBean();();
session.setAttribute("name", nameBean);
}
nameBean.setFirstName(request.getParameter("firstName"));
nameBean.setLastName(request.getParameter("lastName"));nameBean.setLastName(request.getParameter( lastName ));
String address = "/WEB-INF/mvc-sharing/ShowName.jsp";
RequestDispatcher dispatcher =
request.getRequestDispatcher(address);
dispatcher forward(request response);dispatcher.forward(request, response);
}
}
}
49
25. Session-Based Sharing: JSP 1.2
…
<BODY><BODY>
<H1>Thanks for Registering</H1>
<jsp:useBean id="name"
type="coreservlets NameBean"type= coreservlets.NameBean
scope="session" />
<H2>First Name:
<jsp:getProperty name="name"<jsp:getProperty name name
property="firstName" /></H2>
<H2>Last Name:
<jsp:getProperty name="name"jsp:get ope ty a e a e
property="lastName" /></H2>
</BODY></HTML>
50
Session-Based Sharing: JSP 2.0
…
<BODY><BODY>
<H1>Thanks for Registering</H1>
<H2>First Name: ${name.firstName}</H2>
<H2>Last Name: ${name.lastName}</H2>
</BODY></HTML>
51
26. Session-Based Sharing:
ResultsResults
Note: url patternNote: url-pattern
in web.xml is
"register".
52
Comparing Data-Sharing
Approaches: ServletContextApproaches: ServletContext
• Goal
– Display a prime number of a specified length.
– If the user fails to tell us the desired length, we want to
use whatever prime number we most recently computeduse whatever prime number we most recently computed
for any user.
• Type of sharing
– Data is shared among multiple clients, so application-
based sharing is appropriatebased sharing is appropriate.
53
27. ServletContext-Based Sharing:
BeanBean
package coreservlets;
import java.math.BigInteger;p j g g ;
public class PrimeBean {
private BigInteger prime;
public PrimeBean(String lengthString) {
int length = 150;
try {t y {
length = Integer.parseInt(lengthString);
} catch(NumberFormatException nfe) {}
this.prime = Primes.nextPrime(Primes.random(length));
}}
public BigInteger getPrime() {
return(prime);return(prime);
}
…
}54
ServletContext-Based Sharing:
ServletServlet
public class PrimeServlet extends HttpServlet {
public void doGet(HttpServletRequest request,p ( p q q ,
HttpServletResponse response)
throws ServletException, IOException {
String length = request.getParameter("primeLength");
ServletContext context = getServletContext();ServletContext context = getServletContext();
synchronized(this) {
if ((context.getAttribute("primeBean") == null) ||
(!isMissing(length))) {
PrimeBean primeBean = new PrimeBean(length);
context.setAttribute("primeBean", primeBean);
}
String address = "/WEB-INF/mvc-sharing/ShowPrime.jsp";g / / g/ j p ;
RequestDispatcher dispatcher =
request.getRequestDispatcher(address);
dispatcher.forward(request, response);
}}
}
… // Definition of isMissing: null or empty string
}55
30. Forwarding from JSP Pages
<% String destination;
if (Math random() > 0 5) {if (Math.random() > 0.5) {
destination = "/examples/page1.jsp";
} else {
destination = "/examples/page2 jsp";destination = /examples/page2.jsp ;
}
%>
<jsp:forward page="<%= destination %>" /><jsp:forward page <% destination %> />
• Legal but bad idea• Legal, but bad idea
– Business and control logic belongs in servlets
– Keep JSP focused on presentationp p
60
Including Pages Instead of
Forwarding to ThemForwarding to Them
• With the forward method of
RequestDispatcher:RequestDispatcher:
– New page generates all of the output
– Original page cannot generate any outputg p g g y p
• With the include method of
RequestDispatcher:
O t t b t d b lti l– Output can be generated by multiple pages
– Original page can generate output before and after the
included page
– Original servlet does not see the output of the included
page (for this, see later topic on servlet/JSP filters)
– Applicationspp
• Portal-like applications (see first example)
• Setting content type for output (see second example)
61
31. Using RequestDispatcher.include:
portalsportals
response.setContentType("text/html");
String firstTable, secondTable, thirdTable;
if ( di i ) {if (someCondition) {
firstTable = "/WEB-INF/Sports-Scores.jsp";
secondTable = "/WEB-INF/Stock-Prices.jsp";
thirdTable = "/WEB-INF/Weather.jsp";
} else if ( ) { }} else if (...) { ... }
RequestDispatcher dispatcher =
request.getRequestDispatcher("/WEB-INF/Header.jsp");
dispatcher.include(request, response);
dispatcher =dispatcher =
request.getRequestDispatcher(firstTable);
dispatcher.include(request, response);
dispatcher =
request.getRequestDispatcher(secondTable);request.getRequestDispatcher(secondTable);
dispatcher.include(request, response);
dispatcher =
request.getRequestDispatcher(thirdTable);
dispatcher.include(request, response);p q p
dispatcher =
request.getRequestDispatcher("/WEB-INF/Footer.jsp");
dispatcher.include(request, response);
62
Using RequestDispatcher.include:
Setting Content-Type of OutputSetting Content Type of Output
// From Ajax example
public void doGet( ) {public void doGet(...) ... {
...
if ("xml".equals(format)) {
response.setContentType("text/xml");p yp ( )
outputPage = "/WEB-INF/results/cities-xml.jsp";
} else if ("json".equals(format)) {
response.setContentType("application/json");
outputPage = "/WEB-INF/results/cities-json.jsp";
} else {
response.setContentType("text/plain");
t tP "/WEB INF/ lt / iti t i j "outputPage = "/WEB-INF/results/cities-string.jsp";
}
RequestDispatcher dispatcher =
request getRequestDispatcher(outputPage);request.getRequestDispatcher(outputPage);
dispatcher.include(request, response);
}
63
32. cities-xml.jsp
<?xml version="1.0" encoding="UTF-8"?>
<cities><cities>
<city>
<name>${cities[0].name}</name>
<time>${cities[0] shortTime}</time><time>${cities[0].shortTime}</time>
<population>${cities[0].population}</population>
</city>
......
</cities>
• Notes
– Because I use .jsp (not .jspx) and classic JSP syntax, the
default content-type is text/html
– I could use <%@ page contentType="text/xml" %> hereI could use <%@ page contentType text/xml %> here,
but it is more convenient to do it in calling servlet and
keep this page simple and focused on presentation
64
Summary
• Use MVC (Model 2) approach when:
O b i i ill l i h b i l k– One submission will result in more than one basic look
– Several pages have substantial common processing
• ArchitectureArchitecture
– A servlet answers the original request
– Servlet does the real work & stores results in beans
• Beans stored in HttpServletRequest, HttpSession, or
ServletContext
– Servlet forwards to JSP page via forward method of
RequestDispatcher
– JSP page reads data from beans
• JSP 1 2: jsp:useBean with appropriate scope (requestJSP 1.2: jsp:useBean with appropriate scope (request,
session, or application) plus jsp:getProperty
• JSP 2.x: ${beanName.propertyName}
65