If it is not explicitly told in following recipe, setting up services are described for Ubuntu 20.04 server, but applications are described for Xubuntu 20.04 workstation. If you use different Ubuntu version or Linux distribution, settings as well as content, names and places of configuration files may be different!
Got it.

18. Web application

Review the presentation Java Web Frameworks

Tasks

  1. Add execute permissions to the geckodriver library1:

    cd ~/workspace/JTM/lib
    chmod +x geckodriver
  2. If necessary, change the server.port=8800 value in the .../src/main/resources/application.properties file to a different port number, such as 8801, according to the number assigned to you.
  3. Explore the package jtm.activity18.
  4. Implement and annotate the required main() method of the JettyApplication class and the teachers(...) method of the JettyController class to create a simple web application.
  5. Run the JettyApplication class as a Java application and log in to the browser page http://localhost:8800/.
  6. Implement the methods insertTeacher(...), findTeacher(...), and deleteTeacher(...) that:
    1. respond to the appropriate web folders of the application,
    2. take the parameters passed from the web browser,
    3. call the appropriate methods of the TeacherManager class in the package jtm.activity13, and
    4. set the response to success or failure and display the return value as HTML.

Fulfill the requirements of insertTeacher()

  1. The method responds to the folder /insertTeacher
  2. The method takes two optional parameters name and surname
  3. The method references the TeacherManager class method insertTeacher(String, String)
  4. When no parameters are passed, e.g. http://localhost:8800/insertTeacher is opened, it returns:
    Status: OK (200),
    HTML:

    <form action=''>
    Name: <input type='text' name='name' value=''><br/>
    Surname: <input type='text' name='surname' value=''><br/>
    <input type='submit' value='Insert'></form><br/>
    <a href='/'>Back</a>
  5. When valid parameters are passed, e.g. http://localhost:8800/insertTeacher?name=Tom&surname=Tom, it returns:
    Status: OK
    HTML:

    true<br/>
    <a href='/'>Back</a>
  6. If invalid parameters are passed, e.g. http://localhost:8800/insertTeacher?name=&surname=, it returns:
    HTTP status: Bad Request (400)

    false<br/>
    <a href='/'>Back</a>

Fulfill the requirements of findTeacher()

  1. The method responds to the folder /findTeacher
  2. The method takes two optional parameters name and surname
  3. The method calls the TeacherManager class method findTeacher(String, String)
  4. When no parameters are passed, e.g. http://localhost:8800/findTeacher, opens, it returns:
    Status: OK,
    HTML:

    <form action=''>
    Name: <input type='text' name='name' value=''><br/>
    Surname: <input type='text' name='surname' value=''><br/>
    <input type='submit' value='Find'></form><br/>
    <a href='/'>Back</a>
  5. When parameters are passed, e.g. http://localhost:8800/findTeacher?name=Tom&surname=Tom, it returns:
    Status OK
    HTML:

    <form action=''>
    Name: <input type='text' name='name' value=''><br/>
    Surname: <input type='text' name='surname' value=''><br/>
    <input type='submit' value='Find'></form><br/>
    <table>
    <tr>
    <td>11</td>
    <td>Tom</td>
    <td>Tom</td>
    </tr>
    </table><br>
    <a href='/'>Back</a>

    where each returned Teacher object is displayed in a separate table row.

Meet the requirements of deleteTeacher()

  1. The method responds to the folder /deleteTeacher
  2. The method requires one optional parameter id
  3. The method calls the deleteTeacher(int) method of the TeacherManager class
  4. When no parameters are passed, e.g. http://localhost:8800/deleteTeacher, is opened, it returns:
    Status: OK,
    HTML:

    <form action=''>
    ID: <input type='text' name='id' value=''><br/>
    <input type='submit' value='Delete'></form><br/>
    <a href='/'>Back</a>
  5. When a valid parameter is passed, e.g. http://localhost:8800/deleteTeacher?id=5, it returns:
    Status: OK
    HTML:

    true<br/>
    <a href='/'>Back</a>
  6. When invalid parameters are passed, e.g. http://localhost:8800/deleteTeacher?id=1, it returns:
    HTTP status: Bad Request

    false<br/>
    <a href='/'>Back</a>

Run Unit Tests

  1. JettyApplicationTest.java is a unit test that tests the web application using API calls.
  2. SeleniumWebDriverTests.java is a web GUI test that tests the web application by emulating user actions in the Firefox browser.

Additional Information

For Implementation:

  1. http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/bind/annotation/RequestMapping.html
  2. http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/bind/annotation/RequestParam.html
  3. http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/bind/annotation/ResponseBody.html
  4. https://tomcat.apache.org/tomcat-8.0-doc/servletapi/javax/servlet/http/HttpServletRequest.html
  5. https://tomcat.apache.org/tomcat-8.0-doc/servletapi/javax/servlet/http/HttpServletResponse.html

For API testing:

  1. Spring Boot testēšanas līdzekļi

GUI for testing:

  1. SeleniumIDE and Plugin for Firefox
  2. Katalon Recorder and Plugin for Firefox

  1. ^ When the project is extracted from the *.zip file, the execute property is lost for all files. Therefore, the SeleniumWebDriverTests execute permissions. Java file must be restored because Selenium tests require them.

  

Created by Valdis Vītoliņš on 2025-01-28 17:45
Last modified by Valdis Vītoliņš on 2025-01-28 17:48
 
Xwiki Powered
Creative Commons Attribution 3.0 Unported License