8/11/2019 Lab Test Revision
1/29
Lab Test Revision
Week 7 Supplementary Class
8/11/2019 Lab Test Revision
2/29
JSP BASICS
8/11/2019 Lab Test Revision
3/29
JSP Basics
Scriplet Code is inserted in service
method.
Expression Expression is evaluated and
placed in output.
PageDirective
(import)
import parts of a packagenamespace into the local
scope of a Java class.
Declaration Instance variable/method of
translated servlet class
Comment Documentation for yourself
8/11/2019 Lab Test Revision
4/29
Implicit Variables
Name Class Description
request javax.servlet.http.HttpServletRequest Represents the request that triggers
the processing of the current page
response javax.servlet.http.HttpServletResponse Contains information created by the
JSP to be send back to the client
out Subclass of Writer
(javax.servlet.jsp.JspWriter)
Used to output information to the
client
8/11/2019 Lab Test Revision
5/29
Form Processing
welcome.html
A simple form
Enter your name:
8/11/2019 Lab Test Revision
6/29
Form Processing
greet.jsp
8/11/2019 Lab Test Revision
7/29
Form Processing Summary
Method
request.getParameter(String name) Returns the value of a request
parameter as a String, or null if the
parameter does not exist
request.getParameterValues(Stringname)
Returns an array of String objectscontaining all of the values the given
request parameter has, or null if the
parameter does not exist.
8/11/2019 Lab Test Revision
8/29
MORE JSP
8/11/2019 Lab Test Revision
9/29
Forwarding Request (Internal)
// JSP tag (part of HTML)
//another way - part of scriptlet ()
RequestDispatcher dispatcher =
request.getRequestDispatcher("");
dispatcher.forward(request, response);
Servlet/JSP
Servlet/JSP
Servlet Engine
1.HTTP Request
3.HTTP Response
2.Hands over control
Br
ow
ser
8/11/2019 Lab Test Revision
10/29
Redirecting Response (External)
// part of scriptlet ()
response.sendRedirect("");
Servlet Engine B
Servlet Engine A
Servlet/JSP
Servlet/JSP
1.HTTP Request
2.HTTP Response (I have moved to )
Br
ow
ser
3.HTTP Request
4.HTTP Response
8/11/2019 Lab Test Revision
11/29
Internal Forwarding vs. External Redirecting
Internal Forwarding External Redirecting
Is a new HTTP Request
created?
No, it is the same HTTP
Request. The control of
processing is handed
over to anotherresource.
Yes, a new HTTP
Request is created.
Will the URL that the
client sees change?
No Yes
8/11/2019 Lab Test Revision
12/29
Question!
page1.jsp
RequestDispatcher dispatcher =
request.getRequestDispatcher(p
age2.jsp");
String temp = Hello World;
request.addAttribute(temp,
temp);
dispatcher.forward(request,
response);
page2.jsp
String retrieved = (String)
request.getAttribute(temp);
What will be the value in
retrieved?
8/11/2019 Lab Test Revision
13/29
Question!
page1.jsp
String temp = Hello World;
request.addAttribute(temp,
temp);response.sendRedirect(page2.js
p);
page2.jsp
String retrieved = (String)
request.getAttribute(temp);
What will be the value in
retrieved?
8/11/2019 Lab Test Revision
14/29
Implicit Variables
Name Class Description
request javax.servlet.http.HttpServletRequest Represents the request that triggers
the processing of the current page
response Javax.servlet.http.HttpServletResponse Contains information created by the
JSP to be send back to the client
out Subclass of Writer
(javax.servlet.jsp.JspWriter)
Use to output information to the
client
session Javax.servlet.http.HttpSession Allows you to access the client's
session data, managed by the server
8/11/2019 Lab Test Revision
15/29
Methods of HttpSession
Method
setAttribute(String name, Object
value)
Binds an object to this session, using
the name specified.
*Helpful for loginremoveAttribute(String name) Removes the object bound with the
specified name from this session.
*Helpful for logout
setMaxInactiveInterval(int interval) Sets the timeout intervalinvalidate() Invalids and removes the session
8/11/2019 Lab Test Revision
16/29
JSP Include Action Tag
content.jsp
8/11/2019 Lab Test Revision
17/29
Include Directive
content jsp
Xxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxx
xxxxxxxxxxxxxx
-----------------------------------
--------------
header.jspcontent.jsp
translation
Xxxxxxxxxxxxxx---------------------xxxxxxxxxxxxxx
Source(content_jsp.java)
compilation
101010110101010101010101010101010101010101
0101010
Page Servlet(content_jsp.class)
Content of header.jsp
8/11/2019 Lab Test Revision
18/29
JSP Page Composition Summary
JSP Include Action Tag Include Directive
How is the compilation
done?
The separate pages are
compiled separately.
The content of the page
that is included is added
into the page which
includes it and so, they
are compiled together.
What happens if there is
an error in one of the
pages?
The other page will still
be loaded.
The other page will not
be loaded as they are
compiled together.
8/11/2019 Lab Test Revision
19/29
SERVLET
8/11/2019 Lab Test Revision
20/29
Steps to Write a Basic HTTP Servlet
Step 1: Subclass from HttpServlet
Step 2: Override doGet and/or doPost method
Step 3: Specify the content type
Step 4: Get a PrintWriter object Step 5: Write data to the client
Step 6: Close the Writer
8/11/2019 Lab Test Revision
21/29
Step 1: Subclass from HttpServlet
importjava.io.*;
importjavax.servlet.*;
importjavax.servlet.http.*;
public classHelloWorldServlet extendsHttpServlet {
public void doGet(HttpServletRequest request,HttpServletResponse response)
throwsServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("");
out.println("");out.println("Hello World");
out.println("");
out.close();
}
}
8/11/2019 Lab Test Revision
22/29
Step 2: Override doGet and/or doPost method
importjava.io.*;
importjavax.servlet.*;
importjavax.servlet.http.*;
public classHelloWorldServlet extendsHttpServlet {
public void doGet(HttpServletRequest request,HttpServletResponse response)
throwsServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("");
out.println("");out.println("Hello World");
out.println("");
out.close();
}
}
8/11/2019 Lab Test Revision
23/29
Step 3: Specify the content type
importjava.io.*;
importjavax.servlet.*;
importjavax.servlet.http.*;
public classHelloWorldServlet extendsHttpServlet {
public void doGet(HttpServletRequest request,HttpServletResponse response)
throwsServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("");
out.println("");out.println("Hello World");
out.println("");
out.close();
}
}
8/11/2019 Lab Test Revision
24/29
Step 4: Get a PrintWriter object
importjava.io.*;
importjavax.servlet.*;
importjavax.servlet.http.*;
public classHelloWorldServlet extendsHttpServlet {
public void doGet(HttpServletRequest request,HttpServletResponse response)
throwsServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("");
out.println("");out.println("Hello World");
out.println("");
out.close();
}
}
8/11/2019 Lab Test Revision
25/29
Step 5: Write data to the client
importjava.io.*;
importjavax.servlet.*;
importjavax.servlet.http.*;
public classHelloWorldServlet extendsHttpServlet {
public void doGet(HttpServletRequest request,HttpServletResponse response)
throwsServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("");
out.println("");out.println("Hello World");
out.println("");
out.close();
}
}
8/11/2019 Lab Test Revision
26/29
Step 6: Close the Writer
importjava.io.*;
importjavax.servlet.*;
importjavax.servlet.http.*;
public classHelloWorldServlet extendsHttpServlet {
public void doGet(HttpServletRequest request,HttpServletResponse response)
throwsServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("");
out.println("");out.println("Hello World");
out.println("");
out.close();
}
}
8/11/2019 Lab Test Revision
27/29
Handling Form Data
8/11/2019 Lab Test Revision
28/29
HTTP GET vs. HTTP POST
HTTP GET HTTP POST
Where is the form data? It is contained in the
URL of the HTTP request
It is contained in the
body of the HTTP
request
What is the URL on the
browser?
http://localhost:8080/N
ameServlet?fullname=L
ee
http://localhost:8080/N
ameServlet
When do you use it? If you want to expose
the parameter values.
E.g. when doing a search
If you want to hide the
parameter values.
E.g. Login
8/11/2019 Lab Test Revision
29/29
Configuring Servlet
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.WebServlet;
@WebServlet("/display)public class NameServlet extends HttpServlet {
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
// ...
}
}