Creating JMS application using JSP and JSP bean

Let's first create a JSP that displays the form to enter course details. We will also create a JSP bean to process the form data. Right-click on the WebContent folder under the project in the Project Explorer view and select New | JSP File. Create the JSP file named addCourse.jsp.

We will now create CourseDTO and the JSP bean called CourseJSPBean. Create the CourseDTO class in the packt.jee.eclipse.jms.dto package. Add the id, name, and credits properties, and the getters and setters for them:

import java.io.Serializable; 
public class CourseDTO implements Serializable { 
  private static final long serialVersionUID = 1L; 
  private int id; 
  private String name; 
  private int credits; 
   
  //getters and setters follow 
} 

Create CourseJSPBean in the packt.jee.eclipse.jms.jsp.beans package:

import packt.jee.eclipse.jms.dto.CourseDTO; 
 
public class CourseJSPBean { 
   
  private CourseDTO course = new CourseDTO(); 
   
  public void setId(int id) { 
    course.setId(id); 
  } 
  public String getName() { 
    return course.getName(); 
  } 
  public void setName(String name) { 
    course.setName(name); 
  } 
  public int getCredits() { 
    return course.getCredits(); 
  } 
  public void setCredits(int credits) { 
    course.setCredits(credits); 
  } 
  public void addCourse() { 
    //TODO: send CourseDTO object to a JMS queue 
  } 
} 

We will implement the code to send the CourseDTO object to the JMS queue later in the addCourse method. For now, add the following code to addCourse.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8" 
    pageEncoding="UTF-8"%> 
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-
8"> <title>Add Course</title> </head> <body> <!-- Check if form is posted --> <c:if test="${"POST".equalsIgnoreCase(pageContext.request.method) && pageContext.request.getParameter("submit") != null}"> <!-- Create CourseJSPBean --> <jsp:useBean id="courseService" class="packt.jee.eclipse.jms.jsp_beans.CourseJSPBean"
scope="page"></jsp:useBean> <!-- Set Bean properties with values from form submission --> <jsp:setProperty property="name" name="courseService"
param="course_name"/> <jsp:setProperty property="credits" name="courseService"
param="course_credits"/> <!-- Call addCourse method of the bean --> ${courseService.addCourse()} <b>Course detailed are sent to a JMS Queue. It will be
processed later</b> </c:if> <h2>New Course:</h2> <!-- Course data input form --> <form method="post"> <table> <tr> <td>Name:</td> <td> <input type="text" name="course_name"> </td> </tr> <tr> <td>Credits:</td> <td> <input type="text" name="course_credits"> </td> </tr> <tr> <td colspan="2"> <button type="submit" name="submit">Add</button> </td> </tr> </table> </form> </body> </html>

At the top of the JSP file, we check whether the form is submitted. If yes, we then create an instance of CourseJSPBean and set its properties with values from the form submission. Then, we call the addCourse method of the bean.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.60.62