Servlet, JSP/JSP
JSP 기초2
구리Guri
2020. 4. 27. 17:45
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script src="https://code.jquery.com/jquery-3.5.0.js"></script>
</head>
<body>
<form action="index2.jsp" method="post"> <!-- action의 목적지에 Servlet을 넣었듯이 JSP도 넣을 수 있다 -->
<input type=text name=msg>
<input type=submit>
</form>
</body>
</html>
index2.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<% int a = 10; %>
<script>
// scriptlet에서 만들어진 변수는 server의 ram영역에 존재하는 것이기 때문에
// client는 그 변수를 알 수 없음(정의한적이 없다고 함)
// 그럼 어떻게 해결하느냐? 표현식을 이용하여서 a에 있는 값을 전송함
var result = <%= a %>+ 5; // a에 있는 '''값'''을 문자열로 만들어서 통째로 가져감 클라이언트한테 보내줌
//자바스크립트는 Client에서 실행되는 언어이기 때문에 Server에서 자바스크립트의 변수를 가져올 수 없다.
</script>
<%
//Scriptlet 위에는 request와 response가 있다, 지역변수가 있다. (보이지 않지만)
//그러므로 Servlet에서 사용하였던 request.getParameter(arg0) 같은걸 다 쓸 수 있다!
String msg=request.getParameter("msg");
System.out.println(msg);
%>
</body>
</html>