|
Creating a dynamic Web project You create and maintain the resources for your Web applications in Web projects. Dynamic Web projects enable you to create resources such as JavaServer Pages and servlets, and use technologies such as Struts and JavaServer Faces. Note: If you want to specify a target server for the Web project you are going to create, you should first enable server targeting support on the J2EE preferences page. To create a new dynamic Web project:
You can now begin creating or importing content for your Web project using Web Site Designer, Web resource editors (such as Page Designer), the New File wizards, or the Import wizards available from the File menu. Viewing and editing a project's Java build path A project's Java build path can either be defined when creating the project with the New Wizard or later in the project's property dialog. The Java build path settings dialog is the same in both cases. To view and edit a project's Java build path, follow these steps:
Working in the References page in the deployment descriptor editor The References page in the Web deployment descriptor editor lets you add or remove references to EJBs (Enterprise Java Beans) in the deployment descriptor. This page also lets you add or remove references to resources, resource environments, and JSP tag libraries. To work with the References page of the Web deployment descriptor editor:
Creating servlets The Servlet wizard walks you through the process of creating Java servlets, step-by-step, and provides you with output files you can use "as is" or modify for your Web application. The resulting servlets run on the WebSphere Application Server or other J2EE-compliant Web server, and provide server-side processing for dynamic content. To create a servlet, do the following:
The servlet is generated. To run the generated servlet, launch the WebSphere test environment. Add the following code to the ReservationController servlet:
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String firstName = req.getParameter("firstName");
String lastName = req.getParameter("lastName");
String cruise = req.getParameter("cruise");
Context env = null;
TravelAgent agent = null;
TravelAgentHome agentHome = null;
Object home = null;
try {
env = (Context) new InitialContext();
home = env.lookup("ejb/com/titan/travelagent/TravelAgentHome");
} catch (NamingException ne) {
log("ReservationController: can not lookup TravelAgentHome", ne);
try {
env.close();
} catch (NamingException e) {
log("ReservationController: can not close JNDI Context", e);
}
}
agentHome = (TravelAgentHome) PortableRemoteObject.narrow(home, TravelAgentHome.class);
try {
agent = (TravelAgent) agentHome.create();
} catch (CreateException ce) {
log("ReservationController: can not create TravelAgent", ce);
}
agent.sendReservation(firstName, lastName, cruise);
RequestDispatcher dispatch = req.getRequestDispatcher("/index.jsp");
dispatch.forward(req, resp);
}
Add the following code to the com.titan.travelagent.TravelAgent interface:
public void sendReservation(String firstName, String lastName, String cruise) throws java.rmi.RemoteException; Add the following code to the com.titan.travelagent.TravelAgentBean class:
public void sendReservation(String firstName, String lastName, String cruise) {
try {
ic = new javax.naming.InitialContext();
qConnFactory = (javax.jms.QueueConnectionFactory) ic.lookup(fName);
queue = (javax.jms.Queue) ic.lookup(qName);
} catch (Exception e) {
System.err.println("TravelAgentBean: JNDI lookup failed " + e);
}
try {
qConn = qConnFactory.createQueueConnection();
qSession = qConn.createQueueSession(false, javax.jms.Session.AUTO_ACKNOWLEDGE);
qSender = qSession.createSender(queue);
|