Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 19c7c4a

Browse files
Added JspRedirect folder in the MVCPractice section of a small course on Java EE
1 parent 092a51b commit 19c7c4a

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package PartThree.JspRedirect;
2+
/*
3+
Запускать через браузер по адресу (у меня настроено на:)
4+
http://localhost:8080/content-first
5+
*/
6+
import jakarta.servlet.ServletException;
7+
import jakarta.servlet.annotation.WebServlet;
8+
import jakarta.servlet.http.HttpServlet;
9+
import jakarta.servlet.http.HttpServletRequest;
10+
import jakarta.servlet.http.HttpServletResponse;
11+
12+
import java.io.IOException;
13+
14+
@WebServlet("/content-first")
15+
public class ContentServletFirst extends HttpServlet {
16+
@Override
17+
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
18+
/*
19+
В данном случае мы прописали путь руками, хотя можно создать
20+
утилитный (вспомогательный) класс, для упрощения написания пути
21+
к закрытым в WEB-INF/jsp/ страницам. Смотреть ContentServletSecond.
22+
*/
23+
req.getRequestDispatcher("/WEB-INF/jsp/content.jsp").forward(req,resp);
24+
}
25+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package PartThree.JspRedirect;
2+
/*
3+
Запускать через браузер по адресу (у меня настроено на:)
4+
http://localhost:8080/content-second
5+
*/
6+
import PartThree.UtilHelper.JspPathHelper;
7+
import jakarta.servlet.ServletException;
8+
import jakarta.servlet.annotation.WebServlet;
9+
import jakarta.servlet.http.HttpServlet;
10+
import jakarta.servlet.http.HttpServletRequest;
11+
import jakarta.servlet.http.HttpServletResponse;
12+
13+
import java.io.IOException;
14+
15+
@WebServlet("/content-second")
16+
public class ContentServletSecond extends HttpServlet {
17+
@Override
18+
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
19+
/*
20+
В данном случае мы прописали только название jsp страницы, мы использовали
21+
наш JspWritePathHelper и его метод *.getJspPath(), которые задают к имени
22+
странички 'префикс' - /WEB-INF/jsp/ и 'постфикс' - .jsp. И это логично, т.к.
23+
путь к директории и расширение файла сохраняются, для всех jsp в заданной
24+
папке.
25+
*/
26+
String jsp_path = JspPathHelper.getJspPath("content");
27+
System.out.println(jsp_path);
28+
req.getRequestDispatcher(jsp_path).forward(req,resp);
29+
}
30+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package PartThree.JspRedirect;
2+
/*
3+
Запускать через браузер по адресу (у меня настроено на:)
4+
http://localhost:8080/content-jstl-demo?id=2&test=hello_from_jsp
5+
С данного адреса произойдет прямая *.forward(req,resp)
6+
переадресация на content.jsp из папки WEB-INF
7+
*/
8+
import AirportSimulator.DTO.FlightDto;
9+
import AirportSimulator.Service.FlightService;
10+
import PartThree.UtilHelper.JspPathHelper;
11+
import jakarta.servlet.ServletException;
12+
import jakarta.servlet.annotation.WebServlet;
13+
import jakarta.servlet.http.HttpServlet;
14+
import jakarta.servlet.http.HttpServletRequest;
15+
import jakarta.servlet.http.HttpServletResponse;
16+
17+
import java.io.IOException;
18+
import java.util.List;
19+
20+
import static java.util.stream.Collectors.toMap;
21+
22+
@WebServlet("/content-jstl-demo")
23+
public class ContentServletThird extends HttpServlet {
24+
private final String jspPath = JspPathHelper.getJspPath("content");
25+
private final FlightService flightService = FlightService.getInstance();
26+
27+
@Override
28+
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
29+
List<FlightDto> flightDtoList = flightService.findAll();
30+
31+
req.setAttribute("flights", flightDtoList);
32+
req.setAttribute("flightsSize", flightDtoList.size());
33+
34+
req.getSession().setAttribute("flightsMap", flightDtoList.stream().
35+
collect(toMap(FlightDto::getId, FlightDto::getDescription)));
36+
37+
req.getRequestDispatcher(jspPath).forward(req,resp);
38+
}
39+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /