Java Reference
In-Depth Information
Create a
PrintWriter
from the
response
object:
PrintWriter out = response.getWriter();
Create the response to be sent to the browser. If the
validate()
method returns
a
Catalog
entity instance, the catalog id is not valid, as a
Catalog
entity instance
for the catalog id is already defined. Create an XML string that has root element
Catalog
and a sub-element
valid
with value set to
false
, and sub-elements
journal
,
publisher
,
edition
,
title
and
author
with values set to the properties
of the
Catalog
entity instance. If the
validate()
method returns
null
, the catalog is
valid, as a new
Catalog
entity instance for the catalog id is not defined. Construct an
XML string that has root element
valid
with value set to
true
.
if (catalog != null) {
out.println("<catalog>" + "<valid>false</valid>"+"<journal>" +
catalog.getJournal() + "</journal>" + "<publisher>" +
catalog.getPublisher() + "</publisher>" + "<edition>" +
catalog.getEdition() + "</edition>" + "<title>" +
catalog.getTitle() + "</title>" + "<author>" +
catalog.getAuthor() + "</author>" + "</catalog>");
} else {
out.println("<valid>true</valid>");
}
The
doPost()
method of the servlet is invoked when the input form is submitted
for a valid catalog id. In the
doPost()
method, create an
InitialContext
object.
Lookup the remote interface of the session bean using the remote JNDI name.
Retrieve the input form field values and invoke the
persist()
method of the session
bean to persist a new
Catalog
entity instance. The
doPost()
method is not invoked
with an Ajax request; therefore, the method does not return a response, but redirects
to a JSP that displays a message that the database has been updated with a new
catalog entry.
beanRemote.persist(catalogId, journal, publisher, edition, title,
author);
response.sendRedirect("catalog.jsp");
If an error is generated in updating the database, redirect the response to an error JSP
error.jsp
. The
EJB3ClientServlet
class is listed below:
package view;
import model.*;
import java.io.*;
import javax.naming.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class EJB3ClientServlet extends HttpServlet {
Search WWH ::
Custom Search