Creating JSP to add a course

Let's now create a JSP page to add new courses. Right-click on the project in Package Explorer and select the New | Other... option. Type jsp in the filter box and select JSP File. Name the file addCourse.jsp. Eclipse will create the file in the src/main/webapp folder of the project.

Type the following code in addCourse.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8" 
    pageEncoding="UTF-8"%> 
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 
 
<!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> 
  <c:set var="errMsg" value="${null}"/> 
  <c:set var="displayForm" value="${true}"/> 
    <c:if test="${"POST".equalsIgnoreCase(pageContext.request.method) 
&& pageContext.request.getParameter("submit") != null}"> <jsp:useBean id="courseBean" class="packt.book.jee.eclipse.ch4.bean.Course"> <c:catch var="beanStorageException"> <jsp:setProperty name="courseBean" property="*" /> </c:catch> </jsp:useBean> <c:choose> <c:when test="${!courseBean.isValidCourse() || beanStorageException != null}"> <c:set var="errMsg" value="Invalid course details. Please
try again"/> </c:when> <c:otherwise> <c:redirect url="listCourse.jsp"/> </c:otherwise> </c:choose> </c:if> <h2>Add Course:</h2> <c:if test="${errMsg != null}"> <span style="color: red;"> <c:out value="${errMsg}"></c:out> </span> </c:if> <form method="post"> Name: <input type="text" name="name"> <br> Credits : <input type="text" name="credits"> <br> <button type="submit" name="submit">Add</button> </form> </body> </html>

Most of the code should be familiar, if you have read Chapter 2, Creating a Simple JEE Web Application (see the Using JSTL section). We have a form to add courses. At the top of the file, we check whether the post request is made; if so, store content of the form in courseBean (make sure that names of the form field are the same as the members defined in the bean). The new tag that we have used here is <c:catch>. It is like a try-catch block in Java. Any exception thrown from within the body of <c:catch> is assigned to the variable name declared in the var attribute. Here, we are not doing anything with beanStorageException; we are suppressing the exception. When an exception is thrown, the credits field of the Course bean will remain set to zero and it will be caught in the courseBean.isValidCourse method. If the course data is valid, then we redirect the request to the listCourse.jsp page using the JSTL <c:redirect> tag.

We need to add the isValidCourse method in the Course bean. Therefore, open the class in the editor and add the following method:

  public boolean isValidCourse() { 
    return name != null && credits != 0; 
  } 

We also need to create listCourse.jsp. For now, just create a simple JSP with no JSTL/Java code and with only one header in the body tag:

<h2>Courses:</h2> 

Right-click on addCourse.jsp in Package Explorer and select Run As | Run on Server. If you have configured Tomcat properly and added your project in Tomcat (as described in Chapter 2, Creating a Simple JEE Web Application), then you should see the JSP page running in the internal Eclipse browser. Test the page with both valid and invalid data (a wrong credit value; for example, a non-numeric value). If the data entered is valid, then you would be redirected to listCourse.jsp, or else the same page would be displayed with the error message.

Before we start writing JDBC code, let's learn some fundamental concepts of JDBC.

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

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