반응형
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 서블릿
- Servelt
- tomcat
- jsp
- 데이터 공유
- Container
- servlet
- Spring Boot
- 웹 어플리케이션 감시
- #jsp
- ServletContextListener
- spring security
- #java
- servletContext
- @PathVariable
- #페이지이동
- ServletConfig
- 디자인패턴
- 점사라짐
- spring5
- Spring
- #sendRedirect()
- 서블릿 초기화 파라미터
- MVC
- 한글 처리
- 확장자 사라짐
- oauth
Archives
- Today
- Total
Choi의 고유결계
JSP Servlet: response.sendRedirect() 사용시 주의 해야할 점 본문
반응형
response.sendRedirect() 사용시 주의 해야할 점
1. out.print() 무시
sendRedirect가 호출되기 전까지는 앞에서 써왔던 out.print() 들은 무시하게 됩니다.
if(vale)
{
out.print("<script>alert('접속 불가');</script>");
response.sendRedirect("main.html");
}
이 위의 예제를 실행시키면 alert()는 실행 되지 않고 main.html로 이동하게된다.
해결방안
원하는 의도를 내려면 sendRedirect를 사용하기 보다는 javascript를 이용해 페이지를 이동합니다.
if(vale)
{
out.print("<script>alert('접속 불가');</script>");
out.print("location.href = 'main.jsp';");
}
print()에 location.herf=’이동할곳’ 이런식으로 적어줘 사용하시면 됩니다.
밑의 소스 결과는 어떻게 될까요? hi가 출력할까요?
if(vale)
{
out.print("<script>alert('접속 불가');</script>");
response.sendRedirect("main.html");
System.out.print("hi");
}
답은 hi가 출력된다 입니다.
sendRedirect는 호출되더라도 나머지 코드들은 그대로 실행된다는것입니다.
결론
“sendRedirect가 실행되기전 html이나 javascript로 out put되는 코드들은 모두 실행되지 않습니다. 그러므로 가급적이면 모든 기능들을 실행한 후에 마지막에 써주는것이 좋습니다.”
반응형
'Jsp,Servlet' 카테고리의 다른 글
JSP Servlet: 액션태그 (0) | 2018.12.30 |
---|---|
[java에서 MongDB 사용하기] - 1. 몽고디비 연결하기 (0) | 2018.11.28 |
JSP Servlet: 웹어플리케이션 감시: ServletContextListener (0) | 2018.09.29 |
JSP Servlet: 데이터 공유 : ServletContext (0) | 2018.09.29 |
JSP Servlet: 특정 서블릿 초기화 파라메터 만들기 (0) | 2018.09.13 |
Comments