Я имею в виду проект на github, который представляет собой приложение todo. У него только один сервлет, и у него есть аннотация @WebServlet ("/"). Вот код для сервлета
@WebServlet("/")
public class TodoController extends HttpServlet {
private static final long serialVersionUID = 1L;
private TodoDao todoDAO;
public void init() {
todoDAO = new TodoDaoImpl();
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
User user = new User();
HttpSession session = request.getSession();
session.setAttribute("username", user.getUsername());
String action = request.getServletPath();
try {
switch (action) {
case "/new":
showNewForm(request, response);
break;
case "/insert":
insertTodo(request, response);
break;
case "/delete":
deleteTodo(request, response);
break;
case "/edit":
showEditForm(request, response);
break;
case "/update":
updateTodo(request, response);
break;
case "/list":
listTodo(request, response);
break;
default:
RequestDispatcher dispatcher = request.getRequestDispatcher("LogoutController");
dispatcher.forward(request, response);
break;
}
} catch (SQLException ex) {
throw new ServletException(ex);
}
}
private void listTodo(HttpServletRequest request, HttpServletResponse response)
throws SQLException, IOException, ServletException {
List<Todo> listTodo = todoDAO.selectAllTodos();
request.setAttribute("listTodo", listTodo);
RequestDispatcher dispatcher = request.getRequestDispatcher("todo/todo-list.jsp");
dispatcher.forward(request, response);
}
private void showNewForm(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
RequestDispatcher dispatcher = request.getRequestDispatcher("todo/todo-form.jsp");
dispatcher.forward(request, response);
}
private void showEditForm(HttpServletRequest request, HttpServletResponse response)
throws SQLException, ServletException, IOException {
int id = Integer.parseInt(request.getParameter("id"));
Todo existingTodo = todoDAO.selectTodo(id);
RequestDispatcher dispatcher = request.getRequestDispatcher("todo/todo-form.jsp");
request.setAttribute("todo", existingTodo);
dispatcher.forward(request, response);
}
private void insertTodo(HttpServletRequest request, HttpServletResponse response) throws SQLException, IOException {
String title = request.getParameter("title");
String username = request.getParameter("username");
String description = request.getParameter("description");
/*DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-mm-dd");
LocalDate targetDate = LocalDate.parse(request.getParameter("targetDate"),df);*/
boolean isDone = Boolean.valueOf(request.getParameter("isDone"));
Todo newTodo = new Todo(title, username, description, LocalDate.now(), isDone);
todoDAO.insertTodo(newTodo);
response.sendRedirect("list");
}
private void updateTodo(HttpServletRequest request, HttpServletResponse response) throws SQLException, IOException {
int id = Integer.parseInt(request.getParameter("id"));
String title = request.getParameter("title");
String username = request.getParameter("username");
String description = request.getParameter("description");
//DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-mm-dd");
LocalDate targetDate = LocalDate.parse(request.getParameter("targetDate"));
boolean isDone = Boolean.valueOf(request.getParameter("isDone"));
Todo updateTodo = new Todo(id, title, username, description, targetDate, isDone);
todoDAO.updateTodo(updateTodo);
response.sendRedirect("list");
}
private void deleteTodo(HttpServletRequest request, HttpServletResponse response) throws SQLException, IOException {
int id = Integer.parseInt(request.getParameter("id"));
todoDAO.deleteTodo(id);
response.sendRedirect("list");
}
}
И вот некоторые из JSP кода, который вызывает сервлет
'' '
Todo App
<ul class="navbar-nav">
<li><a href="<%=request.getContextPath()%>/list"
class="nav-link">Todos</a></li>
</ul>
<ul class="navbar-nav navbar-collapse justify-content-end">
<li><a href="<%=request.getContextPath()%>/LogoutController"
class="nav-link">Logout</a></li>
</ul>
</nav>
</header>
<div class="row">
<!-- <div class="alert alert-success" *ngIf='message'>{{message}}</div> -->
<div class="container">
<h3 class="text-center">List of Todos</h3>
<hr>
<div class="container text-left">
<a href="<%=request.getContextPath()%>/new"
class="btn btn-success">Add Todo</a>
</div>
<br>
<table class="table table-bordered">
<thead>
<tr>
<th>Title</th>
<th>Target Date</th>
<th>Todo Status</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<!-- for (Todo todo: todos) { -->
<c:forEach var="todo" items="${listTodo}">
<tr>
<td><c:out value="${todo.title}" /></td>
<td><c:out value="${todo.targetDate}" /></td>
<td><c:out value="${todo.status}" /></td>
<td><a href="edit?id=<c:out value='${todo.id}' />">Edit</a>
<a
href="delete?id=<c:out value='${todo.id}' />">Delete</a></td>
<!-- <td><button (click)="updateTodo(todo.id)" class="btn btn-success">Update</button>
<button (click)="deleteTodo(todo.id)" class="btn btn-warning">Delete</button></td> -->
</tr>
</c:forEach>
<!-- } -->
</tbody>
</table>
</div>
</div>
<jsp:include page="../common/footer.jsp"></jsp:include>
`` `
Я хочу добавить еще один сервлет, но я предполагаю, что все запросы идут к этому существующему сервлету, и я не совсем понимаю, как здесь выполняется вызов из jsp сервлета. Я только начал работать над JSP и сервлетами, поэтому любая помощь будет очень полезна.