18. Web application
Review the presentation Java Web Frameworks
Tasks
- Fulfill the requirements of insertTeacher()
- Fulfill the requirements of findTeacher()
- Meet the requirements of deleteTeacher()
- Run Unit Tests
- Additional Information
Add execute permissions to the geckodriver library1:
cd ~/workspace/JTM/lib
chmod +x geckodriver- 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.
- Explore the package jtm.activity18.
- 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.
- Run the JettyApplication class as a Java application and log in to the browser page http://localhost:8800/.
- Implement the methods insertTeacher(...), findTeacher(...), and deleteTeacher(...) that:
- respond to the appropriate web folders of the application,
- take the parameters passed from the web browser,
- call the appropriate methods of the TeacherManager class in the package jtm.activity13, and
- set the response to success or failure and display the return value as HTML.
Fulfill the requirements of insertTeacher()
- The method responds to the folder /insertTeacher
- The method takes two optional parameters name and surname
- The method references the TeacherManager class method insertTeacher(String, String)
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>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>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()
- The method responds to the folder /findTeacher
- The method takes two optional parameters name and surname
- The method calls the TeacherManager class method findTeacher(String, String)
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>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()
- The method responds to the folder /deleteTeacher
- The method requires one optional parameter id
- The method calls the deleteTeacher(int) method of the TeacherManager class
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>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>When invalid parameters are passed, e.g. http://localhost:8800/deleteTeacher?id=1, it returns:
HTTP status: Bad Requestfalse<br/>
<a href='/'>Back</a>
Run Unit Tests
- JettyApplicationTest.java is a unit test that tests the web application using API calls.
- SeleniumWebDriverTests.java is a web GUI test that tests the web application by emulating user actions in the Firefox browser.
Additional Information
For Implementation:
- http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/bind/annotation/RequestMapping.html
- http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/bind/annotation/RequestParam.html
- http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/bind/annotation/ResponseBody.html
- https://tomcat.apache.org/tomcat-8.0-doc/servletapi/javax/servlet/http/HttpServletRequest.html
- https://tomcat.apache.org/tomcat-8.0-doc/servletapi/javax/servlet/http/HttpServletResponse.html
For API testing:
GUI for testing:
- ^ 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.