Sie sind auf Seite 1von 29

Lab Test Revision

Week 7 Supplementary Class


JSP BASICS
JSP Basics
Scriplet Code is inserted in service
method.
<% JavaCode %>
Expression Expression is evaluated and
placed in output.
<%= JavaExpression %>
Page
Directive
(import)
import parts of a package
namespace into the local
scope of a Java class.
<%@ page import="pkg1" %>
<%@ page import="pkg1,pkg2"
%>
Declaration Instance variable/method of
translated servlet class
<%! declaration %>
Comment Documentation for yourself <%-- JSP comment --%>
<!-- HTML comment -->
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
Form Processing
welcome.html

<html>
<head><title>A simple form</title></head>
<body>
<form action="greet.jsp">
Enter your name:
<input type="text" name="fullname" />
<input type="submit" value="send" />
</form>
</body>
</html>
Form Processing
greet.jsp

<html><body>
<%
String fullname = request.getParameter("fullname");
out.println("Hi " + fullname
+ ". Welcome to SE!!<br />");
%>
</body></html>

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(String
name)
Returns an array of String objects
containing all of the values the given
request parameter has, or null if the
parameter does not exist.
MORE JSP
Forwarding Request (Internal)
// JSP tag (part of HTML)
<jsp:forward page="<localURL>" />

//another way - part of scriptlet (<% %>)
RequestDispatcher dispatcher =
request.getRequestDispatcher("<localURL>");
dispatcher.forward(request, response);

Servlet/JSP
Servlet/JSP
Servlet Engine
1.HTTP Request
3.HTTP Response
2.Hands over control
B
r
o
w
s
e
r

Redirecting Response (External)
// part of scriptlet (<% %>)
response.sendRedirect("<URL>");

Servlet Engine B
Servlet Engine A
Servlet/JSP
Servlet/JSP
1.HTTP Request
2.HTTP Response (I have moved to )
B
r
o
w
s
e
r

3.HTTP Request
4.HTTP Response
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 another
resource.
Yes, a new HTTP
Request is created.
Will the URL that the
client sees change?
No Yes
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?
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?
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
Methods of HttpSession
Method
setAttribute(String name, Object
value)
Binds an object to this session, using
the name specified.

*Helpful for login
removeAttribute(String name) Removes the object bound with the
specified name from this session.

*Helpful for logout
setMaxInactiveInterval(int interval) Sets the timeout interval
invalidate() Invalids and removes the session
JSP Include Action Tag
content.jsp
<jsp:include page=header.jsp"/>

Xxxxxxx
xxxxxxx
xxxxxxx
xxxxxxx
xxxxxxx
xxxxxxx
xxxxxxx
-------
-------
-------
-------
-------
-------
-------
<jsp:include page="/header.jsp"/>
header.jsp
content.jsp
Xxxxxxx
xxxxxxx
xxxxxxx
xxxxxxx
xxxxxxx
xxxxxxx
xxxxxxx
Source
(content_jsp.java)
translation
compilation
1010101
1010101
0101010
1010101
0101010
1010101
0101010
Page Servlet
(content_jsp.class)
-------
-------
-------
-------
-------
-------
-------
Source
(header_jsp.java)
compilation
1010101
1010101
0101010
1010101
0101010
1010101
0101010
Page Servlet
(header_jsp.class)
Include Directive
content.jsp
<%@include file="<localUrl>"%>
Xxxxxxx
xxxxxxx
xxxxxxx
xxxxxxx
xxxxxxx
xxxxxxx
xxxxxxx
-------
-------
-------
-------
-------
-------
-------
<%@include file="/header.jsp"%>
header.jsp content.jsp
translation
Xxxxxxx
xxxxxxx
-------
-------
-------
xxxxxxx
xxxxxxx
Source
(content_jsp.java)
compilation
1010101
1010101
0101010
1010101
0101010
1010101
0101010
Page Servlet
(content_jsp.class)
Content of header.jsp
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.
SERVLET
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
Step 1: Subclass from HttpServlet
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class HelloWorldServlet extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<body>");
out.println("Hello World");
out.println("</body></html>");
out.close();
}
}
Step 2: Override doGet and/or doPost method
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class HelloWorldServlet extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<body>");
out.println("Hello World");
out.println("</body></html>");
out.close();
}
}
Step 3: Specify the content type
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class HelloWorldServlet extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<body>");
out.println("Hello World");
out.println("</body></html>");
out.close();
}
}
Step 4: Get a PrintWriter object
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class HelloWorldServlet extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<body>");
out.println("Hello World");
out.println("</body></html>");
out.close();
}
}
Step 5: Write data to the client
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class HelloWorldServlet extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<body>");
out.println("Hello World");
out.println("</body></html>");
out.close();
}
}
Step 6: Close the Writer
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class HelloWorldServlet extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<body>");
out.println("Hello World");
out.println("</body></html>");
out.close();
}
}
Handling Form Data
<form action="http://localhost:8080/NameServlet"
method=GET>

<form action="http://localhost:8080/NameServlet"
method=POST>

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
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 {

// ...
}
}

Das könnte Ihnen auch gefallen